3

I would like to update session variable.

Let me introduce this in simple example. We get a div with input fields printed out by PHP script, with some values etc...

Example PHP code:

echo '
<div id="few-input-fields">
<input id="Name" size="20" value="' . $_SESSION['name'] . '" />
<br />
<input id="Lastname" size="20" value="' . $_SESSION['lastname'] . '" />
</div>
<span id="save">save</span>
</div>
';

Let's say user edit this input field (id=Name) and type name "Mark" inside it and then press save text.

On click it should save/update session variable, without reloading page AND refresh input fields.

Is that possible? Perhaps with ajax / jquery? And most importantly how ?

Drazek
  • 341
  • 2
  • 7
  • 16
  • You would just process this like any other AJAX form. Instead of saving the data to a database or doing something else, you would just have the data saved into the session in your PHP script. – F21 Mar 10 '12 at 23:00
  • possible duplicate of [pass a variable from PHP to JavaScript](http://stackoverflow.com/questions/9650057/pass-a-variable-from-php-to-javascript) - just some moments ago this has been asked. Please use the search first! – hakre Mar 10 '12 at 23:02
  • @hakre Sorry, but that's not even nearly waht i'm looking for. And I used search, thanks for negative score :D – Drazek Mar 10 '12 at 23:26
  • @Drazek: Sorry, it was exactly the other way round (which is asked equally often, so better improve your search skills ;) ): [How can I pass variables from JavaScript to PHP?](http://stackoverflow.com/questions/980770/how-can-i-pass-variables-from-javascript-to-php) – hakre Mar 10 '12 at 23:33
  • @hakre Sorry, my search skills are not bad, it's just my explaining or presenting issue is beyond terrible, because I can't explain what I would like to know, I normaly don't get proper answer, I guess I will have to learn ENGLISH more :) – Drazek Mar 10 '12 at 23:41

1 Answers1

8

Yes, just do a simple AJAX request. With jQuery it would be:

$("#formid").submit(function(){
   $.ajax({
      type: "POST",
      url: "someFileToUpdateTheSession.php",
      data: $(this).serialize(),
      success: function(){
          // Do what you want to do when the session has been updated
      }
   });

   return false;
});

And your PHP:

<?php
   session_start();
   $_SESSION["name"] = $_POST["name"];
   // Add the rest of the post-variables to session-variables in the same manner
?>

Note

You need to add name-attributes to your input-fields.

Christofer Eliasson
  • 32,939
  • 7
  • 74
  • 103
  • Modifying the session data needs to be done on the server side, so you will NEED to submit the form to do that. – F21 Mar 10 '12 at 23:31
  • @phpdev Well the Ajax-request is handled on the server side, so this approach work just as fine as submitting the form the default way. An HTTP POST request will be sent to the server either way. You do not have to submit the form the default way to update the session - try it your self! – Christofer Eliasson Mar 10 '12 at 23:57
  • @ChristoferEliasson: That was in response to Drazek's comment ;) – F21 Mar 11 '12 at 00:09
  • @phpdev Ahh sorry about that, totally missunderstood that comment :) – Christofer Eliasson Mar 11 '12 at 08:51