联系表单PHP脚本不加载?

时间:2015-04-27 12:23:45

标签: php html contact-form

所以我正在尝试使用html和php脚本构建联系表单。我在过去做过这个(有模板),一切看起来都不错,但我尝试自己创建一个,并且由于某些原因,在html页面上提交时没有加载脚本(表单动作等都是正确的)我猜它必须与php有关,请参阅下面的代码。任何帮助都会很棒:

   <?php

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



    // EDIT THE 2 LINES BELOW AS REQUIRED

    $email_to = "text@email.co.uk";

    $email_subject = "Test form";





    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['teamname']) ||

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

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

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

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

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

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

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

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

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

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

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

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

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

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

 {

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

    }



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

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

    $secondname = $_POST['secondname']; // not required

    $address = $_POST['address']; // not required

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

    $your-email = $_POST['youremail']; // required

    $phone = $_POST['phone']; // not required


    $error_message = "";

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

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

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

  }

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

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

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

  }

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

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

  }

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

    $error_message .= 'The Post Code you entered does 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($teamname)."\n";

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

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

    $email_message .= "Address: ".clean_string($address)."\n";

    $email_message .= "Postcode: ".clean_string($postcode)."\n";

    $email_message .= "email: ".clean_string($youremail)."\n";

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

    $email_message .= "other player 1: ".clean_string($otherplayername1)."\n";

    $email_message .= "other player 2: ".clean_string($otherplayername2)."\n";

    $email_message .= "other player 3: ".clean_string($otherplayername3)."\n";

    $email_message .= "other player 4: ".clean_string($otherplayername4)."\n";

    $email_message .= "other player 5: ".clean_string($otherplayername5)."\n";

    $email_message .= "other player 6: ".clean_string($otherplayername6)."\n";

    $email_message .= "other player 7: ".clean_string($otherplayername7)."\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.
You can choose to play either on the day or through Paypal (please bring your Paypal reference with you on the match day)



<?php

}

?>

编辑:更新了Php并添加了html

这也是我的html表单:

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

<input type="text" name="teamname" value="" size="40" placeholder="Team Name"/><br>
<input type="text" name="yourname" value="" size="40" placeholder="Your Name"/><br>
<input type="text" name="secondname" value="" size="40" placeholder="Last Name"/><br>
<input type="text" name="address" value="" size="40" placeholder="Address"/><br>
<input type="text" name="postcode" value="" size="40" placeholder="Postcode"/><br>
<input type="email" name="youremail" value="" size="40" placeholder="Email"/><br>
<input type="tel" name="phone" value="" size="40" placeholder="Phone Number"/><br>
</div>

<input type="text" name="otherplayername1" value="" size="40" placeholder="Other Players Name 1"/><br>
<input type="text" name="otherplayername2" value="" size="40" placeholder="Other Players Name 2"/><br>
<input type="text" name="otherplayername3" value="" size="40" placeholder="Other Players Name 3"/><br>
<input type="text" name="otherplayername4" value="" size="40" placeholder="Other Players Name 4"/><br>
<input type="text" name="otherplayername5" value="" size="40" placeholder="Other Players Name 5"/><br>
<input type="text" name="otherplayername6" value="" size="40" placeholder="Other Players Name 6"/><br>
<input type="text" name="otherplayername7" value="" size="40" placeholder="Other Players Name 7"/><br>
<input type="text" name="location" value="" size="40"  placeholder="Location"/></span><br><br>
<input type="checkbox" /> <a target="_blank" style="color: #fff;">I confirm that I accept the Cash Leagues Rules and Regulations.</a>
<input type="submit" value="Send" action="contact.php"/></form>

2 个答案:

答案 0 :(得分:1)

$team-name = $_POST['team-name']; // required
$your-name = $_POST['your-name']; // required
$second-name = $_POST['second-name']; // not required
$your-email = $_POST['your-email']; // required

不要使用&#34; - &#34;在var-names中:

$team-name -> $teamname
$your-name -> $yourname
...

启用error_reporting,当你开发看到所有错误的php给你:)(在这种情况下:解析错误)

第二个问题: 你检查$_POST['email'],如果设置了,如果没有,你什么都不做。但是在您的表单中,没有带有此名称的输入字段,因此您的脚本永远不会执行任何操作。

答案 1 :(得分:0)

变量名称中不允许使用字符-。 尝试替换像

这样的行
$team-name = $_POST['team-name']; // required

$team_name = $_POST['team-name']; // required

在PHP中,有效的变量名称以字母或下划线开头,后跟任意数量的字母,数字或下划线。不允许-个字符。这是因为否则数学运算-会有歧义。

您可以在official documentation page中找到详细说明。

作为一般提示,无论如何,我强烈建议您仔细阅读PHP生成的错误消息(并在请求StackOverflow帮助时将其与源代码一起报告):PHP,几乎与所有编程语言一样在那里,在失败的情况下提供相当丰富的信息。如果您阅读了错误消息,则很可能会告诉您 问题是什么,哪里(通常是行号)。例如,典型的PHP错误消息可能是

Fatal error: Call to undefined function my_function()
in /home/mysite/public_html/test.php on line 2 

Call to undefined function my_function()是问题,/home/mysite/public_html/test.phpline 2为您提供有关问题排名的信息。

如果您的脚本没有提供足够的信息,请参阅手册中的Error Reporting页面:它包含有关如何改进错误消息详细信息的宝贵信息。