简体   繁体   中英

Send a form array to PHP using AJAX as a POST array

Guys I am looking for a way to send my html form array to the php script to execute as an array.I tried several methods but nothing worked for me.Please suggest me a method to do it.My php array works well with normal submit but with ajax it says

Warning: Invalid argument supplied for foreach() in C:\\wamp\\www\\submit_order.php on line 42

My HTML form code-

 <input disabled="disabled" class="input_text" onkeyup="JAVASCRIPT:check_row();" name="qty[]" type="text" id="qty_field1" size="6">

My current ajax send params-

var params="qty="+document.table_form.elements["qty[]"]";   

My PHP array fetch -

  foreach($_POST['qty'] as $value){
        if($index<=$rows){
        $clean_value=mysql_real_escape_string($value);
        $clean_value=stripcslashes($clean_value);
        $product_data[$index][3]=$clean_value;
        $index=$index+1;
        }    

Thank you.

EDIT

I am a bonehead--you are not using jQuery. Here is some coee which iterates the inputs in a form looking for elements named qty[] http://jsfiddle.net/JAAulde/WQjQM/6/

END EDIT

Original (jQuery) Answer Below

If I have 3 inputs named qty[] with values a , b , c

Both:

var params = $.param( $( '[name="qty\[\]"]' ) );

(demo: http://jsfiddle.net/JAAulde/WQjQM/1/ )

and:

var params = $( '[name="qty\[\]"]' ).serialize();

(demo: http://jsfiddle.net/JAAulde/WQjQM/ )

give me:

qty%5B%5D=a&qty%5B%5D=b&qty%5B%5D=c

This should be correct for you.

You may want to look at http://api.jquery.com/serialize/ as a solution of how to send off the form values. It should work with arrays (I think)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM