2011-02-15 13:28:57 8 Comments
I am trying to send data from a form to a database. Here is the form I am using:
<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>
The typical approach would be to submit the form, but this causes the browser to redirect. Using jQuery and Ajax, is it possible to capture all of the form's data and submit it to a PHP script (in example, form.php)?
Related Questions
Sponsored Content
18 Answered Questions
40 Answered Questions
[SOLVED] Is there an "exists" function for jQuery?
- 2008-08-27 19:49:41
- Jake McGraw
- 695693 View
- 2504 Score
- 40 Answer
- Tags: javascript jquery
27 Answered Questions
[SOLVED] jQuery scroll to element
- 2011-07-13 09:49:44
- DiegoP.
- 2137497 View
- 2021 Score
- 27 Answer
- Tags: javascript jquery
40 Answered Questions
[SOLVED] Setting "checked" for a checkbox with jQuery?
- 2009-01-08 22:20:24
- tpower
- 2983175 View
- 3787 Score
- 40 Answer
- Tags: javascript jquery checkbox selected checked
53 Answered Questions
[SOLVED] How do I check if an element is hidden in jQuery?
- 2008-10-07 13:03:18
- Philip Morton
- 2365443 View
- 7082 Score
- 53 Answer
- Tags: javascript jquery dom visibility
32 Answered Questions
15 Answered Questions
[SOLVED] "Thinking in AngularJS" if I have a jQuery background?
- 2013-02-21 04:09:56
- Mark Rajcok
- 799732 View
- 4522 Score
- 15 Answer
- Tags: javascript jquery angularjs
59 Answered Questions
18 Answered Questions
[SOLVED] Abort Ajax requests using jQuery
- 2009-01-15 12:45:18
- lukewm
- 533541 View
- 1706 Score
- 18 Answer
- Tags: javascript jquery ajax
31 Answered Questions
[SOLVED] How to manage a redirect request after a jQuery Ajax call
- 2008-10-13 21:29:50
- Elliot Vargas
- 773598 View
- 1249 Score
- 31 Answer
- Tags: javascript jquery ajax redirect
11 comments
@Mr. Alien 2014-01-17 17:24:12
I would like to share a detailed way of how to post with PHP + Ajax along with errors thrown back on failure.
First of all, create two files, for example
form.php
andprocess.php
.We will first create a
form
which will be then submitted using thejQuery
.ajax()
method. The rest will be explained in the comments.form.php
Validate the form using jQuery client-side validation and pass the data to
process.php
.Now we will take a look at
process.php
The project files can be downloaded from http://projects.decodingweb.com/simple_ajax_form.zip.
@pawan sen 2016-05-18 06:33:25
handling ajax error and loader before submit and after submitting success show alert boot box with an example:
@Waseem Bashir 2017-10-23 06:58:54
Hi please check this is the complete ajax request code.
@mekwall 2011-02-15 13:32:44
Basic usage of
.ajax
would look something like this:HTML:
JQuery:
Note: Since jQuery 1.8,
.success()
,.error()
and.complete()
are deprecated in favor of.done()
,.fail()
and.always()
.Note: Remember that the above snippet has to be done after DOM ready, so you should put it inside a
$(document).ready()
handler (or use the$()
shorthand).Tip: You can chain the callback handlers like this:
$.ajax().done().fail().always();
PHP (that is, form.php):
Note: Always sanitize posted data, to prevent injections and other malicious code.
You could also use the shorthand
.post
in place of.ajax
in the above JavaScript code:Note: The above JavaScript code is made to work with jQuery 1.8 and later, but it should work with previous versions down to jQuery 1.5.
@Thew 2011-02-15 13:34:18
I can do something with that ;) But how should the form / php code look like?
@Andrey Mikhaylov - lolmaus 2013-06-05 06:44:33
Edited your answer to fix a bug:
request
was declared as a local var makingif (request) request.abort();
never work.@Taj Morton 2013-08-04 23:07:53
Shouldn't
serializedData
be$inputs.serialize()
, or is there a reason you serialize and submit the whole form?@mekwall 2013-08-05 07:35:54
@TajMorton Doesn't really matter. In this case they would do the exact same thing. The reason I serialize the form is for ease of use and readability. As stated in the docs: It is typically easier, however, to select the
<form>
itself for serialization@Philibert Perusse 2013-10-22 22:50:04
A VERY IMPORTANT note, because I spent/wasted/invested a lot of time trying to use this example. You need to either bind the event inside a $(document).ready block OR have the FORM loaded before the bind is executed. Otherwise, you spend a lot of time trying to figure out WHY in hell the binding isn't called.
@mekwall 2013-10-23 10:11:28
@PhilibertPerusse Like with any event binding you obviously need the element to exist in the DOM before trying to bind to it, or if you use a delegated bind.
@Philibert Perusse 2013-10-23 19:11:37
Yes, I understand that now. But I found many examples that always put a $(document).ready block around so that the example is self-contained. I wrote the comment for a future user who may, like me, stumble on this and end-up reading the comment thread and this beginner 'tip'
@Ben Flynn 2014-04-16 04:13:07
If you are applying this to your own code, note that the 'name' attributes are critical to the inputs otherwise
serialize()
will skip them.@JasonDavis 2015-05-27 19:35:06
Could someone kindly explain the purpose of the
request.abort();
part. Running the code as is keeps throwing an abort error. When I disable that part my AJAX post works without error. Just want to understand fully what that part is doing and why I should or shouldn't need it? Thanks in advance anyone? The comment saysAbort any pending request
so i'm thinking possibly to prevent multiple posts at once?@user4393693 2016-04-04 13:41:41
how to link the html with your js?
@Robert Rocha 2016-08-24 01:55:06
Is it necessary to serialize the data? Can I just get the form values and send them instead of serailizing?
@Juned Ansari 2017-04-13 13:08:28
Example:
@Uchiha Itachi 2016-05-08 14:22:41
I am using this simple one line code for years without problem. (It requires jquery)
Here ap() means ajax page and af() means ajax form. In a form just simply calling af() function will post form to the url and load response on desired html element.
@johny why 2018-06-13 23:36:03
I wish you included the server file! No idea how to test.
@NullPoiиteя 2013-01-08 15:06:45
To make ajax request using jQuery you can do this by following code
HTML:
JavaScript:
Method 1
Method 2
The
.success()
,.error()
, and.complete()
callbacks are deprecated as of jQuery 1.8. To prepare your code for their eventual removal, use.done()
,.fail()
, and.always()
instead.MDN: abort()
. If the request has been sent already, this method will abort the request.so we have successfully send ajax request now its time to grab data to server.
PHP
As we make a POST request in ajax call (
type: "post"
) we can now grab data using either$_REQUEST
or$_POST
You can also see what you get in POST request by simply either, Btw make sure that $_POST is set other wise you will get error.
And you are inserting value to database make sure you are sensitizing or escaping All request ( weather you made GET or POST) properly before making query, Best would be using prepared statements.
and if you want to return any data back to page, you can do it by just echoing that data like below.
and than you can get it like
There are Couple of Shorthand Methods you can use below code it do the same work.
@Clarence 2013-02-12 09:48:25
Pointer : what is
bar
in$id = $_POST['bar'];
and how it works ???@NullPoiиteя 2013-02-12 10:07:13
@Clarence bar is input type text name and since i am suing post method so $_POST['bar'] is used to get value of it
@K. Kilian Lindberg 2013-06-01 13:39:12
For anyone wanting to use json - while using JSON the call should contain the parameter dataType: 'json'
@nnnnnn 2013-06-01 13:41:59
@CarlLindberg - What if you want jQuery to guess based on the MIME type of the response (which is what it should do when you don't set the
dataType
), so that you can potentially accept JSON or some other format?@K. Kilian Lindberg 2013-06-01 14:02:28
@nnnnnn you are right - that's way better - indeed is default: Intelligent Guess
@Adelmar 2014-08-18 23:21:31
To access the JSON response object (data.returned_val), don't forget to include dataType: "json" in your original ajax call
@user1788736 2015-10-04 17:41:31
@NullPoiиteя can you tell me the steps of method1 if i want to send the form data to google spreadsheet ?
@kta 2016-02-28 03:21:19
Finding form elements and and later serialize it and afterword post it by ajax is a life saver.
@DDeme 2015-01-07 09:38:14
HTML:
JavaScript :
@Shaiful Islam 2015-02-03 21:41:14
I use this way.It submit everything like files
@john 2014-09-10 12:26:29
@NullPoiиteя 2015-05-08 06:47:29
what id difference between yours and other answer ?
@john 2015-05-13 13:24:17
it is posted by me others are by others,.
@user2610222 2013-09-10 09:10:49
You can use serialize. Below is an example.
@foochow 2014-02-26 01:38:14
$.parseJSON()
is a total lifesaver, thanks. I was having trouble interpreting my output based on the other answers.