0

I'm sending the following info to a php file. data contains 'one' and 'two'. 'One' contains the serialized form and two contains similar custom info. How can i read those post values with php. I want to be able to differentiated between the value contained in one and value contains into two.

$('form').submit(function() {
     x = $(this).serialize(),
     test = 'testing=whatever&something=else';
    $.ajax({
        type: 'POST',
        data: {'one':x, 'two':test}
        ...
    })
})

How can i read the values in php in such a way where i can do

$one = $_POST['one'];
foreach($one as $key=>$value){ $message.= $key.": ".$value."\r\n"; }
mowwwalker
  • 16,634
  • 25
  • 104
  • 157
Pinkie
  • 10,126
  • 22
  • 78
  • 124

4 Answers4

1

you need to cancel the default behavior of submit

$('form').submit(function(e) {
  e.preventDefault();
     x = $(this).serialize();
     test = 'testing=whatever&something=else';
    $.ajax({
        type: 'POST',
        data: {one:x, two:test}
        ...
    })
})

on the php side

$one = $_POST['one'];
$two = $_POST['two'];

update:

im not that well versed in php but i think the following should work

$one = $_POST['one'];
$two = $_POST['two'];

$cols = explode("&", $one);
foreach($cols as $col) {
    $key_values = explode("=", $col);
    $key = urldecode($key_values[0]);
    $values = urldecode(key_values[1]);
}
echo $key, $values;
Rafay
  • 30,950
  • 5
  • 68
  • 101
  • Thanks, but how can i get the name and values in php so i can do something like `foreach($one as $key=>$value){ $message.= $key.": ".$value."\r\n"; }` – Pinkie Feb 03 '12 at 05:29
  • This works +1, but not an elegant solution. parse_str is what i was looking for. – Pinkie Feb 03 '12 at 20:07
1

I'm not sure what you want to do with the serialised version of the form (x) but you can get access to both of the variables in the receiving PHP script using $_POST as per usual and then probably use parse_str (http://au.php.net/manual/en/function.parse-str.php) to break 'test' out into the various parameters, but I question why you are taking this route instead of breaking the parameters up and passing them as individual arguments in the data argument:

data: {'testing' : whatever, 'something' : else}
Alpaus
  • 646
  • 1
  • 7
  • 21
  • I have a purpose for having 2 array. One array is used as the form values and the other array is used for other purposes. Your answer is not addressing my question. – Pinkie Feb 03 '12 at 05:32
  • Does parse_str not extract the parameters from the string for you? – Alpaus Feb 03 '12 at 09:31
  • Thanks alpaus, parse_str is what i was looking for. It worked as expected. I'm just wondering, since the data is a query string, do we need to url encode it incase it has special characters or spaces. – Pinkie Feb 03 '12 at 20:04
0

If your form contains checkboxes or radioboxes (inputs with same names) use $(this).serializeArray() instead of $(this).serialize() to distinguish between them

0

You need to first convert your form data into JSON! not the query string which serialize does, for that see This, JSON can give you ability to have nested keys.

Then you can put seperate data in different keys in JSON like:

var myData = 
{
    'one': $('form').serializeObject(),
    'two': 'testing=whatever&something=else',
};

$('form').submit(function() {
    $.ajax({
        type: 'POST',
        data: myData,
        ...
    });
});

And the on PHP side you can easily get it using the way you want to:

$one = $_POST['one']
foreach($one as $key=>$value) { $message.= $key.": ".$value."\r\n"; }
Community
  • 1
  • 1
DivinesLight
  • 2,398
  • 2
  • 24
  • 30
  • How do i can get my serializeArray into this JSON format where it is inside `one`and my other values inside `two` – Pinkie Feb 03 '12 at 07:35
  • You should not call jQuery's serialize function, instead copy paste the function from correct answer from the link I gave in my answer and then use: x = $(this).serializeObject(); – DivinesLight Feb 03 '12 at 12:10
  • What is serializeObject(). We can't foreach these values without doing parse_str as aplaus indicated. – Pinkie Feb 03 '12 at 20:06
  • @Pinkie it converts your form data to JSON. And you can parse array one but not array two. And if one is still giving problems use json_decode on it. – DivinesLight Feb 05 '12 at 15:12