2014-04-05 67 views
0

所以我使用PHP在WordPress中創建了一個自定義聯繫表單。表單發送,我正在接收電子郵件。我遇到的問題是,一旦您點擊提交,就會進入帖子頁面,並且不會保留在原始頁面上。點擊提交後,我如何留在wordpress的同一頁面?

我試過使用會話和標題位置(沒有工作) 我也試過把這個在我的行動「<?php echo $_SERVER['PHP_SELF']; ?>」,也不起作用。 (郵件只是不發送它,併發送給我的404頁面。

所以我有點卡住了,以解決這個問題。通常我會沒有問題,如果這是一個靜態網頁,但因爲我「M使用WordPress,這個任務似乎更加麻煩

下面是該網站http://www.indianpointresort.ca/

下面的鏈接是PHP驗證:

<?php 

    /*session_start(); 
    if(!isset($_SESSION['afaisfjisjfijfjiwaefjawsefijef'])){ 
     $url = 'http://www.indianpointresort.ca/'; 
     header("Location:home.php?url=$url"); 
    }*/ 

    $name = trim($_POST['name']); 
    $email = trim($_POST['email']); 
    $phone = trim($_POST['phone']); 
    $subject = trim($_POST['subject']); 
    $message = trim($_POST['message']); 

    echo "$name | $email | $phone | $subject | $message"; 


    if(isset($_POST['submit'])){ 

     $boolValidationOK = 1; 
     $strValidationMessage = ""; 

     //validate first name 
     //validate last name 
     if(strlen($name)<3){ 
      $boolValidationOK = 0; 
      $strValidationMessage .= "Please fill in a proper first and last name </br>"; 
     } 
     //email validation: 
     $emailValidate = validate_email($email);// calls the function below to validate the email addy 
     if(!$emailValidate){ 
      $boolValidationOK = 0; 
      $strValidationMessage .= "Please fill in proper email address </br>"; 
     } 
     //validate phone 
     $phone = checkPhoneNumber($phone); 
     if(!$phone){ 
      $boolValidationOK = 0; 
      $strValidationMessage .= "Please fill proper phone number </br>"; 
     } 
     //validate subject 
     if(strlen($subject)<3){ 
      $boolValidationOK = 0; 
      $strValidationMessage .= "Please fill in a proper subject description </br>"; 
     } 
     //validate description 
     if(strlen($message)<3){ 
      $boolValidationOK = 0; 
      $strValidationMessage .= "Please fill in a proper message </br>"; 
     } 
     if($boolValidationOK == 1){ 
      //$strValidationMessage = "SUCCESS"; 

      //MAIL SECURITY !!!!!!! 


    // WE MUST VALIDATE AGAINST EMAIL INJECTIONS; THE SPAMMERS BEST WEAPON 
    $badStrings = array("Content-Type:", 
    "MIME-Version:", 
    "Content-Transfer-Encoding:", 
    "bcc:", 
    "cc:"); 
    foreach($_POST as $k => $v){// change to $_POST if your form was method="post" 
     foreach($badStrings as $v2){ 
      if(strpos($v, $v2) !== false){ 
       // In case of spam, all actions taken here 
       //header("HTTP/1.0 403 Forbidden"); 
       echo "<script>document.location =\"http://www.bermuda-triangle.org/\" </script>"; 
       exit; // stop all further PHP scripting, so mail will not be sent. 
      } 
     } 
    } 


    $ip = $_SERVER['REMOTE_ADDR']; 
    //echo $ip; 


    /* Spammer List: IP's that have spammed you before ***********/ 
      $spams = array (
      "static.16.86.46.78.clients.your-server.de", 
      "87.101.244.8", 
      "144.229.34.5", 
      "89.248.168.70", 
      "reserve.cableplus.com.cn", 
      "94.102.60.182", 
      "194.8.75.145", 
      "194.8.75.50", 
      "194.8.75.62", 
      "194.170.32.252" 
      //"S0106004005289027.ed.shawcable.net" Phil's IP as test 

     ); // array of evil spammers 

     foreach ($spams as $site) {// Redirect known spammers 
      $pattern = "/$site/i"; 
      if (preg_match ($pattern, $ip)) { 
       // whatever you want to do for the spammer 
       echo "logging spam activity.."; 

       exit(); 
      } 
     } 
     $to = ""; 
     //$subject = " Indian Point"; 
     // compose headers 
     $headers = "From: Indian Point Resort.\r\n"; 
     $headers .= "Reply-To: $email\r\n"; 
     $headers .= "X-Mailer: PHP/".phpversion(); 

     $message = wordwrap($message, 70); 

     // send email 
     mail($to, $subject, $message, $headers); 
      } 
    }//end of submit 

    //validate phone number 
    function checkPhoneNumber($number){ 
     $number = str_replace("-", "", $number); 
     $number = str_replace(".", "", $number); 
     $number = str_replace(" ", "", $number); 
     $number = str_replace(",", "", $number); 
     $number = str_replace("(", "", $number); 
     $number = str_replace(")", "", $number); 

     if((strlen($number) != 10) || (!is_numeric($number))){ 
      return false; 
     }else{ 
      return $number; 
     } 
    } 
    //email validation 
    function validate_email($senderemail){ // this is a function; it receives info and returns a value. 
    $email = trim($senderemail); # removes whitespace 
    if(!empty($email)): 
     // validate email address syntax 
     if(preg_match('/^[a-z0-9\_\.][email protected][a-z0-9\-]+\.[a-z]+\.?[a-z]{1,4}$/i', $email, $match)): 
     return strtolower($match[0]); # valid! 
     endif; 
    endif; 
    return false; # NOT valid! 
} 
?> 

下面的形式:

<div id="msgForm" class=" msgForm five columns"> 
            <h4>Questions?</h4> 
            <h5>Send us a message!</h5> 
            <form id="contactForm" name="contactForm" method="post" action="<?php the_permalink(); ?>"> 
             <p><input type="text" name="name" value="<?php echo $name; ?>" placeholder="name*"/></p> 
             <p><input type="email" name="email" placeholder="E-mail*"/></p> 
             <p><input type="text" name="phone" placeholder="Phone #*"/></p> 
             <p><input type="text" name="subject" placeholder="subject*"/></p> 
             <p><textarea name="message" placeholder="Message*"></textarea></p> 
             <p><input type="submit" name="submit" placeholder="Submit"/></p> 
             <div class="error"> 
             <?php 
             if($strValidationMessage){ 
              echo $strValidationMessage; 
             } 
             ?> 
             </div> 
            </form> 
           </div><!--end of form--> 
+0

的唯一途徑,從提交停止形式與JavaScript,或者轉動透過按鈕進入常規按鈕(但仍然可以提交)。 – adeneo

+0

請注意,Wordpress有它自己的做Ajax請求的特殊方式。 – adeneo

回答

0

那麼,首先我會從您的信息中刪除該Gmail帳戶(爲了安全起見)。 其次我建議你使用Wordpress提供的sendmail腳本。 有一些像重力形式這樣的插件,可以讓你製作一個表單並決定所有這些選項,而不需要製作一個靜態表單,也不需要爲此提供一個新的模板文件。

只能更改到頁面的形式將如果你希望它留在同一頁上,你可以把網頁本身的行動,並在刷新後重定向(動作將決定)

上面放一個if語句像

if(isset($_POST['submit'])){ 

//驗證,sendmail和可能的錯誤這裏 } 其他{// 展現形式 }

無論如何,一個令人耳目一新的webform是標準的。這只是它如何提交的東西。你可以阻止網頁的唯一方法是通過使用jQuery或JavaScript像這樣:(給你提交一個id)

$('#submit').on("click", function(e){ 
//this prevents any submit functionality (like refresh) 
e.preventDefault(); 
//custom code to get values here and put them in the sendmail function like so: 
var message = $('$message').text(); 

} 
+0

我試圖避免使用插件。我不知道這是否會造成問題,但我的聯繫表單是頁腳,而不是實際頁面。我的表單提交,這只是我遇到問題的重定向。 – Lauren

+0

啊,是所有?在這種情況下,您可以使用表單操作。 使用表單動作調用發送郵件的頁面,但確保該頁面不會打印任何內容。 (沒有文字),因爲它會進入該頁面並顯示任何文字。 如果你確定動作頁面只有函數(比如sendmail),並且沒有打印任何東西,它應該只刷新頁面而不重定向。如果這不起作用,您可以隨時將當前網址與表單重新導回。 如果仍然無法正常工作,則必須使用jQuery,就像我在文章中所說的那樣。 – NoobishPro

+0

哦,也不,插件絕對不是必需的。如果你足夠熟練,我會建議不要使用它們,因爲它們減慢你的wordpress。 :) – NoobishPro

0

嘗試ajax表單提交。並在一個單獨的文件中添加insert query

相關問題