0

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.

2
  • 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! Commented Aug 6, 2013 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. Commented Aug 6, 2013 at 16:15

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

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?
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.
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.
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?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.