Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Hello friends a newbie question. I am new to programming hence please be gentle.

I am trying to post multiple session variables using JavaScript so that I can use them later in my PHP at multiple places.

My index.php file

<?php
   session_start();
?>
<!DOCTYPE html>
<html>
   <head>
      <title></title>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      <?php 
         if ((empty($_SESSION['function1Val'])) & (empty($_SESSION['function2Val'])) && (empty($_SESSION['jsStatus']))) {
            echo '<script type="text/javascript" src="vals.js"></script>
            <script type="text/javascript">
            var session=false;
            var jsStatus;
            var function1Val;
            var function2Val;
            </script>';
         } else {
            echo '<script type="text/javascript"> 
            var session=true; var jsStatus=' . $_SESSION['jsStatus'] . ';
            var session=true; var function1Val=' . $_SESSION['function1Val'] . ';
            var session=true; var function2Val=' . $_SESSION['function2Val'] . ';
            </script>';
         }
      ?>
   </head>
   <body>
    <?php

echo $jsStatus;
echo $function1Val;
echo $function2Val;

session_destroy ();
         ?>
   </body>
</html>

My vals.js file

window.onload = function() {
    // if there is no session (session = false)
    if (!session) {

        // Set value to variable jsStatus
        jsStatus = 'enabled';

        // Call function to get function1
        function1();

        // Call function to get function2
        function2();

        // Make ajax call to php page to set the session variable
        setSession();
    }
}

function function1() {

    // code to get value goes here
    function1Val = 'result1';
}

function function2() {

    // code to get value goes here
    function2Val = 'result2';
}

function setSession() {
    var xmlhttp;
    if (window.XMLHttpRequest)
      {// code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp=new XMLHttpRequest();
      }
    else
      {// code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    xmlhttp.onreadystatechange=function()
      {
      if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
        // Reload the page
        window.location.reload();
        }
      }
    xmlhttp.open("POST","session.php?function1Val=" + function1Val,true);
    xmlhttp.send();

    // like this I want to transfer other values of 'function2Val' and 'jsStatus'
}

My session.php file

<?php
    session_start();

    // check if it is already set if you like otherwise:
    $_SESSION['function1Val'] = $_REQUEST['function1Val'];
    $_SESSION['function2Val'] = $_REQUEST['function2Val'];
    $_SESSION['jsStatus'] = $_REQUEST['jsStatus'];
?>

I know the code is messy but I don't know how to write the correct syntax. I actually tried to modify a single variable code but failed. hence need help.

The idea is to post various values derived out of various JavaScript functions to the session for use by PHP.

Please help.

UPDATE:

It has to be this way as the values to the said variables can be calculated with the help of JavaScript only.

share|improve this question
1  
Man I would suggest you save your php variablees into hidden divs. Then you can use and update those values easily and its more clean! –  Ideal Bakija Aug 6 '13 at 16:09
    
I don't think using hidden divs is going to give you problems in the app. I don't see a reason. They don't take space and its like they don't exist they just hold up the value. –  Ideal Bakija Aug 6 '13 at 16:15

1 Answer 1

up vote 1 down vote accepted

You have to concatenate the parameters with an ampersand (&).

Use this line

xmlhttp.open("POST","session.php?function1Val=" + function1Val+"&function2Val=" + function2Val+"&jsStatus=" + jsStatus,true);

BTW: I would really suggest to use jQuery or a similar library for AJAX requests. Furthermore I would use JSON for exchanging the data or a Javascript array where the key names are those of the variables.

share|improve this answer
    
Thanks for helping. What about this line in index.php ------ if ((empty($_SESSION['function1Val'])) & (empty($_SESSION['function2Val'])) && (empty($_SESSION['jsStatus']))) and also the other parts of code? –  Vikram Rao Aug 6 '13 at 16:52
    
I don't really understand the question. Are you asking what a cleaner solution would be? An easy hack would be to create an array in PHP where the keys are the variable names and the values is the JS code of these. –  Pat Aug 6 '13 at 23:47
    
But use good safeguards against XSS injections then (e.g. refrain to predefined function names instead of pure Javascript code). If really needed I can provide an example tomorrow. –  Pat Aug 6 '13 at 23:56
    
Hi thanks again. Would really appreciate an illustrated example with code. As I mentioned, I am new to programming and still in the learning process. Please help me using the code in my question as example. That will be really very helpful to me. I want to know where all I need to make changes in my code and what all changes I need to make. Also there is no User input involved in this, do I still need to check for XSS injection? –  Vikram Rao Aug 7 '13 at 2:49
    
Any update please? –  Vikram Rao Aug 9 '13 at 4:41

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.