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

Link to comment
Share on other sites

$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...

Link to comment
Share on other sites

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..

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.