简体   繁体   中英

Nothing happens when i click on submit button in my contact form

When I click on the submit button, nothing happens, even though I do have PHP set up. I thought at first that my code is obviously wrong. But now I even copy/pasted the code from here, that is checked as correct, yet nothing happens.

Here is that code:

<?php
if(isset($_POST['submit'])){
    $to = "zezesurla@gmail.com"; // this is your Email address
    $from = $_POST['email']; // this is the sender's Email address
    $first_name = $_POST['first_name'];
    $last_name = $_POST['last_name'];
    $subject = "Form submission";
    $subject2 = "Copy of your form submission";
    $message = $first_name . " " . $last_name . " wrote the following:" . "\n\n" . $_POST['message'];
    $message2 = "Here is a copy of your message " . $first_name . "\n\n" . $_POST['message'];

    $headers = "From:" . $from;
    $headers2 = "From:" . $to;
    mail($to,$subject,$message,$headers);
    mail($from,$subject2,$message2,$headers2); // sends a copy of the message to the sender
    echo "Mail Sent. Thank you " . $first_name . ", we will contact you shortly.";
    // You can also use header('Location: thank_you.php'); to redirect to another page.
}
?>

<!DOCTYPE html>
<head>
<title>Form submission</title>
</head>
<body>

<form action="" method="post">
First Name: <input type="text" name="first_name"><br>
Last Name: <input type="text" name="last_name"><br>
Email: <input type="text" name="email"><br>
Message:<br><textarea rows="5" name="message" cols="30"></textarea><br>
<input type="submit" name="submit" value="Submit">
</form>

</body>
</html>


</div>

Could this be a problem that is in the server and not the actual code? Because I tried several other examples that I found on various tutorials and the same issue occurs.

Or am I still doing something wrong?

As a minimum, I would make the following changes to your code:

<?php
$msg='';
if (isset($_POST['submit']))
{

...

    if (!mail($to,$subject,$message,$headers))
        error_log(($msg='Send form submission to "'.$to.'" failed'));
    else if (!mail($from,$subject2,$message2,$headers2)) // sends a copy of the message to the sender
        error_log(($msg='Send copy of form submission to user "'.$from.'" failed'));
    else $msg='Mail Sent. Thank you ' . $first_name . ', we will contact you shortly.';

...

<body>
<?php if ($msg)
{ ?>
<p><?php echo $msg ?></p>
<?php
} ?>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">

These changes will not address the problem of the form being vulnerable to sending SPAM, but it will address these issues:

  • if a mail() call error occurs, it will be logged, and a message will be displayed (which will also confirm the email address involved)
  • single-quoted strings don't have to be parsed by the PHP interpreter and should be used unless you are embedding variables or character sequences (such as "\\n" ) that need to be interpreted
  • you won't be generating bad HTML by having text written to the browser before the <head> section of the document
  • you will be assured the correct action is being used to handle the form

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