我正在尝试将数据从表单发送到数据库。这是我使用的表格:
<form name="foo" action="form.php" method="POST" id="foo">
<label for="bar">A bar</label>
<input id="bar" name="bar" type="text" value="" />
<input type="submit" value="Send" />
</form>
典型的方法是提交表单,但这会导致浏览器重定向。使用jQuery和Ajax,是否可以捕获表单的所有数据并将其提交给PHP脚本(例如form.php)?
This is a very good article that contains everything that you need to know about jQuery form submission.
Article summary:
Simple HTML Form Submit
HTML:
JavaScript:
HTML Multipart/form-data Form Submit
To upload files to the server, we can use FormData interface available to XMLHttpRequest2, which constructs a FormData object and can be sent to server easily using the jQuery Ajax.
HTML:
JavaScript:
I hope this helps.