2

I am posting serialized data using this function:

$( "form" ).on( "submit", function( event ) {
  event.preventDefault();
 console.log($( this ).serialize());
  var data = $( this ).serialize();
         $.ajax({
            type: "POST",
            url: "content/rev/a_submit.php",
            data: "data=" + data,
            success: function(result){
                $("#acct_content").html(result);
             }
        });
});

the console.log of the data matches what is in the form:

month1%5B%5D=4&sap=721&name=uname&month1%5B%5D=10000.00&month2%5B%5D=10000.00&month3%5B%5D=0.00&month4%5B%5D=&month5%5B%5D=&month6%5B%5D=&month1%5B%5D=15000&month2%5B%5D=0.00&month3%5B%5D=0.00&month4%5B%5D=&month5%5B%5D=&month6%5B%5D=&month1%5B%5D=50&month2%5B%5D=50&month3%5B%5D=50&month4%5B%5D=&month5%5B%5D=&month6%5B%5D=&month1%5B%5D=15000&month2%5B%5D=10000&month3%5B%5D=0&month4%5B%5D=0&month5%5B%5D=0&month6%5B%5D=0&sap_data%5B%5D=&sap_data%5B%5D=&sap_data%5B%5D=&sap_data%5B%5D= 

What I print_r the array on the php file I get:

Array ( [0] => 10000.00 [1] => 1 [2] => 50 [3] => 15000 )** Array ( [0] => 10000.00 [1] => 1 [2] => 50 [3] => 10000 ) Array ( [0] => 0.00 [1] => 1 [2] => 50 [3] => 0 ) Array ( [0] => [1] => [2] => [3] => 0 ) Array ( [0] => [1] => [2] => [3] => 0 ) Array ( [0] => [1] => [2] => [3] => 0 ) Array ( [0] => [1] => [2] => [3] => )

Th issue lies in the first array (month1). You can see that in the console.log for the 2nd index it shows:

month1%5B%5D=15000

but in the array it has [1] => 1

This happens for each month in that specific index.

I'm a little lost. Index 0 and 1 are handled the exact same way in my php code although only one is correctly getting put into the array...

edit: here is the html/php Getting the variables to pass:

$month1= mysql_query("SELECT conf_budget,incremental,round((confidence*100),0),(updated_at) FROM rev_acct WHERE sap_id = '$sap' AND MONTH = '$mb' ORDER BY updated_at DESC LIMIT 1;");
            $row1 = mysql_fetch_row($month1);
                $bud1 = $row1[0];
                $inc1 = $row1[1];
                $con1 = $row1[2];

The inputs in the form:

<td>$<input class="input-small" name="month1[]" id="conf1_<?php echo $sap; ?>" onkeypress="return isNumberKey(event)" type="text" value="<?php echo $bud1; ?>"></td>
<td>$<input class="input-small" name="month1[]" type="text" id="inc1_<?php echo $sap; ?>" onkeypress="return isNumberKey(event)" value="<?php echo $inc1; ?>" ></td>

EDIT2: adding sumbit code

$month1 = array();
    $month1 = $_POST['month1'];
print_r($month1);

I just var dumped the array and index one is a bool for some reason. The value originally comes from a mysql table where its stored as a decimal. (the same as index 0), yet when I post it it comes in as a bool :\

5
  • 1
    It will be more ok if you just add your html form code. Commented Apr 8, 2014 at 16:10
  • 3
    Perhaps I'm misreading this console.log data, but... aren't month1%5B%5D, &month1%5B%5D=10000.00, etc. the same parameter? That would explain why one would be written while another is overwritten... Commented Apr 8, 2014 at 16:12
  • added code, and @summea if that were the case then Everything would be over writing. But of the 6 fields I pass into 6 different arrays this is the only one that has an issue. Commented Apr 8, 2014 at 16:43
  • @Alex: would be great if you show the code in a_submit.php to get and idea of how you're parsing and decoding the data. Commented Apr 8, 2014 at 17:33
  • You're concatenating a string - "data=" - to the beginning of the data variable in the AJAX call. If you want to do that you must treat it as part of the serialized string or your posted information in PHP is going to be goofed up. Try removing that string and see if your code works as expected. Commented Apr 8, 2014 at 18:01

2 Answers 2

1

There is no problem serializing or deserializing the data, the problem is your ajax call when you send:

data: "data=" + data

The result of this is data=month1=4. That's why you don't get the 4 in the array, since you're concatenating "data" string to the data variable. But I still have no idea why you get the [1] => 1 and why the others values are not in a correct order.

This should work:

$( "input[type='submit']" ).click(function(e){
    e.preventDefault();
    $.ajax({
        type: "POST",
        url: "content/rev/a_submit.php",
        data: $("form").serialize(),
        success: function(result){
            $("#acct_content").html(result);
        }
    });
});

That should be working correctly, I got the right values after decoding and parse the data string. Note that somehow you are not parsing correctly the data var. Try using parse_str as I do. Also, would be great if you show what you have done before the print_r line in your a_submit.php.

Code:

$res = "month1%5B%5D=4&sap=721&name=uname&month1%5B%5D=10000.00&month2%5B%5D=10000.00&month3%5B%5D=0.00&month4%5B%5D=&month5%5B%5D=&month6%5B%5D=&month1%5B%5D=15000&month2%5B%5D=0.00&month3%5B%5D=0.00&month4%5B%5D=&month5%5B%5D=&month6%5B%5D=&month1%5B%5D=50&month2%5B%5D=50&month3%5B%5D=50&month4%5B%5D=&month5%5B%5D=&month6%5B%5D=&month1%5B%5D=15000&month2%5B%5D=10000&month3%5B%5D=0&month4%5B%5D=0&month5%5B%5D=0&month6%5B%5D=0&sap_data%5B%5D=&sap_data%5B%5D=&sap_data%5B%5D=&sap_data%5B%5D=";
print_r(urldecode($res));
$output= array();
parse_str($res, $output);
print_r($output);

Result:

Array(
    [month1] => Array
        (
            [0] => 4
            [1] => 10000.00
            [2] => 15000
            [3] => 50
            [4] => 15000
        )

    [sap] => 721
    [name] => uname
    [month2] => Array
        ...

)
Sign up to request clarification or add additional context in comments.

3 Comments

In the submit I just do this: $month1 = array(); $month1 = $_POST['month1']; print_r $month1;
so it looks like it has something to do with the way I'm assigning $_POST['month1']; to the $month1 array variable. If I just print_r $_POST['month1']; then the correct values print out, but if I print_r the variable they dont.
No, it has to do with concatenating "data=" to you string variable 'data' in your AJAX call. Get rid of that and you should be golden.
0

use this

data: $("form").serialize()

i use the onclick button event instead and i dont need to prevent form from submiting. like this:

$( "input[type='submit']" ).click(function(){
         $.ajax({
            type: "POST",
            url: "content/rev/a_submit.php",
            data: $("form").serialize(),
            dataType: "json",
            success: function(result){
                $("#acct_content").html(result);
             }
        });
});

4 Comments

Can you explain why is this the solution?
i was sugeting a cleaner way to use the same function. avoiding more steps and more junk code you can identify errors more quickly... i cant get to understand yet if php is reciving wrong the data or javascript is sending it wrong. what says the header information of the request?
The data is sended correctly as the console.log shows, the problem is unserializing and/or parsing the data parameter.
so If I change my code to this it works, but posts to another page instead of staying on the same page, which is why I had the prevent default.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.