Jump to content

Passing array to AJAX


LoneStarJack

Recommended Posts

I have tried everything I could find to combine these two AJAX requests into one request,

Is there a doable answer?

<!doctype html>
<html>
  <head>
    <meta  charset=utf-8"/>
    <title>Test sessions</title>
    <script src="../js/jquery.js" ></script>
 </head>
     
<script>
   $(function() {
    $("#btn_test").click(function(){ 
        $.post("sessions_set.php", {
            key: "test_type",
            value: "any old type"
              });
        $.post("sessions_set.php", {
            key: "test_mode",
            value: "any mode"
              });     
            }); 
    }) ;
</script>

<button id="btn_test" />Click

<?php session_start(); 
    $_SESSION{$_REQUEST["key"]} = $_REQUEST["value"];  
?>
Link to comment
https://forums.phpfreaks.com/topic/284764-passing-array-to-ajax/
Share on other sites

i'm curious how you managed to post this thread three times? there have been other recent double/triple posts.

 

did the forum software act like it wasn't accepting the form submission and you hit the submit button multiple times? are you using a mobile device?

Define your array then serialise it using $.param

<script>
    $(function() {
    $("#btn_test").click(function(){ 

        //            key     :    value
        values = { "test_type": "any old type",
                   "test_mode": "any mode" }

        $.post("sessions_set.php", $.param(values));
    });
</script>

In your PHP code you'd use a loop to define the $_SESSION vars

foreach($_GET as $key => $value)
{
    $_SESSION[$key] = $value;
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.