2015-08-14 129 views
-1

我希望人們能夠通過我在網站上的表單提交他們的聯繫信息。我不知道如何自己編寫PHP代碼,因此我採用了一種「工作模式」並對其進行了一些編輯以適合我的網站。下面是HTML代碼:.PHP表單提交不起作用

<form name="contact" method="post" action="formsubmit.php">    
<li> 

<input name="first_name" type="text" value="first name (Required)" onfocus="if(this.value == 'first name') { this.value = ''; }" onblur="if(this.value == '') { this.value = 'first name'; }" /> 

<input name="last_name" type="text" value="last name (Required)" onfocus="if(this.value == 'last name') { this.value = ''; }" onblur="if(this.value == '') { this.value = 'last name'; }" /> 

<input name="email" type="text" value="email (Required)" onfocus="if(this.value == 'email (Required)') { this.value = ''; }" onblur="if(this.value == '') { this.value = 'email (Required)'; }" /> 

<input name="telephone" type="text" value="phone" onfocus="if(this.value == 'phone (Required)') { this.value = ''; }" onblur="if(this.value == '') { this.value = 'phone'; }" /> 

<input name="comments" type="text" value="What are your goals?" onfocus="if(this.value == 'What are your goals?') { this.value = ''; }" onblur="if(this.value == '') { this.value = 'What are your goals?'; }" /> 

<div class="g-recaptcha" data-sitekey="I am using site key here, just wasn't sure if the public should be privy to that info or not"></div> 

</li> 
</form> 

<li> 
<input type="submit" value="SUBMIT" class="submitbtn" form="contact" /> 
</li> 

這裏是.PHP

<!doctype html> 
<html> 
<head> 

<!-- CSS --> 
<link href="main.css" rel="stylesheet" type="text/css" /> 

<meta charset="UTF-8"> 
<title>We Will EnduroFit</title> 
</head> 

<body> 
<?php 

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



    // EDIT THE 2 LINES BELOW AS REQUIRED 

    $email_to = "[email protected]"; 

    $email_subject = "Message from website visitor";   

    function died($error) { 

     // your error code can go here 

     echo "I am 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['first_name']) || 

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

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

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

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

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

    } 

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

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

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

    $telephone = $_POST['telephone']; // 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,$first_name)) { 

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

    } 

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

    $error_message .= 'The Last Name you entered does not appear 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 .= "First Name: ".clean_string($first_name)."\n"; 
    $email_message .= "Last Name: ".clean_string($last_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 success html here --> 

<h2>Thanks! I will respond to you soon :)</h2> 

<?php 

} 

?> 
</body> 
</html> 

如果任何人都可以看一看這一點,告訴我,我錯過了什麼。提交按鈕就像死人一樣。它什麼也沒做。我知道這是一個模型,所以我不知道我做了什麼/沒有改變打破愚蠢的提交按鈕。太感謝了!

+2

我認爲你的問題在這裏*我不知道如何自己編碼PHP *。也許你應該看看這個鏈接http://php.net/manual/en/tutorial.forms.php。 – Script47

+0

不是答案,但我建議使用輕量級PHP表單類/庫來代替。他們通常會添加文本框等。 – twentylemon

+0

__我不知道如何自己編寫PHP代碼。 –

回答

3

試試這個:我已經改變您的交易形式標記

<form name="contact" method="post" action="formsubmit.php">    
    <li> 
     <input name="first_name" type="text" value="first name (Required)" onfocus="if(this.value == 'first name') { this.value = ''; }" onblur="if(this.value == '') { this.value = 'first name'; }" /> 

     <input name="last_name" type="text" value="last name (Required)" onfocus="if(this.value == 'last name') { this.value = ''; }" onblur="if(this.value == '') { this.value = 'last name'; }" /> 

     <input name="email" type="text" value="email (Required)" onfocus="if(this.value == 'email (Required)') { this.value = ''; }" onblur="if(this.value == '') { this.value = 'email (Required)'; }" /> 

     <input name="telephone" type="text" value="phone" onfocus="if(this.value == 'phone (Required)') { this.value = ''; }" onblur="if(this.value == '') { this.value = 'phone'; }" /> 

     <input name="comments" type="text" value="What are your goals?" onfocus="if(this.value == 'What are your goals?') { this.value = ''; }" onblur="if(this.value == '') { this.value = 'What are your goals?'; }" /> 

     <div class="g-recaptcha" data-sitekey="xxxxxxxxxxxxxxxxxxxxxxxxxxx"></div> 

    </li> 

    <li> 
    <input type="submit" value="SUBMIT" class="submitbtn" form="contact" /> 
    </li> 
</form> 
+0

是的。提交按鈕需要在

標籤 – TheNytangel

+0

裏面我做出了這個舉動,但無濟於事。表單仍然不會提交:( –

+0

那麼你看到的錯誤是什麼? – aldrin27

2

的我不完全確定的位置,但我認爲,提交按鈕必須在標籤內。

這樣它就知道它意味着要提交什麼樣的表單。