2016-11-12 112 views
0

提交表單時,我得到一個404頁面。有些東西缺失或錯誤。 這是一個wordpress網站。我在wordpress的選項中插入了電子郵件,沒有發生任何事情。我看到了很多堆棧溢出的問題,但沒有人幫助我。我不想使用插件。聯繫表單不起作用

<form class="form-horizontal" role="form" method="post" action="contact.php"> 

      <div class="input-group"> 
       <span class="input-group-addon" id="basic-addon1"><i class="glyphicon glyphicon-user"></i></span> 
       <input type="text" class="form-control" id="name" name="name" placeholder="Name" value=""> 
      </div> 
     <br/>  
      <div class="input-group"> 
       <span class="input-group-addon" id="basic-addon1"><i class="glyphicon glyphicon-envelope"></i></span> 
       <input type="email" class="form-control" id="email" name="email" placeholder="[email protected]" value=""> 
      </div> 
     <br/>  
      <div class="input-group"> 
       <span class="input-group-addon" id="basic-addon1"><i class="glyphicon glyphicon-pencil"></i></span> 
       <textarea class="form-control" rows="4" name="message" placeholder="Message..."></textarea> 
      </div> 
     <br/>  
      <div class="input-group"> 
       <span class="input-group-addon" id="basic-addon1">2 + 3 = ?</span> 
       <input type="text" class="form-control" id="human" name="human" placeholder=""> 
      </div> 
    <br/> 
    <div class="form-group"> 
     <div class="col-sm-10 col-sm-offset-5"> 
      <input id="submit" name="submit" type="submit" value="Enviar" class="btn btn-button"> 
     </div> 
    </div> 
    <div class="form-group"> 
     <div class="col-sm-10 col-sm-offset-2"> 
      <! Will be used to display an alert to the user> 
     </div> 
    </div> 
</form> 

PHP代碼:

<?php 
    if (isset($_POST["submit"])) { 
     $name = $_POST['name']; 
     $email = $_POST['email']; 
     $message = $_POST['message']; 
     $human = intval($_POST['human']); 
     $from = 'Contact Form'; 
     $to = '[email protected]'; 
     $subject = 'Contact '; 

     $body = "From: $name\n E-Mail: $email\n Message:\n $message"; 

     // Check if name has been entered 
     if (!$_POST['name']) { 
      $errName = 'Please enter your name'; 
     } 

     // Check if email has been entered and is valid 
     if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) { 
      $errEmail = 'Please enter a valid email address'; 
     } 

     //Check if message has been entered 
     if (!$_POST['message']) { 
      $errMessage = 'Please enter your message'; 
     } 
     //Check if simple anti-bot test is correct 
     if ($human !== 5) { 
      $errHuman = 'Your anti-spam is incorrect'; 
     } 

// If there are no errors, send the email 
if (!$errName && !$errEmail && !$errMessage && !$errHuman) { 
    if (mail ($to, $subject, $body, $from)) { 
     $result='<div class="alert alert-success">Thank You! I will be in touch</div>'; 
    } else { 
     $result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later</div>'; 
    } 
} 
    } 
?> 
+1

然後'contact.php'不存在 – RiggsFolly

+0

是的存在,它在同一個文件夾中。但我猜這個表格不能取他。你有什麼想法,爲什麼? – APDB

+0

是正確的嗎? * NIX系統上的'contact.php'!= Contact.php' – RiggsFolly

回答

0

首先感謝您的回覆。

這裏的問題是PHP郵件()。我希望我能幫助這個線程的人。

得出這一結論這些是步驟:

  • 我在堆棧溢出看到了同樣的問題,嘗試一切有我;
  • 安裝Check Email Plugin;
  • 我沒有收到任何電子郵件,所以我聯繫我的託管服務,他們說他們不會啓用PHP郵件()來避免垃圾郵件。

Solution - 發生這種情況時,這是解決問題的最佳方法。

0

創建爲主題的PHP文件(您當前的主題,如果有兒童主題然後在thild主題),然後把下面的代碼文件中,並將其保存。

