1

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.

2
  • Are you using POST in your AJAX? Commented Jun 21, 2011 at 15:44
  • @Kyle Yes I use POST Kyle @KilZone I'll accept some.I didn't know that function.:( Commented Jun 21, 2011 at 15:47

2 Answers 2

2

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.

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

8 Comments

@JAAulde Thank you for your response but I have more than 40 values with qty[].So do you have some other way to do this.
@Ceylo I fail to see why it makes a difference...have you tried it? Does it work? Is it performing ok?
@JAAulde sorry my mistake.I am new to jquery friend.Can you explain me what is above give me: string.How should I send it to php.How php get this data?.Thanx
@Ceylo no problem. Send it the same way you already are, I imagine: $.ajax( { url: '/yourPage.php', type: 'POST', data: params } );
Oh, dude, I am sooooo sorry. Based on the first answer submitted, and having seen the $ in your PHP code block, I totally effed up and thought you were using jQuery. I've misled you, let me reformulate...
|
1

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)

1 Comment

Or found this which could help if you do not wish to use jQuery: ra-ajax.org/…

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.