简体   繁体   中英

Contact form PHP script doesnt load?

So I am trying to build a contact form using html and a php script. 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. 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

Here is also my html form:

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

Second problem: You check for $_POST['email'] , if it is set, if not, you doing nothing. 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. 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. 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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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