点击弹出层ajax php jquery 发送表单

2014-11-24 03:17:02 · 作者: · 浏览: 0

点击弹出层ajax php jquery 发送表单

点击弹出层ajax php jquery 发送表单演示

java script Code
  1. <script type="text/java script">
  2. var messageDelay = 2000; // How long to display status messages (in milliseconds)
  3. // Init the form once the document is ready
  4. $( init );
  5. // Initialize the form
  6. function init() {
  7. // Hide the form initially.
  8. // Make submitForm() the form's submit handler.
  9. // Position the form so it sits in the centre of the browser window.
  10. $('#contactForm').hide().submit( submitForm ).addClass( 'positioned' );
  11. // When the "Send us an email" link is clicked:
  12. // 1. Fade the content out
  13. // 2. Display the form
  14. // 3. Move focus to the first field
  15. // 4. Prevent the link being followed
  16. $('a[href="#contactForm"]').click( function() {
  17. $('#content').fadeTo( 'slow', .2 );
  18. $('#contactForm').fadeIn( 'slow', function() {
  19. $('#senderName').focus();
  20. } )
  21. return false;
  22. } );
  23. // When the "Cancel" button is clicked, close the form
  24. $('#cancel').click( function() {
  25. $('#contactForm').fadeOut();
  26. $('#content').fadeTo( 'slow', 1 );
  27. } );
  28. // When the "Escape" key is pressed, close the form
  29. $('#contactForm').keydown( function( event ) {
  30. if ( event.which == 27 ) {
  31. $('#contactForm').fadeOut();
  32. $('#content').fadeTo( 'slow', 1 );
  33. }
  34. } );
  35. }
  36. // Submit the form via Ajax
  37. function submitForm() {
  38. var contactForm = $(this);
  39. // Are all the fields filled in
  40. if ( !$('#senderName').val() || !$('#senderEmail').val() || !$('#message').val() ) {
  41. // No; display a warning message and return to the form
  42. $('#incompleteMessage').fadeIn().delay(messageDelay).fadeOut();
  43. contactForm.fadeOut().delay(messageDelay).fadeIn();
  44. } else {
  45. // Yes; submit the form to the PHP script via Ajax
  46. $('#sendingMessage').fadeIn();
  47. contactForm.fadeOut();
  48. $.ajax( {
  49. url: contactForm.attr( 'action' ) + " ajax=true",
  50. type: contactForm.attr( 'method' ),
  51. data: contactForm.serialize(),
  52. success: submitFinished
  53. } );
  54. }
  55. // Prevent the default form submission occurring
  56. return false;
  57. }
  58. // Handle the Ajax response
  59. function submitFinished( response ) {
  60. response = $.trim( response );
  61. $('#sendingMessage').fadeOut();
  62. if ( response == "success" ) {
  63. // Form submitted successfully:
  64. // 1. Display the success message
  65. // 2. Clear the form fields
  66. // 3. Fade the content back in
  67. $('#successMessage').fadeIn().delay(messageDelay).fadeOut();
  68. $('#senderName').val( "" );
  69. $('#senderEmail').val( "" );
  70. $('#message').val( "" );
  71. $('#content').delay(messageDelay+500).fadeTo( 'slow', 1 );
  72. } else {
  73. // Form submission failed: Display the failure message,
  74. // then redisplay the form
  75. $('#failureMessage').fadeIn().delay(messageDelay).fadeOut();
  76. $('#contactForm').delay(messageDelay+500).fadeIn();
  77. }
  78. }
  79. XML/HTML Code
    1. 点击这里测试