How to import javascript variable from .js file to html document - javascript

I am trying to print out a variable from a default.js file. That variable counts up to a number, which I want to print on the html page.
So as it is now, I have:
We found <script type="text/javascript">
var $tempCountPlaces;
document.write($tempCountPlaces); </script> places for you!
<div id="places_list">
<script type="text/javascript">
var places_file = '<?php echo site_url().'/places.html'; ?>';
var region_id = <?php echo $region; ?>;
var event_type_id = <?php echo $eventtype; ?>;
var seats = '<?php echo $seats; ?>';
</script>
As you can see, I can send files from PHP to Javascript, and then use these variables in the default.js file. But how do I import a variable from default.js to the html page, and then print the variable, as shown above?
The var $tempCountPlaces; IS defined in the default.js file, and it is not a local variable within a function.
So basicly i want to pass the variable from default.js $tempCountPlaces into my HTML file using the script tag.

You might want to consider approaching the problem like this:
HTML file contains:
We found <span id="numOfPlaces"></span> places for you!
JavaScript file contains:
var tempCountPlaces = 10;//example value fetched from your PHP service
document.getElementById("numOfPlaces").innerText = tempCountPlaces;
//of if you choose to use jQuery: $("#numOfPlaces").text(tempCountPlaces);
Your resulting interface would end up looking like this:
We found 10 places for you!

I use to ask the same question, and yes ajax and axios can help, but if you really need to pass any var from directly php to javascript, or from the script in your html yo you doc.js you have to do this:
<head>
<script type="text/javascript">
var places_file = '<?php echo site_url().'/places.html'; ?>';
</script>
<script src="js/youDoc.js" type="text/javascript"></script>
</head>
As you can see, you have only to declare your vars first and then add your js doc.

Related

Word Press PHP ACF Snippets in JS functions

I'm currently adding in Advanced Custom Fields for my site but when I try and add an ACF snippet to any of the functions the content doesn't show in the browser and all my js code in the script tags stop working and displaying in the browser. what am I doing wrong?
the js code is in script tags in the front-page.php file
function changeTestimonial1(){
document.getElementById("client-name").innerHTML ="<?php the_field('client_name1'); ?>";
}
I would suggest constructing your html/php/javascript as such:
<?php
$somevar = 1234;
?>
<HTML>
<script language="JavaScript">
// JS will contain value of php variable $somevar, but make sure you escape the value.
var some_js_var = <?php echo $somevar; ?>
</script>
</HTML>

php variable to javascript variable in html file

i have php file which has a variable which is calculated from a given data. i want to pass this variable to a java script variable which is located in other html file can some one help. i have tried like this but when i tried to display the value nothing comes. i want the variable to display countdown timer.
<html>
<body>
<?php
require_once "getuser.php";
?>
<script>
var arrival_time = "<?php echo $arrival_time; ?>";
</script>
<body>
</html
Try making it the php value vs. a string like this:
var arrival_time = <?php echo $arrival_time; ?>;
Well, I don't know if this is a good practice but this is my method of passing a php value to javascript.
<input type="hidden" value="<?php echo $arrivaltime; ?>" id="atime">
<script>
var arrival_time = $("#atime").val();
</script>

How to use a php inside js

When i use the php and js separately it is working correctly
<script> var test = 'asdasdasd'; </script> <?php $id =
"<script>document.write(test)</script>"; echo $id; ?>
But when I use the php inside the js function it is not working correctly
<script> var test = 'asdasdasd'; <?php $id =
"<script>document.write(test)</script>"; echo $id; ?> </script>
How can make work my second code?
The second code produces wrong HTML/JS:
<script> var test = 'asdasdasd'; <script>document.write(test)</script> </script>
- see for yourself in the generated page.
Since everything between <script> and </script> is considered to be JS code, it will try to parse the inner <script> as Javascript code...and fail.
I think the inner other of <script>...</script> tags (those that are in PHP markup) is not needed at all, just get rid of them and it will work.
You can insert any value from php to absolutely everywhere in your file. Literally everywhere.
While writing client-side code, in the right place open php tag <?php and echo what you need. Don't forget to write a closing tag ?> afterwards.
Here, you are echoing the opening script tag, which you already wrote before. That results in a syntax error:
<script>
<script>
...
</script>

How to input a php variable in a html file

I would like to export a variable from a php file in to a html file.
The php file is called example.php and looks like this:
<?php
$array= ['one','two'];
?>
The html file is called example2.html and want the $array variable to be the input of var list, like this:
<html>
<head>
<script type="text/javascript">
var list = $array
echo list;
</script>
</head>
</html>
I am new on this field and donĀ“t know where to start from, thanks for your help.
You could try json_encode() or implode() for printing your array from to javascript (html), i have shown you two solution below
<html>
<head>
<script type="text/javascript">
var list = <?="['".implode("','",$array)."']";?>;
//or
var list2 = <?=json_encode($array);?>;
</script>
</head>
</html>
you've used echo in your javascript code that is a syntax error, in javascript you can use alert() or console.log for testing your varriable, for example in top code you can try this: alert(list[0]);

Possible to assign PHP $_SESSION variable to javascript variable...?

I am making a game for my computing class. The user has to change settings of their robot before playing.
As the settings file where the user customises their robot is a separate PHP file I was wondering if there was a way to assign the $_SESSION variables to JavaScript variables like so:
var tractionType = <?php echo $_SESSION['traction']; ?>;
...
Is this possible?
Sure, you can either create the whole JS file from PHP, as in
<script type="text/javascript" src="js/settings.php"></script>
or you put those variables inline
<script type="text/javascript">
var my_variable=<?=(int)$_SESSION['my_variable']?>;
</script>

Categories

Resources