2013-05-06 70 views
-1

我想爲我的網站做一個聯繫表格,但當我按下提交時,PHP文件被下載,而不是被運行。 我使用鉻但不認爲這應該重要 我認爲有一個語法錯誤,但我已經搞砸了刪除,添加和東西,即使沒有語法錯誤,它仍然下載文件,而不是運行它 而且,是......是php文件的確切名稱(SendEmail.php)提交按鈕正在下載的PHP而不是運行它

HTML

<form name="contactform" method="post" action="SendEmail.php"> 
<div class="ContactHeaders">Name:</div> 
<input type="text" name="Name" class="ContactBoxes"/> 
<div class="ContactHeaders">Email:</div> 
<input type="text" name="Email" class="ContactBoxes"/> 
<div class="ContactHeaders">Message:</div> 
    <div style="width:100%"> 
    <textarea name="Message" maxlength="1000"></textarea> 
    </div> 
<div style="width: 100%"> 
<input type="submit" class="Submitbtn" value="Submit"> 
</div> 
</form> 

PHP

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


    $email_to = "[email protected]"; 
    $email_subject = "Website Contact"; 


    function died($error) { 

     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['Message'])) { 
     died('Sorry, but there appears to be a problem with the form you submitted.');  
    } 

    $first_name = $_POST['Name']; // required 
    $last_name = $_POST['Email']; // required 
    $email_from = $_POST['Message']; // 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)) { 
    $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 First Name you entered does not appear to be valid.<br />'; 
    } 

    if(strlen($Message) < 10) { 
    $error_message .= 'The message you entered does not appear to be valid.<br />'; 
    } 
    if(strlen($error_message) > 0) { 
    died($error_message); 
    } 

    function clean_string($string) { 
     $bad = array("content-type","bcc:","to:","cc:","href"); 
     return str_replace($bad,"",$string); 
    } 


    $Name .= clean_string($Name)."\n"; 
    $Email .= clean_string($Email)."\n"; 
    $Message .= clean_string($Message)."\n"; 


//email headers 
$headers = 'From: '.$Email."\r\n". 
'Reply-To: '.$Email."\r\n" . 
'X-Mailer: PHP/' . phpversion(); 
@mail($email_to, $email_subject, $Message, $headers); 
?> 

Thank you for contacting me. I will be in touch with you very soon. 

<?php 
} 
?> 

我無法找到問題所在!

+2

你已經安裝了Apache,PHP?嘗試運行'<?php phpinfo(); ?>'看看是否有實際安裝的PHP – 2013-05-06 12:24:44

+0

U正在使用WAMP或XAMPP或者已經安裝了APACHE,PHP分開? – 2013-05-06 12:25:13

+0

你有一個開放的PHP標籤之前,你的'如果(isset ...'? – smerny 2013-05-06 12:27:03

回答

2

如果正在下載實際的php soure代碼,您的網絡服務器上有一些配置問題,但我不會在這裏進入。

我的確建議您刪除@ infront的郵件命令,因爲這樣可以避免您可能遇到的錯誤。

其中一個錯誤,即。你壓抑的事實是,你不'有一個$消息變量,考慮到你的代碼說:$email_from = $_POST['Message']; // required

除此之外:我建議你閱讀關於大寫/小寫字符命名約定。它使調試代碼更容易一些。首先嚐試http://framework.zend.com/manual/1.12/en/coding-standard.naming-conventions.html

PS。你確實有一個$ Message變量,但它是空的。

+0

我已經整理過了,謝謝:) – 2013-05-06 13:05:07

+1

不客氣:)。如果答案回答了您的問題,您應該點擊旁邊的複選標記以接受答案。 – 2013-05-08 06:38:41

0

我面臨同樣的問題。我在WAMP服務器上試圖這樣做。當我輸入URL:localhost/login.html並註冊時,它工作正常。問題出現在我通過編輯器打開它時。新的網址是localhost:66342/login.html

所以我只需鍵入原始URL而不是這個新的,它的工作。

相關問題