<?php 
/* 
* Template Name: Contact us 
*/ 
get_header(); 
if (isset($_POST["submit"])) 
{ 
    $name = $_POST['name']; 
    $email = $_POST['email']; 
    $message = $_POST['message']; 
    $human = intval($_POST['human']); 
    $from = 'Contact Form'; 
    $to = '[email protected]'; 
    $subject = 'Contact '; 
    $body = "From: $name\n E-Mail: $email\n Message:\n $message"; 
    // Check if name has been entered 
    if (!$_POST['name']) 
    { 
     $errName = 'Please enter your name'; 
    } 
    // Check if email has been entered and is valid 
    if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) 
    { 
     $errEmail = 'Please enter a valid email address'; 
    } 
    //Check if message has been entered 
    if (!$_POST['message']) 
    { 
     $errMessage = 'Please enter your message'; 
    } 
    //Check if simple anti-bot test is correct 
    if ($human !== 5) 
    { 
     $errHuman = 'Your anti-spam is incorrect'; 
    } 
    // If there are no errors, send the email 
    if (!$errName && !$errEmail && !$errMessage && !$errHuman) 
    { 
     if (mail ($to, $subject, $body, $from)) 
     { 
      $result='<div class="alert alert-success">Thank You! I will be in touch</div>'; 
     } 
     else 
     { 
      $result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later</div>'; 
     } 
    } 
} 
?> 
<div id="primary" class="content-area"> 
    <main id="main" class="site-main" role="main"> 
     <form class="form-horizontal" role="form" method="post" action="#"> 
      <div class="input-group"> 
       <span class="input-group-addon" id="basic-addon1"><i class="glyphicon glyphicon-user"></i></span> 
       <input type="text" class="form-control" id="name" name="name" placeholder="Name" value=""> 
      </div><br/>  
      <div class="input-group"> 
       <span class="input-group-addon" id="basic-addon1"><i class="glyphicon glyphicon-envelope"></i></span> 
       <input type="email" class="form-control" id="email" name="email" placeholder="[email protected]" value=""> 
      </div><br/>  
      <div class="input-group"> 
       <span class="input-group-addon" id="basic-addon1"><i class="glyphicon glyphicon-pencil"></i></span> 
       <textarea class="form-control" rows="4" name="message" placeholder="Message..."></textarea> 
      </div><br/>  
      <div class="input-group"> 
       <span class="input-group-addon" id="basic-addon1">2 + 3 = ?</span> 
       <input type="text" class="form-control" id="human" name="human" placeholder=""> 
      </div><br/> 
      <div class="form-group"> 
       <div class="col-sm-10 col-sm-offset-5"> 
        <input id="submit" name="submit" type="submit" value="Enviar" class="btn btn-button"> 
       </div> 
      </div> 
      <div class="form-group"> 
       <div class="col-sm-10 col-sm-offset-2"> 
        <!-- Will be used to display an alert to the user --> 
       </div> 
      </div> 
     </form> 
    </main> 
    <?php get_sidebar('content-bottom'); ?> 
</div> 
<?php get_footer(); ?> 

現在去可溼性粉劑管理員,並創建新的頁面(標題可能是接觸頁等),選擇頁面模板「聯繫我們」,從頁面模板下拉(頁面內容的右側),發佈網頁並查看該頁面。

希望它會有所幫助。

+0

@APDP你試試嗎? –

+0

嗨,對不起謝謝你的回覆,這是行不通的,這種錯誤從來沒有發生過,始終是404頁面 – APDB

+0

什麼是你的固定鏈接設置?也可以嘗試從上面的代碼中刪除action =「#」。 –

-1

在操作使用文件位置網址:

action="`<?php echo get_template_directory()'contact.php';?>`" 

試試這個。

+0

不,如果你閱讀我上面的評論,你可以看到我嘗試過。解析錯誤:語法錯誤。沒有PHP,給了我一個404。謝謝你的回覆。 – APDB

+0

試試這個https://www.inkthemes.com/how-you-can-easily-create-customized-form-in-wordpress/ – Kenil

+0

再次感謝你的回覆,但再次404。如果你閱讀評論,你可以看到有些人即使使用該代碼也有同樣的問題。 – APDB