3

I was wondering if there was a succinct way to submit a form to my backend with JQuery AJAX (async). What I mean, more specifically, is:

HTML Form:

<form method="post" action="/Intranet/Resources/Forms/WorkOrder.php">
    <input type="text" name="notes[]" />
    <input type="text" name="notes[]" />
    ...
    <!-- a whole bunch more input fields here -->
</form>

This form is pretty awkward to separate into a bunch of variables with JQuery. I don't want the user to be redirected away from the page - I want the form to submit asynchronously. It's actually a save button...

Would it be better to screw it and just use JSON?

3 Answers 3

4
$(function(){
    $("form").submit(function(){
        $.ajax({
            url:"save.php",
            type:"post",
            data:$(this).serialize(),
            success: alert('saved');
        });
    });
});
Sign up to request clarification or add additional context in comments.

Comments

1

You can use $('form').serialize() to serialize a form. The result (for your form) would be:

notes=val1&notes=val2

http://api.jquery.com/serialize/

Comments

1

jQuery Serialize...

Encode a set of form elements as a string for submission.

http://api.jquery.com/serialize/

Use it like so....

$.post("test.php", $("#testform").serialize());

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.