0

I understand how to send this form normally with a regular submit. I however am confused as to how to send my multidimensional form array via .ajax.

My form:

<form action="week_update.php" id="theform" method="post">   
 <input type="text" name="data[][wednesday_crew]" value=""/>
 <input type="text" name="data[][thursday_crew]" value=""/>
 <input type="text" name="data[][friday_crew]" value=""/>
 <input type="text" name="data[][saturday_crew]" value=""/>
 <input type="text" name="data[][sunday_crew]" value=""/>

My PHP:

$stmt = $connection->stmt_init();

if($stmt->prepare("UPDATE tableName SET ... = ?, ... = ?,)) {

// Bind your variables
$stmt->bind_param('ss', $.., $..,);

// get the multi array from form
$returnedData = $_POST['data'];

//let's loop through it.
for($i=0;$i<count($returnedData);$i+=2){
  $log_dates_ID = $returnedData[$i]['...'];
  $crew_chief = $returnedData[$i+1]['...'];
  $stmt->execute();
}
1
  • If you do a form serialization that should work, since you just need to send the urlencoded string to your script Commented Feb 5, 2014 at 21:04

2 Answers 2

1

This is relatively easy, if i have understood you correctly. I answered a question similar to this a while back.

$("#theform").submit(function() {

    var url = "path/to/your/script.php"; // the script where you handle the form input.

    $.ajax({
           type: "POST",
           url: url,
           data: $("#theform").serialize(), // serializes the form's elements.
           success: function(data)
           {
               alert(data); // show response from the php script.
           }
         });

    return false; // avoid to execute the actual submit of the form.
});
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks! What about on the server side?
Server side - The php in your example works, as you said. As this is the case put that code in a file path/to/your/script.php and it will execute on form submission.
The form is submitting, but the data is not being inserted into my database. Any other quick ideas? Thanks again!
For the purpose of error checking, use a form with one input field with a basic value and also ensure your script has a DB connection.
Yes, I can also use the print_r function and see the array. No worries, I'll figure it out. Thanks for your time.
1

You've almost got it, but your input elements should be

<input .... name="data[friday_crew][]" />
                                   ^^--

That'll produce a sub-array under `data for every type you've got. Right now yours will be build some hideous franken-array that'll be really ugly to traverse/analyze. By comparison, the above version you can simply have

foreach($_POST['data']['friday_crew'] as $key => $val) {
    $fri_crew = $val;
    $sat_crew = $_POST['data']['sat_crew'][$key];
    etc...
}

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.