PHP: How to make the user stay on the same page if the required fields in the form aren't filled?

StackOverflow https://stackoverflow.com/questions/23196740

  •  06-07-2023
  •  | 
  •  

Question

How can I make the user stay on the same page if the required fields in the form aren't filled? Any answer would be greatly appreciated.

<?php 
header('Location: COMING SOON.html');
$name = $_POST['name'];
$surname = $_POST['last_name'];
$cons = $_POST['cons'];
$password = $_POST['pass'];
$email = $_POST['email'];
$message = $_POST['message'];
$recipient = "pixiedustmed@yahoo.com";

$to = 'pixiedustmed@yahoo.com';

$subject = "Contact Message from: $name $surname - $email";

$headers = "From: $name $surname \r\n";
$headers .= "Reply-To: ". strip_tags($_POST['email']) . "\r\n";
$headers .= "CC: avedis@avedis.ga\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";

$message = '<html><body>';
$message .= '<div class="about-center"><h1 style="font-family: Lato-Light; font-size: 2em">You have received a contact message from Your website.</h1></div>';
$message .= '<table rules="all" style="border-color: #666; font-family: Segoe UI; font-size: 16px;" cellpadding="12">';
$message .= "<tr style='background: #eee;'><td><p style='color: #2c3e50; font-weight:bold'>Name: </p> </td><td>" . strip_tags($_POST['name']) . "</td></tr>";
$message .= "<tr style='background: #eee;'><td><p style='color: #2c3e50; font-weight:bold'>Surname: </p> </td><td>" . strip_tags($_POST['last_name']) . "</td></tr>";
$message .= "<tr><td><p style='color: #2c3e50; font-weight:bold'>Email: </p> </td><td>" . strip_tags($_POST['email']) . "</td></tr>";
$message .= "<tr style='background: #71bdf4;'><td><p style='color: #ffffff'>Message: </p> </td><td>" . strip_tags($_POST['message']) . "</td></tr>";
$message .= "<tr style='background: #34495e;'><td><p style='color: #ffffff'>Consultation Info: </p> </td><td style='color: #ffffff'>" . strip_tags($_POST['cons']) . "</td></tr>";
$message .= "<tr><td><p style='color: #2c3e50; font-weight:bold'>Password: </p> </td><td>" . strip_tags($_POST['pass']) . "</td></tr>";
$message .= "</table>";
$message .= '<br><h1 style="font-family: Lato-Light; font-size: 14px; color:#c0392b;"><a href="http://avedis.ga">Click here to open avedis.ga</a></h1>';
$message .= '<h1 style="font-family: Segoe UI; font-size: 13px; color:#eee;">This is an autogenerated message designed by S.G.</h1>';
$message .= '<div class="about-center">-</div>';
$message .= "</body></html>";


mail($recipient, $subject, $message, $headers, $password) or die("Error!");
echo "Thank You!";
?>

So, as you see, I have put header('Location: COMING SOON.html'); which evantually works. Plus, in my css, I have put a required="" field which didn't let the user click the submit button and displayed a message. But now that I put a header, the PHP disregards everything.

My site can be accessed by clicking here; scroll down to the end of the page and try in the contact form, and. Let me know what you think about the design and all!

Was it helpful?

Solution

if(empty($name) || empty($surname) || empty($cons) || empty($password) || empty($email) || empty($_POST['email']) || empty($message)){
    // do something if a field is empty
} else {
    // do something if fields are all filled
}

Put this after you declare the variables like $name, $surname, and $cons.

OTHER TIPS

Use some javascript to ensure that the user has filled all the fields, or just include the php inside the index page and if the user has filled all the forms send the mail, otherwise show an error around the unfilled fields.

Example using jQuery: Check all input fields have been filled out with jQuery

You should check and validate the feilds first and if the feilds are correctly submitted, then only u should redirect the user using the header().

ps: what you asked was actually not clear.

Hope it helps thankz!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top