the_it_crowd
the_it_crowd

Reputation: 59

Setting PHP session variables with ajax

Want to change value of SESSION variable "fullname" without refreshing the page.

My implementation using ajax:

Page 1 html:

<input type="text" name="fullname" id="fullname" placeholder="Full name">

<button onclick="setSession()"> GO </button>

Page 1 script:

<script>
function setSession(){
   var fullname = $("#fullname").val();
   var dataString = 'fullname=' + fullname;

   $.ajax({
        type: "POST",
        url: "Page2.php",
        data: dataString,
        cache: false,
        success: function( data ) {
             if(data === 'True'){
                 alert("<?php echo $_SESSION['fullname'];?>");
              }
            }
        });
      }
</script>

And in Page 2:

session_start();
$_SESSION["fullname"] = $_POST["fullname"];
echo 'True';
exit();

It doesn't change the value of the session variable.

Both pages have session_start().

Upvotes: 0

Views: 15441

Answers (4)

Don&#39;t Panic
Don&#39;t Panic

Reputation: 41810

Your code should already be changing the value in the PHP session. You just don't have your client-side script set up properly to show that.

Return some kind of indicator in your PHP script:

<?php
// Page2.php

session_start();

$_SESSION["fullname"] = $_POST["fullname"];

echo 'set session fullname to ' . $_POST['fullname'];

Then in your AJAX success function, show that response:

...
success: function( response ) {
    alert(response);
}
...

When you use alert("<?php echo $_SESSION['fullname'];?>"); in your success function, PHP will fill in the $_SESSION['fullname'] value in that alert box once when the page loads, and it will never change until the page is reloaded, even if you do successfully update the session value via AJAX.

Upvotes: 3

dokgu
dokgu

Reputation: 6030

Page 1 HTML

<input type="text" name="fullname" id="fullname" placeholder="Full name">
<button onclick="setSession()"> GO </button>

Page 1 Script

<script>
function setSession(){
   $.ajax({
        type: "POST",
        url: "Page2.php",
        data: { fullname: $("#fullname").val() },
        dataType: "json",
        cache: false,
        success: function( data ) {
             alert(data.fullname);
        }
   });
}
</script>

Page 2 PHP Script

session_start();
$_SESSION["fullname"] = $_POST["fullname"];
echo json_encode(array('fullname' => $_SESSION['fullname']));

It's generally a bad idea to mix server-side and client-side scripts together so try to separate your PHP and Javascript logic. They both execute at different times/stages of a page request life-cycle.

Upvotes: 2

AltShiftZero
AltShiftZero

Reputation: 363

You are setting the variable in JS using PHP Inline coding. You could however echo the session variable in page2 if no post has been set. If you then request page two without any post you can use the response body in JS to get the current set session variable and put it in a JS variable.

Var name;
$.get(‘page2.php’, function(data){ name = data; } );

I’m using the iOS app so above code is not complete but an indication for what can be done.

Upvotes: 0

Jay Blanchard
Jay Blanchard

Reputation: 34406

First, have you watched the AJAX request / response in the browser's developer tools? Have you included the jQuery library in the project? Are there any errors reported? Are you running this on a web-server?

Second, you're starting a session in a remote page. That session data will not be available in the current page until you reload the current page. In addition you have some wonky quoting in your alert, it should be:

alert("<?php echo $_SESSION['fullname'];?>");

Upvotes: 1

Related Questions