2013-09-20 26 views
0

我有一個聯繫表格,它只發送與PHP ATM,我想發送它與AJAX? 最簡單的方法是什麼?如何使這個PHP腳本發送在AJAX而不是

  <?php 
      //If the form is submitted 
      if(isset($_POST['submit'])) { 

       //Check to make sure that the name field is not empty 
       if(trim($_POST['contactname']) == '') { 
        $hasError = true; 
       } else { 
        $name = trim($_POST['contactname']); 
       } 




       //Check to make sure that the subject field is not empty 
       if(trim($_POST['subject']) == '') { 
        $hasError = true; 
       } else { 
        $subject = trim($_POST['subject']); 
       } 

       //Check to make sure sure that a valid email address is submitted 
       if(trim($_POST['email']) == '') { 
        $hasError = true; 
       } else if (!eregi("^[A-Z0-9._%-][email protected][A-Z0-9._%-]+\.[A-Z]{2,4}$", trim($_POST['email']))) { 
        $hasError = true; 
       } else { 
        $email = trim($_POST['email']); 
       } 

       //Check to make sure comments were entered 
       if(trim($_POST['message']) == '') { 
        $hasError = true; 
       } else { 
        if(function_exists('stripslashes')) { 
         $comments = stripslashes(trim($_POST['message'])); 
        } else { 
         $comments = trim($_POST['message']); 
        } 
       } 

       //If there is no error, send the email 
       if(!$hasError) { 
        $emailTo = '[email protected]'; // Put your own email address here 
        $body = "Name: $name \n\nEmail: $email \n\nSubject: $subject \n\nComments:\n $comments"; 
        $headers = 'From: My Site <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email; 

        if(wp_mail($emailTo, $subject, $body, $headers)) { 
         $emailSent = true; 
        }else{ 
         echo '<p class="alert-message error">Error sending mail.</p>'; 
        } 
       } 
      } 
      ?> 

請也許指點也提高發送功能。

任何幫助真的很感激。

,如果你需要看的形式讓我知道,我將編輯並把它放在

+0

你試過的是什麼? – qooplmao

+0

給我幾分鐘來輸入答案 – gibberish

回答

0

你可以修改你的代碼如下。

以下是您要求的獨立示例(未經測試)。

只需複製兩個代碼塊/粘貼到兩個文件:

contacttest.php(或任何你想將它命名)
yourphpprocessor.php(如果改名,也必須改變在AJAX代碼塊)

注意的是:
1.各形式元素現在具有ID ATTR
2. <form> FUNC族體不再使用在所有,並通過e.preventDefault()


HTML其實防止contacttest.php

<html> 
<head> 
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> 
    <script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.9.1/jquery-ui.min.js"></script> 
    <link rel="stylesheet" href="http://code.jquery.com/ui/1.9.1/themes/base/jquery-ui.css" /> 

     <style> 
     </style> 

     <script type="text/javascript"> 
      $(document).ready(function() { 

       //If you want the output from php side in a lightbox... 
       $('#alertmsg').dialog({ 
        autoOpen: false, 
        modal: true, 
        width: 400, 
        buttons: { 
         Thanks: function() { 
          $(this).dialog('close'); 
         } 
        } 
       }); 

       $('#contactform').submit(function(e) { 
        e.preventDefault(); 
        var frm = $('#cname').val(); 
        var eml = $('#cemail').val(); 
        var sub = $('#subj').val(); 
        var msg = $('#msg').val(); 

        //validation goes here, for eg. 
        if (frm == '' || eml==''){ 
         alert('All fields must be completed'); 
         return false; 
        } 

        $.ajax({ 
         type: "POST", 
         url: "yourphpprocessor.php", 
         data: 'f=' +frm+ '&e=' +eml+ '&s=' +sub+ '&m=' +msg, 
         success: function(recd) { 
          $('#alertmsg').html(recd); 
          $('#alertmsg').dialog('open'); //Uses lightbox to display message 
         } 
        }); 
       }); //END AJAX 


      }); //END $(document).ready() 

     </script> 
    </head> 
<body> 

    <form id="contactform" action="" method="POST"> 
     From: <input type="text" name="contactname" id="cname"><br /> 
     Email: <input type="text" name="email" id="cemail"><br /> 
     Subject: <input type="text" name="subject" id="subj"><br /> 
     Message: <textarea name="message" id="msg"></textarea><br /><br /> 
     <input type="submit" id="mysubmit" value="Submit"> 
    </form> 

    <div id="alertmsg"></div> 

</body> 
</html> 

PHP端:yourphpprocessor.php

<?php 
    $name = $_POST['f']; 
    $email = $_POST['e']; 
    $subject = $_POST['s']; 
    $comments = $_POST['m']; 

    $emailTo = '[email protected]'; // Put your own email address here 
    $body = "Name: $name \n\nEmail: $email \n\nSubject: $subject \n\nComments:\n $comments"; 
    $headers = 'From: My Site <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email; 

    if(wp_mail($emailTo, $subject, $body, $headers)) { 
     $emailSent = true; 
    }else{ 
     echo '<p class="alert-message error">Error sending mail.</p>'; 
    } 
?> 
+0

非常感謝你如何讓錯誤信息顯示在像之前的p標籤中? – user1415303

+0

嗨胡言亂語你有Skype我似乎無法得到它的工作? – user1415303

相關問題