User clicks on my site's url with a GET parameter like so:
www.mywebsite.com/index.php?name=Mark
Let's say index.php
looks like that:
if(isset($_GET['param'])){
MyPhpClass::staticFunction();
exit();
}
include 'index.html';
Let's make index.html
look like this:
<html>
<head></head>
<body><div id='greetings'>Hello</div></body>
</html>
What I want is to do is replace
Hello
withHi there
using thePHP
staticFunction
. Note: this is just a toy example. In reality I would like to simulate calling a Javascript function from a PHP function either by calling directly or having some global variable accessible for both.
I have tried something like this:
public static function staticFunction(){
echo "<script type='text/javascript'>$('#greetings').innerHTML = 'Hi there'</script>";
}
But it doesn't seem to work which makes sense. Another thing that I've tried is setting a global variable in PHP
and then trying to access it from JavaScript
like so:
public static function staticFunction() {
$GLOBALS['myglobalvariable'] = 'HiThere';
}
And then including a Javascript
inside index.html
:
var myglobalvariable = <?php echo $GLOBALS['myglobalvariable']; ?>;
if(myglobalvariable == 'HiThere') {
$('#greetings').innerHTML = 'Hi there';
}
I suspect that I could make it work using one of the solutions I've mentioned (there might be some bugs there) but I would be very grateful for a suggestion for an elegant solution to the problem I described.
I by no means expect a full solution - a push in the right direction will be great. Thanks!