使用JQuery AJAX向PHP提交HTML表单

时间:2022-11-24 13:26:16

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:

我想知道是否有一种简洁的方式使用JQuery AJAX(异步)向我的后端提交表单。更具体地说,我的意思是:

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...

这个表单很难用JQuery分成一堆变量。我不希望用户被重定向到页面之外 - 我希望表单以异步方式提交。它实际上是一个保存按钮......

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

将它拧干并使用JSON会更好吗?

3 个解决方案

#1


4  

$(function(){
    $("form").submit(function(){
        $.ajax({
            url:"save.php",
            type:"post",
            data:$(this).serialize(),
            success: alert('saved');
        });
    });
});

#2


1  

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

您可以使用$('form')。serialize()来序列化表单。结果(对于您的表单)将是:

notes=val1&notes=val2

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

#3


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());

#1


4  

$(function(){
    $("form").submit(function(){
        $.ajax({
            url:"save.php",
            type:"post",
            data:$(this).serialize(),
            success: alert('saved');
        });
    });
});

#2


1  

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

您可以使用$('form')。serialize()来序列化表单。结果(对于您的表单)将是:

notes=val1&notes=val2

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

#3


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());