0

Need help with examples using $.post()

Simplified form with a multi-dimensional array:

<form action="save-objectives.php" name="save-objectives-form" id="save-objectives-form">
 <input type="text" name="objective[0][subject]" value="" />
 <input type="text" name="objective[0][detail]" value="" />
 <input type="submit" value="Save" />
</form>

The name of this array is "objective" (it's dynamically generated so the "objective" array might have many elements). I want to pass this entire array to a PHP page that I call using jquery's .post() method.

I can call the .post method with single values on other forms. I need to pass the entire array, so I want to be able to access that array on the called page like so objectives[0][subject] or objectives[39][detail] and get the value.

Here is how I'm trying to use post

$(document).on('submit', "#save-objectives-form", function(event){
     event.preventDefault();

     var p = $.post("save-objectives.php",  objective:objective);

     p.done(function(data) { ... do other stuff ... } ); 
}); /*end ajax call*/

So the line starting with "var p =" what is the syntax to send the array to the next page via post? I want to be able to loop through the form array on the save-objectives.php page as if it were a regular post array in HTML.

what I'm trying is not working. I have no idea what JSON is, and quite frankly think it might be overkill for what I'm trying to do so it would be best to not use that. Thank you for taking a look.

1 Answer 1

1

Just serialize the form ?

$(document).on('submit', "#save-objectives-form", function(event){
     event.preventDefault();
     $.post("save-objectives.php",  $(this).serialize()).done(function(data) {
          ... do other stuff ... 
     }); 
});
Sign up to request clarification or add additional context in comments.

3 Comments

On the processing save-objectives-php how do I then access the values from the form?
Like you would any other form. Try print_r($_POST) to see what you got.
Thank you the serialize did the job and I am able to access the dynamically created form on the processing page as normal.

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.