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>
Related
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>
Normally that is how one includes JS files in HTML:
<script type="text/javascript" src="myFile.js" ></script>
But I want to write the JS file inline the HTML, using PHP; but still having the possibility of having my neat JS file, that can be chached, debugged and minified.
How can I do something like this?
<script>
<?php include 'myFile.js';?>
</script>
In other words, how to include a JS file in HTML on the server-side. That may get a lot of advantages because it is transparent to the browser.
How can I do something like this?
<script>
<?php include 'myFile.js';?>
</script>
Exactly that code would work, yes. Though it would try to interpret the file as PHP code, and if anything looked like <?php .. ?> inside that file you may see weird side effects. If you want to include the file uninterpreted, simply do:
<script><?php echo file_get_contents('myFile.js'); ?></script>
Be aware of possibly having to escape "</script>" now, should that appear in your included file; see https://stackoverflow.com/a/6908901/476.
YOu need to make custom function or use :
echo '<script type="text/javascript" src="myFile.js" ></script>';
Or
function add_js($filename){
return '<script type="text/javascript" src="'.$filename.'" ></script>';
}
echo add_js('myFile.js');
I notice that I can substitue PHP into JavaScript that's place inline in the HTML, as below:
<script>
jQuery('select#age_group').val(<?php echo $age_group?>);
</script>
But the PHP substitution doesn't work inside a .js file that the PHP file loads. Why is that and how can I do the substituion in the loaded .js file?
Here are two ways you can do this without changing the file extensions that the PHP engine can process.
First, you can set a global variable inline and then access that variable in the JS you include.
<script>global_age_group = <?php echo $age_group?>;</script>
<script src="js_that_accesses_age_group.js" /></script>
(standard warnings about global variables apply)
Second, you can give your JS file a .php extension, so...
<script src="js_that_accesses_age_group.php" type ='application/javascript'/></script>
And then use
<?php echo $age_group?>
in that file as you do in your inline example.
Try this instead... Let me know if it works
</form>
</div>
<?php echo"<script>
jQuery('select#age_group').val($age_group);
</script>";
?>
</body>
</html>
you pass the value on a element attribute or on js var as you did
<script>
var age = 20;
</script>
<script src="main.js"></script>
and your can use the age inside main.js
By default, PHP will only process files ending in certain extensions (most commonly .php). If you want to use PHP in a JavaScript file then the easiest way to do it is to give it a .php extension.
Most browsers will also refuse to process your JavaScript unless it has the right content type, so at the top of your file add
<?php
header("Content-Type: application/javascript");
The Simple answer that you can't !
You can't write a PHP script any where except .php files,
That you can't write php scripts in .html, .css or .js files because the compiler will not pass .js file or .html file to read it!
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.
Non-programmer here, trying to figure something out.
I have a javascript function in the header of my document that upon page load it opens another page in an iframe and reveals an svg file on my server for minor online editing. I would like to place my javascript function in a php file, so that these folder locations of the svg files cannot be determined for anyone to download these svg files freely.
Currently I have this on my html header:
<script type="text/javascript">
function loaded()
{
document.getElementById("myiframe").src="http://www.mydomain.com/myfolder/mypage.html?url=myotherfolder/"+window.location.search.substr(1);
}
onload=loaded;
</script>
Since I have heard that php is a server side script and not viewable, I thought this would mask the location of these files on my server.
I want to remove this javascript code from my header and place it in a php file and replace the header code with either of these:
<script src="phpjavafile.php"></script>
or
<?php include ('phpjavafile.php'; ?>
and finally put the javascript into a php file like this:
<?php
<script type="text/javascript">
function loaded()
{
document.getElementById("myiframe").src="http://www.mydomain.com/myfolder/mypage.html?url=myotherfolder/"+window.location.search.substr(1);
}
onload=loaded;
</script>
?>
I have tried both of these methods and neither load properly.
I must be doing something wrong. Am I on the right track, or is there a better way of getting this to work.
Thank you ahead of time.
By using the echo() function
PHP
<?php
echo '<script>
some javascript
</script';
?>
However if you are just trying to load the php on page load without any php args then just write it out of the tags.
If you have the JavaScript is another file then using
PHP
<?php
include 'path/to/javascript/file.php';
?>
Should work.
You cannot hide javascript as it is interpreted browser side and not server side so the users browser has to have accesses to the code.
Here is the code I think you are asking for:
PHP HTML
<?php
$html = file_get_contents("path/to/data.html");
?>
<div>
<?php echo $html; ?>
</div>
doesn't use an iframe but should still work. However any relative links will not work unless both files are in the same dir and there will be no iframe functionality without additional css
You can just output it as text in your PHP file.
<?php
// Some PHP stuff here
?>
<script type="text/javascript">
function loaded(){
....
}
onload=loaded;
</script>
<?php
// Some more PHP stuff here
?>
Alternatively, you can just link to it in the usual way from within the same file:
<script src="path/to/your/file.js"></script>