Jump to content

Only submit form if fields have content in


sphinx

Recommended Posts

Hello,

 

I'm using the following code to echo what fields have been left blank:

 

if(isset($_POST['submit']))
{
$emailconfirm = $_POST['emailconfirm'];
$name = trim($_POST['name']);
$visitor_email = trim($_POST['email']);
$user_message = trim($_POST['message']);
if(empty($name)){
$name1 .= "<br>Name is empty.";
}
if(empty($visitor_email)){
$email1 .= "<br>Email is empty.";
}
if(empty($user_message)){
$message1 .= "<br>User Message is empty.";
}

 

But what seems to happen, is once you submit the form with blank fields, the echo does show, however, when you click submit again, the form submits even though the fields were blank. Is their a way of blocking 'post' if the fields are blank?

 

Thanks

$name1 .= "<br>Name is empty.";
$email1 .= "<br>Email is empty.";
$message1 .= "<br>User Message is empty.";

if(isset($_POST['submit']) && trim($_POST['email']) != $email1 && trim($_POST['name']) != $name1 && trim($_POST['message']) != $message1)
{
$emailconfirm = $_POST['emailconfirm'];
$name = trim($_POST['name']);
$visitor_email = trim($_POST['email']);
$user_message = trim($_POST['message']);
if(empty($name)){



$name1 .= "<br>Name is empty.";
}
if(empty($visitor_email)){



$email1 .= "<br>Email is empty.";
}
if(empty($user_message)){



$message1 .= "<br>User Message is empty.";
}

 

 

I think something like this would do it.

 

EDIT: Well actually you'd need to change the name of the variables at the top so they don't appear in the form lol...

i don't see in your code where you are actually disabling the form from being submitted.. if the criteria is not met, you will want to use "return false" so the form does not submit.. also, where do you initially set your values of $name1 $email1 $message1... from the code you posted, you are concatenating where it is not needed.. you will have to set each variable to an empty string before concatenating, if you havn't done so already..

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.