Question : session variable $subtotal not printing

i have an existing session of which

I am trying to display


I am using the same url


   $_SESSION["subtotal"]=$_GET["subtotal"];
   echo $_SESSION["subtotal"];  
   $_SESSION['subtotal']=$_GET['subtotal'];
   echo $_SESSION['subtotal'];
   
   $subtotal1=$_GET['subtotal'];
   echo $subtotal1;  
   $subtotal2=$_GET["subtotal"];
   echo $subtotal2;
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
<?php
    session_start();
    if(isset($_GET["sessionVar"])) {
        $svar = $_GET["sessionVar"];
        $_SESSION["sessionVar"] = $svar;
echo $_SESSION["sessionVar"];
echo 'isset';
echo $svar;




   $_SESSION["subtotal"]=$_GET["subtotal"];
   echo $_SESSION["subtotal"];   
   $_SESSION['subtotal']=$_GET['subtotal'];
   echo $_SESSION['subtotal']; 
   
   $subtotal1=$_GET['subtotal'];
   echo $subtotal1;   
   $subtotal2=$_GET["subtotal"];
   echo $subtotal2;



    }
    else {
      echo 'else';
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script language="javascript">
    function saveChoiceToSession(o) {
        var sessionVar = o.options[o.selectedIndex].value;
        if(sessionVar!=null) {
            var xmlhttp = null;
            if(window.XMLHttpRequest) { xmlhttp=new XMLHttpRequest(); } else{ xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); }
            if(xmlhttp != null) {
                xmlhttp.open("GET","saveToSession.php?sessionVar="+sessionVar, false);
                xmlhttp.send();
                document.getElementById("displayhere").innerHTML = xmlhttp.responseText;
            }
        }
    }
</script>
</head>
<body>
<select onchange="saveChoiceToSession(this)">
    <option value="">Choose one...</option>
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
</select>
<br />
<div id="displayhere"></div>
</body>
</html>
<?
    }
?>

Answer : session variable $subtotal not printing

You need to pass subtotal parameter to your php ajax script :

For example if subtotal is associated to the value of a textbox (check line 42) : < input type="text"

We get the value of the texbox (line 24)
And pass it as param with the Ajax call (line 29)
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
<?php
    session_start();
    if(isset($_GET["sessionVar"])) {
		
		$_SESSION["subtotal"] = $_GET["subtotal"];
		echo "echo 1 : " . $_SESSION["subtotal"] . "\n";	   
		$_SESSION["subtotal"] = $_GET["subtotal"];
		echo "echo 2 : " . $_SESSION['subtotal'] . "\n"; 
    
		$subtotal1 = $_GET['subtotal'];
		echo "echo 3 : " . $subtotal1 . "\n";   
		$subtotal2 = $_GET["subtotal"];
		echo "echo 4 : " . $subtotal2 . "\n";
	}
    else {
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script language="javascript">
    function saveChoiceToSession(o) {
        var sessionVar = o.options[o.selectedIndex].value;
		var subtotalValue = document.getElementById("subtotalTextbox").value;
        if(sessionVar!=null) {
            var xmlhttp = null;
            if(window.XMLHttpRequest) { xmlhttp=new XMLHttpRequest(); } else{ xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); }
            if(xmlhttp != null) {
                xmlhttp.open("GET","saveToSession.php?sessionVar=" + sessionVar + "&subtotal=" + subtotalValue,false);
                xmlhttp.send();
				debugger;
                alert(xmlhttp.responseText);
                return;
            }
            alert("bad news : ajax call failed !");
            return;
        }
    }
</script>
</head>
<body>
Subtotal : <input type="text" value="10" id="subtotalTextbox" name="subtotalTextbox" /><br />
<select onchange="saveChoiceToSession(this)">
    <option value="">Choose one...</option>
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
</select>
</body>
</html>
<?
    }
?>
Random Solutions  
 
programming4us programming4us