Reputation: 1491
I am trying to grab a single variable from an array through my function open_boosters()
, then send the variable to my PHP script. What is the correct way to do this?
All I have at the moment is this JavaScript function that generates random numbers, called open_boosters()
.
><img src="site3.php?"action="document.getElementById("?").innerHTML=open_boosters();"/>
In my PHP, should I simply bind the variable with a $_GET method?
Upvotes: 0
Views: 529
Reputation: 14345
It is best practice to move behaviors out of the HTML and into a <script>
tag, or better, an external script file (though with the ability to attach events to HTML elements). In this case, there is no reason to bind to an image element your code which has nothing to do with the image (it looks like it is trying to populate some other element).
<script>
// This code will not work with older IE versions; you may
// wish to use a library like jQuery to handle all the complexities
// of browser compatibility; or you can avoid all this by putting
// the script tag after your elements
window.addEventListener('DOMContentLoaded', function () {
var rand = open_boosters();
document.getElementById("?").innerHTML=rand;
document.getElementById("randImg").src = "site3.php?randomNumber"+rand;
}, false);
</script>
<img id="randImg" />
Then in your PHP code:
<?php
$_GET['randomNumber']; // Do something with it (though safely--e.g., do not add directly to a SQL string, etc.)
?>
Upvotes: 1