2015-12-03 23 views
2

我想配置我的'發送消息/提交'按鈕,以便在單擊時訪問本地目錄中的php文件,並執行代碼以通過電子郵件發送相應的數據。無法訪問php腳本的聯繫表格

layout

的HTML代碼顯示如下:

<section class="contact-us"> 
    <div class="form-wrapper"> 
     <div class="contact-form"> 

      <form> 
       <fieldset> 
        <div class="control-group"> 
         <input id="Field12" name="Field12" placeholder="Full Name" required="" tabindex="1" type="text" value=""> 
        </div> 
        <div class="inline-groups"> 
         <div class="control-group"> 
          <input id="Field3" maxlength="255" name="Field3" placeholder="Email" required="" spellcheck="false" tabindex="2" type="email" value=""> 
         </div> 
         <div class="control-group"> 
          <input id="Field4" maxlength="255" name="Field4" placeholder="Phone number (optional)" tabindex="3" type="tel" value=""> 
         </div> 
        </div> 
        <div class="control-group"> 
         <textarea cols="50" id="Field5" name="Field5" placeholder="Let us know what we can do for you!" spellcheck="true" tabindex="4"></textarea> 
        </div> 
        <input id="idstamp" name="idstamp" type="hidden" value="r885sYVzp1O8HO4V4MdnZvkis7E3NZIfZ8CtHRCtCdY"> 
        <button class="button" action="send_form_email.php" type="submit">Send message</button> 
       </fieldset> 
      </form> 

的PHP代碼顯示如下:

<?php 
if(isset($_POST['email'])) { 

    // EDIT THE 2 LINES BELOW AS REQUIRED 

    $email_to = "[email protected]"; 

    $email_subject = "Greenmount Engineering | Customer Enquiry"; 





    function died($error) { 

     // your error code can go here 

     echo "We are very sorry, but there were error(s) found with the form you submitted. "; 

     echo "These errors appear below.<br /><br />"; 

     echo $error."<br /><br />"; 

     echo "Please go back and fix these errors.<br /><br />"; 

     die(); 

    } 



    // validation expected data exists 

    if(!isset($_POST['name']) || 

     !isset($_POST['email']) || 

     !isset($_POST['tel']) || 

     !isset($_POST['comments'])) { 

     died('We are sorry, but there appears to be a problem with the form you submitted.');  

    } 



    $name = $_POST['name']; // required 

    $email_from = $_POST['email']; // required 

    $telephone = $_POST['tel']; // not required 

    $comments = $_POST['comments']; // required 



    $error_message = ""; 

    $email_exp = '/^[A-Za-z0-9._%-][email protected][A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/'; 

    if(!preg_match($email_exp,$email_from)) { 

    $error_message .= 'The Email Address you entered does not appear to be valid.<br />'; 

    } 

    $string_exp = "/^[A-Za-z .'-]+$/"; 

    if(!preg_match($string_exp,$name)) { 

    $error_message .= 'The name you entered does not seem to be valid.<br />'; 

    } 


    if(strlen($comments) < 2) { 

    $error_message .= 'The Comments you entered do not appear to be valid.<br />'; 

    } 

    if(strlen($error_message) > 0) { 

    died($error_message); 

    } 

    $email_message = "Form details below.\n\n"; 



    function clean_string($string) { 

     $bad = array("content-type","bcc:","to:","cc:","href"); 

     return str_replace($bad,"",$string); 

    } 


    $email_message .= "Name: ".clean_string($name)."\n"; 

    $email_message .= "Email: ".clean_string($email_from)."\n"; 

    $email_message .= "Telephone: ".clean_string($telephone)."\n"; 

    $email_message .= "Comments: ".clean_string($comments)."\n"; 





// create email headers 

$headers = 'From: '.$email_from."\r\n". 

'Reply-To: '.$email_from."\r\n" . 

'X-Mailer: PHP/' . phpversion(); 

@mail($email_to, $email_subject, $email_message, $headers); 

?> 



<!-- include your own success html here --> 



Thank you for contacting us. We will be in touch with you very soon. 



<?php 

} 

?> 

我不知道如果這個代碼將工作100%但爲我還沒有嘗試過,但正如你所看到的,嘗試將HTML按鈕鏈接到PHP腳本只會在發送時刷新頁面,但是當我檢查我的電子郵件時, 。 <button class="button" action="send_form_email.php"

+3

'

'&&''。 – D4V1D

+0

當按鈕被點擊時,它會帶我到一個帶有php代碼的新網頁,它似乎不會執行/將此數據通過電子郵件發送到目標電子郵件?你可以檢查我的PHP並告訴我哪裏出了問題? – Revo

+0

看到下面的評論,我猜你試圖從你自己的計算機上運行,​​而沒有安裝web服務器或PHP,或者以'c:/// file.xxx'而不是'http:// localhost/file .xxx' @Revo am I right? –

回答

2

您必須更換一行代碼。

始終在表單標籤中使用動作,因此請將action="send_form_email.php"標記爲<form method="post">標記,並且您的代碼將運行。

+0

你忘了提及必須設置爲'post'的'method'屬性。它會發送表單,否則就是'get'。 – D4V1D

+0

[不,表單默認情況下使用'get'方法發送。](http://stackoverflow.com/a/2314441/2788131) – D4V1D

+0

感謝指出我將編輯。謝謝 –

2

一些調整,需要做你的代碼:

  1. 添加method="post"屬性您<form>標籤(所以使用的方法是不get)。

  2. action="send_form_email.php"屬性添加到您的<form>標記(以便您的表單重定向到您的腳本)。

  3. 添加name="submit"<button>標籤(所以if(isset($_POST["submit"]))send_form_email.php計算結果爲true和代碼塊被執行的表達式)。

  4. 更改name<input>的屬性根據您的$_POST['...'](因此PHP識別輸入值)。

你應該很好去。

+0

name =「submit」或type =「submit」??此外,在:發送 – Revo

+0

&&顯示爲文本,而不是實際功能代碼 – Revo

+0

您的'