I have a javascript function called check1()
that connects to server side via ajax and returns results. This occurs three times so check1 is called three times. If result is ok i then call function htmlnow1 . If result is not ok i call htmlnow2. First one i use $html=”hello 1” to return the $html code . Second function i use $html=”hello 2” . The first time htmlnow1()
is triggered. The second and third time htmlnow2()
is triggered. So it should show me logically thinking the first time hello 1 output and the other two times hello 2 output so the output should be hello 1 hello 2 hello 2 . The issue is that on my page i see both hello 2 everytime. So i have output hello 2 hello 2 hello 2 although i check with alert box that only the second and third time is triggered the htmlnow2() function. Any thoughts why this might happening? Any help appreciated!
<script type="text/javascript">
function htmlnow1()
{
<?php
$html=" hello 1";
?>
}
function htmlnow2()
{
<?php
$html=" hello 2";
?>
}
function check1()
{
$.ajax({
url: "/ok/alert.php",
type: "post",
dataType: 'json',
data: {
reg: "success",
checkifexist: 1
},
success:function(response)
{
var JsonObject=response['checkifexist'];
if (JsonObject==1)
{
alert('text ' + JsonObject);
htmlnow();
}
else if (JsonObject==0)
{
alert('text '+JsonObject);
htmlnow1();
}
},
error: function(x,y,z){
alert('An error has occurred:
' + x + '
' + y + '
' + z);
}
});
}
</script>
<script>
window.onload=check1();
</script>
On the server side, the HTML page is generated, including the javascript code that is sent to client to be run. Javascript can not create and run php code, it can only send http request to server to run php code there. The php code is not at client side.
So the server side creates empty functions htmlnow1()
and htmlnow2()
, sets the $html
variable to hello 1
and immediately rewrites it to hello 2
. Calling the empty functions has no effect.