0

I tried to create an array with serializeArray and post it to php. but my code doesn't work. I read this questions (question) but I didn't understand my mistake yet.

this is my ajax code

    var str = $("form").serializeArray();
    $.ajax({
        type: "POST",
        url: "myfile.php",
        data: str,
        success: function (value) {
            $("#mydata").html(value);
        }
    });

HTML Code

<form>
    <select name="num0">
        <option value="">num0</option>
        <option value="12">12</option>
        <option value="13">13</option>
    </select>
    <select name="num1">
        <option value="">num2</option>
        <option value="123">123</option>
        <option value="133">133</option>
    </select>
    <select name="num2">
        <option value="">num3</option>      
        <option value="12345">12345</option>
    </select>
</form>

PHP Code

$postarr = array();
$num=$_POST['num0'];
$postarr[]=$num;
$num=$_POST['num1'];
$postarr[]=$num;
$num=$_POST['num2'];
$postarr[]=$num;

it giving me the following error message:

Notice: Undefined index: num0 (and same message for other variables).

By the way, English is not my native language; please excuse typing errors.

7
  • 1
    I don't know if this is related or not, but your <form> tag needs a method. Since it isn't proper HTML, the serialize may not work. Commented Mar 25, 2014 at 16:33
  • use $("form").serialize(), not $("form").serializeArray(); see stackoverflow.com/a/10430571/689579 Commented Mar 25, 2014 at 16:36
  • Did you try to debug with print_r($_REQUEST); ? Commented Mar 25, 2014 at 16:36
  • 1
    @SableFoste: method and action are optional. By default, the form will submit to the current page via GET. Commented Mar 25, 2014 at 16:37
  • Works fine for me: jsfiddle.net/XN6aJ (Check the headers of the request) Commented Mar 25, 2014 at 16:41

3 Answers 3

0

With the name attribute of an input field you are able to create an array in your $_POST:

<form method="post">
    <select name="values[num0]">
        <option value="">num0</option>
        <option value="12">12</option>
        <option value="13">13</option>
    </select>
    <select name="values[num1]">
        <option value="">num2</option>
        <option value="123">123</option>
        <option value="133">133</option>
    </select>
    <select name="values[num2]">
        <option value="">num3</option>      
        <option value="12345">12345</option>
    </select>
</form>

In your php you can use it like this:

$postarr = $_POST['values'];
echo $postarr['num0'];
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks @Dinistro ,but it giving me this error: values are undefined
@ArvinFirouzi did you also copy my html? By me, it works fine.
0

Try this:

   $.ajax({
     type: "POST",
     url: "myfile.php",
     data: $('form').serialize(),
     success: function (value) {
         $("#mydata").html(value);
     }
 });

and instead of

$num=$_POST['num0'] 

use

$num=filter_input(INPUT_POST,'num0');

1 Comment

Thanks @andrew, but when I tried to echo $num, it doesn't echo $num value.
0

Add an id to your form,

  <form id="my_form">
<select name="num0">
    <option value="">num0</option>
    <option value="12">12</option>
    <option value="13">13</option>
</select>
<select name="num1">
    <option value="">num2</option>
    <option value="123">123</option>
    <option value="133">133</option>
</select>
<select name="num2">
    <option value="">num3</option>      
    <option value="12345">12345</option>
</select>

Just change

  var str = $("#my_form").serialize();

In your script

Comments

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.