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

前端之家收集整理的这篇文章主要介绍了点击弹出层ajax php jquery 发送表单前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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


JavaScript Code
  1. <scripttype="text/javascript">
  2. varmessageDelay=2000;//Howlongtodisplaystatusmessages(inmilliseconds)
  3. //Inittheformoncethedocumentisready
  4. $(init);
  5. //Initializetheform
  6. functioninit(){
  7. //Hidetheforminitially.
  8. //MakesubmitForm()theform'ssubmithandler.
  9. //Positiontheformsoitsitsinthecentreofthebrowserwindow.
  10. $('#contactForm').hide().submit(submitForm).addClass('positioned');
  11. //Whenthe"Sendusanemail"linkisclicked:
  12. //1.Fadethecontentout
  13. //2.Displaytheform
  14. //3.Movefocustothefirstfield
  15. //4.Preventthelinkbeingfollowed
  16. $('a[href="#contactForm"]').click(function(){
  17. $('#content').fadeTo('slow',.2);
  18. $('#contactForm').fadeIn('slow',function(){
  19. $('#senderName').focus();
  20. })
  21. returnfalse;
  22. });
  23. //Whenthe"Cancel"buttonisclicked,closetheform
  24. $('#cancel').click(function(){
  25. $('#contactForm').fadeOut();
  26. //Whenthe"Escape"keyispressed,0);">$('#contactForm').keydown(function(event){
  27. if(event.which==27){
  28. $('#contactForm').fadeOut();
  29. }
  30. }
  31. //SubmittheformviaAjax
  32. functionsubmitForm(){
  33. varcontactForm=$(this);
  34. //Areallthefieldsfilledin?
  35. if(!$('#senderName').val()||!$('#senderEmail').val()||!$('#message').val()){
  36. //No;displayawarningmessageandreturntotheform
  37. $('#incompleteMessage').fadeIn().delay(messageDelay).fadeOut();
  38. contactForm.fadeOut().delay(messageDelay).fadeIn();
  39. }else{
  40. //Yes;submittheformtothePHPscriptviaAjax
  41. $('#sendingMessage').fadeIn();
  42. contactForm.fadeOut();
  43. $.ajax({
  44. url:contactForm.attr('action')+"?ajax=true",
  45. type:contactForm.attr('method'),
  46. data:contactForm.serialize(),0);">success:submitFinished
  47. //Preventthedefaultformsubmissionoccurring
  48. returnfalse;
  49. //HandletheAjaxresponse
  50. functionsubmitFinished(response){
  51. response=$.trim(response);
  52. $('#sendingMessage').fadeOut();
  53. if(response=="success"){
  54. //Formsubmittedsuccessfully:
  55. //1.Displaythesuccessmessage
  56. //2.Cleartheformfields
  57. //3.Fadethecontentbackin
  58. $('#successMessage').fadeIn().delay(messageDelay).fadeOut();
  59. $('#senderName').val("");
  60. $('#senderEmail').val("");
  61. $('#message').val("");
  62. $('#content').delay(messageDelay+500).fadeTo('slow',0);">//FormsubmissionFailed:Displaythefailuremessage,0);">//thenredisplaytheform
  63. $('#failureMessage').fadeIn().delay(messageDelay).fadeOut();
  64. $('#contactForm').delay(messageDelay+500).fadeIn();
  65. }
  66. }
  67. </script>
@H_603_502@ XML/HTML Code
    <divid="content">
  1. pstyle="padding-bottom:50px;font-weight:bold;text-align:center;"><ahref="#contactForm">点击这里测试</a></p>
  2. divid="click_area">
  3. </div>
  4. div>
  5. formid="contactForm"action="processForm.PHP"method="post">
  6. h2>发送表单</h2>
  7. ul>
  8. li>
  9. labelfor="senderName">YourName</label>
  10. inputtype="text"name="senderName"id="senderName"placeholder="Pleasetypeyourname"required="required"maxlength="40"/>
  11. li>
  12. labelfor="senderEmail">YourEmailAddress</label>
  13. inputtype="email"name="senderEmail"id="senderEmail"placeholder="Pleasetypeyouremailaddress"required="required"maxlength="50"/>
  14. labelfor="message"style="padding-top:.5em;">YourMessage</textareaname="message"id="message"placeholder="Pleasetypeyourmessage"required="required"cols="80"rows="10"maxlength="10000"></textarea>
  15. ul>
  16. divid="formButtons">
  17. inputtype="submit"id="sendMessage"name="sendMessage"value="SendEmail"/>
  18. inputtype="button"id="cancel"name="cancel"value="Cancel"/>
  19. form>
  20. divid="sendingMessage"class="statusMessage"><p>Sendingyourmessage.Pleasewait...</p></divid="successMessage"class="statusMessage"><p>Thanksforsendingyourmessage!We'llgetbacktoyoushortly.</divid="failureMessage"class="statusMessage"><p>Therewasaproblemsendingyourmessage.Pleasetryagain.</divid="incompleteMessage"class="statusMessage"><p>Pleasecompleteallthefieldsintheformbeforesending.</div>

processForm.PHP

@H_603_502@ PHP Code
    <?PHP
  1. //Definesomeconstants
  2. define("RECIPIENT_NAME","text");
  3. define("RECIPIENT_EMAIL","john@example.com");
  4. define("EMAIL_SUBJECT","freejs.net");
  5. //Readtheformvalues
  6. $success=false;
  7. $senderName=isset($_POST['senderName'])?preg_replace("/[^\.\-\'a-zA-Z0-9]/","",$_POST['senderName']):"";
  8. $senderEmail=isset($_POST['senderEmail'])?preg_replace("/[^\.\-\_\@a-zA-Z0-9]/","",$_POST['senderEmail']):"";
  9. $message=isset($_POST['message'])?preg_replace("/(From:|To:|BCC:|CC:|Subject:|Content-Type:)/",$_POST['message']):"";
  10. //Ifallvaluesexist,sendtheemail
  11. if($senderName&&$senderEmail&&$message){
  12. $recipient=RECIPIENT_NAME."<".RECIPIENT_EMAIL.">";
  13. $headers="From:".$senderName."<".$senderEmail.">";
  14. $success=mail($recipient,EMAIL_SUBJECT,$message,$headers);
  15. //Returnanappropriateresponsetothebrowser
  16. if(isset($_GET["ajax"])){
  17. echo$success?"success":"error";
  18. }else{
  19. ?>
  20. <html>
  21. <head>
  22. <title>Thanks!</title>
  23. </head>
  24. <body>
  25. <?PHPif($success)echo"<p>Thanksforsendingyourmessage!We'llgetbacktoyoushortly.</p>"?>
  26. <?PHPif(!$success)echo"<p>Therewasaproblemsendingyourmessage.Pleasetryagain.</p>"?>
  27. <p>Clickyourbrowser'sBackbuttontoreturntothepage.</p>
  28. </body>
  29. </html>
  30. <?PHP
  31. ?>


原文地址: http://www.freejs.net/article_biaodan_127.html

猜你在找的Ajax相关文章