0

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>
  • Which part of the code shows up? Only part of it? – Cheruvian Jul 05 '14 at 22:04
  • 1
    possible duplicate of [Why is this PHP code showing up as regular text (html)?](http://stackoverflow.com/questions/22899752/why-is-this-php-code-showing-up-as-regular-text-html) – FuzzyTree Jul 05 '14 at 22:05
  • this is what shows up: – user3808508 Jul 05 '14 at 22:05
  • this is what shows up Your message has been sent! '; } else { echo ' Something went wrong, go back and try again! '; } else if ($_POST['submit'] && $human != '4') { echo ' You answered the anti-spam question incorrectly! '; } } ?> – user3808508 Jul 05 '14 at 22:06
  • this does look like the same code as the second comment. same issue. – user3808508 Jul 05 '14 at 22:13
  • You have a misplaced closing brace `}` for your `if()`,remove it from after your `else if` and place it before the `else if` – Sean Jul 05 '14 at 22:25

1 Answers1

0

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.

Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291