Kris.Du Kris.Du - 1 month ago 6
PHP Question

Why is my contact form PHP script not working? No error log

I don't get any errors. I used to get Undefined index/variable errors before but I fixed that with isset(). I fill the form out and hit submit but nothing shows up but a blank white screen.

This is my PHP code:

<?php


$name = isset($_POST['name']) ? $_POST['name'] : '';
$email = isset($_POST['email']) ? $_POST['email'] : '';
$message = isset($_POST['message']) ? $_POST['message'] : '';
$from = 'From: ContactForm';
$to = 'email@email.com';
$subject = 'Hello';
$verify = $_POST['verify'];

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

if (isset($_POST["submit"]) && $verify == '4') {
if (mail ($name, $email, $message)) {
echo '<p>Your message has been sent!</p>';
}

else {
echo '<p>Something went wrong, go back and try again!</p>';
}
}
else if (isset($_POST["submit"]) && verify != '4') {
echo '<p>You answered the anti-spam question incorrectly!</p>';
}

?>


This is my HTML code:

<form action="pages/mail.php" method="post">

<div class="col-md-12">
<div class="form-group">
<label for="name" class="sr-only">Name</label>
<input placeholder="Name" id="name" type="text" class="form-control input-lg">
</div>
</div>

<div class="col-md-12">
<div class="form-group">
<label for="email" class="sr-only">Email</label>
<input placeholder="Email" id="email" type="text" class="form-control input-lg">
</div>
</div>

<div class="col-md-12">
<div class="form-group">
<label for="message" class="sr-only">Message</label>
<textarea placeholder="Message" id="message" class="form-control input-lg" rows="3"></textarea>
</div>
</div>

<div class="col-md-12">
<div class="form-group">
<input id="submit" type="submit" class="btn btn-primary btn-lg " value="Send">
</div>
</div>

<div class="col-md-12">
<div class="form-group">
<label for="verify" class="sr-only">Verify</label>
<input name="verify" placeholder="What is 2+2" class="form-control input-lg">
</div>
</div>
</form>

Answer

White screen usually means internal or syntax error

PHP, unlike JavaScript doesn't let you get away with missing semicolons or curly braces. First, make sure your code is syntactically valid and without typos before going on to fixing the logic. Errors can be displayed in most cases by including the following lines at the top of your PHP file:

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

After you find and fix those errors, here is why the form itself doesn't work:

Inputs need to be referenced by their name attribute

While it is good practice to set the IDs of input elements, your PHP script doesn't know about them. You need to give your input fields a name attribute with the same value as you expect on the server side.

This means if you have the following HTML form and input

<form action="whatever.php" method="POST">
  <input type="text" name="my_text_input">
</form>

You can access it on the server side in whatever.php like so

<?php

$myTextBoxValue = $_POST['my_text_input'];

?>

Hope this helps!

Comments