联系表 PHP 脚本未加载?

Contact form PHP script doesnt load?

所以我正在尝试使用 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>
$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

不要在变量名中使用“-”:

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

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

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

字符-不能出现在变量名中。 尝试替换像

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

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

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

会出现歧义

您可以在official documentation page中找到详细的解释。

作为一般性提示,无论如何,我强烈建议您仔细阅读 PHP 产生的错误消息(并在 Whosebug 上寻求帮助时将其与源代码一起报告):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.php 加上 line 2 为您提供有关问题位置的信息。

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