简体   繁体   中英

Contact form redirect to index.php page shows up with code in the page

I have found this code for redirecting my contact form through an index.php page, but it shows up with part of the code on the redirect page. The code looks okay to me, I don't know what I am missing. Here it is:

<html>
<head>



<style type= "text/css">
   body {
    background-color: #C2C2FF;
  } 

#holla {
width: 500px;
margin-left: auto;
margin-right: auto;
margin-top: 100px;
 }
</style>


</head>
<body>

    <div id= "holla">
       <img src="smiley.png">
    <?php
      $name = $_POST['name'];
      $email = $_POST['email'];
      $message = $_POST['message'];
      $from = 'From: webpage'; 
      $to = 'emailaddressblocked'; 
      $subject = 'Hello';
      $human = $_POST['human'];

      $body = "From: $name\n E-Mail: $email\n Message:\n $message";

      if ($_POST['submit'] && $human == '4') {                 
      if (mail ($to, $subject, $body, $from)) { 
      echo '<p>Your message has been sent!</p>';
      } else { 
        echo '<p>Something went wrong, go back and try again!</p>'; 
      } else if ($_POST['submit'] && $human != '4') {
        echo '<p>You answered the anti-spam question incorrectly!</p>';
      }
      }
     ?>
    </div>

</body>
</html>

You should change this part of code:

if ($_POST['submit'] && $human == '4') {
    if (mail ($to, $subject, $body, $from)) {
        echo '<p>Your message has been sent!</p>';
    } else {
        echo '<p>Something went wrong, go back and try again!</p>';
    } else if ($_POST['submit'] && $human != '4') {
        echo '<p>You answered the anti-spam question incorrectly!</p>';
    }
}

into:

if ($_POST['submit']) {
    if ($human == '4') {
        if (mail($to, $subject, $body, $from)) {
            echo '<p>Your message has been sent!</p>';
        } else {
            echo '<p>Something went wrong, go back and try again!</p>';
        }
    } else {
        echo '<p>You answered the anti-spam question incorrectly!</p>';
    }
}

Earlier in your code you have else and else if together and you repeated the same condition inside if statement and for sure it wasn't what you wanted to do.

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