简体   繁体   English

联系表格PHP脚本无法加载?

[英]Contact form PHP script doesnt load?

So I am trying to build a contact form using html and a php script. 因此,我正在尝试使用html和php脚本建立联系表单。 I have done this in the past (with templates) and all seem fine however I tried creating one on my own and for some reasons it isnt loading the script when submitting it on the html page (the form action etc is all correct) Im guessing it must be sometihng to do with the php please see the code below. 我过去已经做到了这一点(使用模板),但看起来都不错,但是我尝试自己创建一个,由于某些原因,它在html页面上提交脚本时并没有加载脚本(表单操作等都是正确的)。它必须与php有关,请参见下面的代码。 Any help would be greatful: 任何帮助将是巨大的:

   <?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

}

?>

EDIT: Updated Php and added html 编辑:更新PHP和添加html

Here is also my html form: 这也是我的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

Don't use "-" in var-names: 请勿在变量名中使用“-”:

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

And turn on error_reporting, when you developing to see all erros php give you :) (in this case: Parse error) 并打开error_reporting,在进行开发以查看所有erros php都给您时:)(在这种情况下:解析错误)

Second problem: You check for $_POST['email'] , if it is set, if not, you doing nothing. 第二个问题:您检查$_POST['email'] ,如果已设置,如果没有设置,则什么也不做。 But in your form, there is no input field with this name, so your script never do anything. 但是在您的表单中,没有使用该名称的输入字段,因此您的脚本永远不会做任何事情。

The character - is not allowed in variables names. 变量名称中不允许使用字符- Try replacing lines like 尝试替换像

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

with

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

In PHP, a valid variable name starts with a letter or an underscore, followed by any number of letters, numbers, or underscores. 在PHP中,有效的变量名称以字母或下划线开头,后跟任意数量的字母,数字或下划线。 No - character is allowed. -允许使用字符。 This is because otherwise there would be an ambiguity with the maths operation - . 这是因为,否则数学运算符-会产生歧义。

You can find a detailed explanation in the official documentation page . 您可以在官方文档页面中找到详细的说明。

As a general hint, anyway, I strongly suggest you to carefully read the error message produced by PHP (and report it together with the source code, when asking for a help on StackOverflow): PHP, like almost all the programming languages out there, is pretty informative in case of a failure. 无论如何,总的来说,我强烈建议您仔细阅读PHP产生的错误消息(并在寻求StackOverflow帮助时将其与源代码一起报告):PHP,就像几乎所有的编程语言一样,在发生故障时非常有用。 If you read the error message, it is very likely that it tells you what the problem is and where it has been found (that is, typically, the line number). 如果你读了错误信息,则很可能是它告诉你问题是什么在哪里 ,已经发现(也就是通常,行号)。 For example, a typical PHP error message could be 例如,典型的PHP错误消息可能是

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

where Call to undefined function my_function() is the problem and /home/mysite/public_html/test.php plus line 2 give you an information on the problem position. 其中Call to undefined function my_function()是问题,而/home/mysite/public_html/test.php加上line 2为您提供有关问题位置的信息。

In case your script didn't give you enough information, please, refer to the Error Reporting page in the manual: it contains precious information on how to improve details in error messages. 如果您的脚本没有为您提供足够的信息,请参考手册中的“ 错误报告”页面:其中包含有关如何改进错误消息中的详细信息的宝贵信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM