.PHP表单提交无效

时间:2015-08-14 00:00:34

标签: php html forms

我希望人们能够通过我在网站上的表单提交他们的联系信息。我不知道如何自己编写PHP代码,所以我抓住了一个"工作模型"并进行了一些编辑以适应我的网站。这是HTML代码:

<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 = "someone@example.com";

    $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._%-]+@[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 个答案:

答案 0 :(得分: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>

答案 1 :(得分:2)

我不完全确定,但我认为提交按钮需要在标签内。

这样它就知道它要提交的形式。