找回密码
 注册

QQ登录

只需一步,快速开始

查看: 378|回复: 1

jQuery Ajax POST example - submit all form's data

[复制链接]
发表于 2013-10-3 07:39:59 | 显示全部楼层 |阅读模式
Basic usage of .ajax would look something like this:
HTML:
  1. <form id="foo">

  2. <label for="bar">A bar</label>
  3. <input id="bar" name="bar" type="text" value="" />

  4. <input type="submit" value="Send" />

  5. </form>
复制代码
JavaScript:
  1. // variable to hold request
  2. var request;
  3. // bind to the submit event of our form
  4. $("#foo").submit(function(event){
  5. // abort any pending request
  6. if (request) {
  7. request.abort();
  8. }
  9. // setup some local variables
  10. var $form = $(this);
  11. // let's select and cache all the fields
  12. var $inputs = $form.find("input, select, button, textarea");
  13. // serialize the data in the form
  14. var serializedData = $form.serialize();

  15. // let's disable the inputs for the duration of the ajax request
  16. $inputs.prop("disabled", true);

  17. // fire off the request to /form.php
  18. request = $.ajax({
  19. url: "/form.php",
  20. type: "post",
  21. data: serializedData
  22. });

  23. // callback handler that will be called on success
  24. request.done(function (response, textStatus, jqXHR){
  25. // log a message to the console
  26. console.log("Hooray, it worked!");
  27. });

  28. // callback handler that will be called on failure
  29. request.fail(function (jqXHR, textStatus, errorThrown){
  30. // log the error to the console
  31. console.error(
  32. "The following error occured: "+
  33. textStatus, errorThrown
  34. );
  35. });

  36. // callback handler that will be called regardless
  37. // if the request failed or succeeded
  38. request.always(function () {
  39. // reenable the inputs
  40. $inputs.prop("disabled", false);
  41. });

  42. // prevent default posting of form
  43. event.preventDefault();
  44. });
复制代码
Note: Since jQuery 1.8, .success, .error and .complete are deprecated in favor of .done, .fail and .always.
PHP (i.e. form.php):
  1. // you can access the values posted by jQuery.ajax
  2. // through the global variable $_POST, like this:
  3. $bar = $_POST['bar'];
复制代码
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:
  1. $.post('/form.php', serializedData, function(response) {
  2. // log the response to the console
  3. console.log("Response: "+response);
  4. });
复制代码
Note: The above JavaScript is made to work with jQuery 1.8 but should work with previous versions down to jQuery 1.5.

Marcus Ekwall


 楼主| 发表于 2013-10-3 07:44:28 | 显示全部楼层
You can also do this by:
HTML:
  1. <form id="foo">
  2. <label for="bar">A bar</label>
  3. <input id="bar" name="bar" type="text" value="" />
  4. <input type="submit" value="Send" />
  5. </form>

  6. <!-- The result of the search will be rendered inside this div -->
  7. <div id="result"></div>
复制代码
Script
  1. /* Attach a submit handler to the form */
  2. $("#foo").submit(function(event) {

  3. /* Stop form from submitting normally */
  4. event.preventDefault();

  5. /* Clear result div*/
  6. $("#result").html('');

  7. /* Get some values from elements on the page: */
  8. var values = $(this).serialize();

  9. /* Send the data using post and put the results in a div */
  10. $.ajax({
  11. url: "test.php",
  12. type: "post",
  13. data: values,
  14. success: function(){
  15. alert("success");
  16. $("#result").html('Submitted successfully');
  17. },
  18. error:function(){
  19. alert("failure");
  20. $("#result").html('There is error while submit');
  21. }
  22. });
  23. });
复制代码
Now you can get data in php
  1. $bar = $_post['bar']
复制代码
Now some additional thing


  • Return data in Ajax call
You can either just simply echo it, or (better still as it allows multiple values to be returned and is handled negatively by JavaScript) use JSON.
  1. $id = $_POST['bar'];
  2. //Code that would run here for whatever

  3. // 1. Without JSON
  4. echo "hello this is one"

  5. // 2. By JSON. Then here is where I want to send a value back to the success of the Ajax below
  6. echo json_encode(array('returned_val' => 'yoho'));
复制代码
And then in the success function you can get
  1. success: function(data) {
  2. alert(data.returned_val);
  3. }
复制代码
stackoverflow
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|小黑屋|BC Morning Website ( Best Deal Inc. 001 )

GMT-8, 2026-4-11 10:59 , Processed in 0.015602 second(s), 16 queries .

Supported by Weloment Group X3.5

© 2008-2026 Best Deal Online

快速回复 返回顶部 返回列表