1

Syntax If I change <?= to <php echo then the website no longer displays, I get a 500 error.

Edit Again None of the answers in the duplicate question solve the issue.

Edit I have updated the code, as below, to fix the syntax errors in error messages and change the file to index.php.

The problems persists. When I enter an invalid name or email, and click submit, it opens the PHP file, rather than displaying the error message in the span. When I enter valid details and click submit, it sends the email (so script is working) but still opens the PHP file rather than displaying success message in the div.

I have a contact form on my page as follows:

<form action="form_process.php" method="post" class="form" id="form1">

  <p class="name">
    <input name="name" type="text" placeholder="Name" id="name" class= "feedback-input" value="<?= $name ?>"/>
    <span class="error"><?= $name_error ?></span>
  </p>

  <p class="email">
    <input name="email" type="text" id="email" placeholder="Email" class= "feedback-input" value="<?= $email ?>"/>
    <span class="error"><?= $email_error ?></span>
  </p>

  <p class="text">
    <textarea name="message" type="text" id="comment" placeholder="Message" class= "feedback-input" value="<?= $message ?>"></textarea>
  </p>

  <div class="submit">
    <input type="submit" value="Submit" id="button-blue"/>
    <div class="ease"></div>
  </div>
  <div class="success"><?= $success; ?></div>
</form>

This is the php file:

    <?php 

// define variables and set to empty values
$name_error = $email_error = "";
$name = $email = $message = $success = "";

//form is submitted with POST method
if ($_SERVER["REQUEST_METHOD"] == "POST") {
  if (empty($_POST["name"])) {
    $name_error = "Name is required";
  } else {
    $name = test_input($_POST["name"]);
    // check if name only contains letters and whitespace
    if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
      $name_error = "Only letters and white space allowed"; 
    }
  }

  if (empty($_POST["email"])) {
    $email_error = "Email is required";
  } else {
    $email = test_input($_POST["email"]);
    // check if e-mail address is well-formed
    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
      $email_error = "Invalid email format"; 
    }
  }

  if (empty($_POST["message"])) {
    $message = "";
  } else {
    $message = test_input($_POST["message"]);
  }

  if ($name_error == '' and $email_error == '' ){
      $message_body = '';
      unset($_POST['submit']);
      foreach ($_POST as $key => $value){
          $message_body .=  "$key: $value\n";
      }

      $to = 'xxx@xxx.com';
      $subject = 'Contact Form Submit';
      if (mail($to, $subject, $message)){
          $success = "Message sent, thank you for contacting us!";
          $name = $email = $message = '';
      }
  }

}

function test_input($data) {
  $data = trim($data);
  $data = stripslashes($data);
  $data = htmlspecialchars($data);
  return $data;
}

When I click submit on my computer (mac) it opens the php file and displays the code.

When i click it on the actual website (hosted on dreamhost with php installed) it opens the file and displays a blank page.

I've taken a look at some of thew other issues on here and it doesn't seem to be related to them. Really not sure what's wrong? Would greatly appreciate any help!

2 Answers2

0

By the code you have provided in your question the only issue i see that could make this happen is that you have typos at the php syntax. For example: <span class="error"><?= $email_error >?</span>, where it should be like this: <span class="error"><?php echo $email_error ?></span>.

alex55132
  • 177
  • 1
  • 14
-1

I am quoting your comment:

The php file is on the web server. I meant I had tested it locally, by opening index.html in browser, as well as uploading to the web server.

You cannot write PHP code on .html files - change the extension to .php to the index file.

Ylber Veliu
  • 349
  • 2
  • 13