How to update SESSION variable with jQuery + AJAX, is it even possible?

后端 未结 1 1823
盖世英雄少女心
盖世英雄少女心 2020-12-12 02:36

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

相关标签:
1条回答
  • 2020-12-12 02:59

    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.

    0 讨论(0)
提交回复
热议问题