Main.php
<?php
session_start();
msgbox("Are you sure?", "confirm");
$Result = $_SESSION['id'] ;
print "<p id='txt'> </p>";
if($Result == 1 )
echo "Result is true";
elseif ( $Result == 2 )
print "<br><br>Return value is '$Result'";
function msgbox($msg, $type)
{
if ($type == "confirm")
{
print "<script src='Validate.js'></script>";
print "<script> ret = confirm ('$msg') ;
if (ret)
Write_to_variable('1');
else
Write_to_variable('2');
</script>";
?>
Validate.js
function Write_to_variable( value )
{
if (window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest();
}
else
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("txt").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","Variable.php?id="+value,true);
xmlhttp.send();
}
Variable.php
<?php
session_start();
if ( $_GET['id'] )
{
$_SESSION['variable'] = $_GET['id'];
print $_SESSION['variable'];
}
?>
What I am trying to do is,
Getting the value from the javascript confirm box and store return value of confirm box to php SESSION
variable using Ajax. In Ajax File I just assigning $_SESSION['id']= $_GET['id']
. When the program get execute, I pressed OK
from the confirm box. It assign 1
to the session variable of id
. If I pressed cancel
next time the value doesn't changed, when I am tried to pressed cancel
another time, the value of session have changed to id
as 2. What is the problem, any suggestions and solution are kindly accepted.
I cant find what is logic hidden behind it. I can clearly see what is returns and what ajax returns but the session variable used in the main.php is changing after second click.
Thanks in Advance.