Jump to content

[SOLVED] if form is not left blank check for....


runnerjp

Recommended Posts

here is my code

 

    <?php  foreach($_POST as $field => $value) {
   if (($field != 'submit') && ((!$value) || (trim($value) == ''))) {
      $er .= "<label for=\"uname\" class=\"error\"><em>*</em>$field cannot be empty.</label> <br />";
      $warnings[$field] ="required";
   }
   
if ($value != ''){
if (!$_POST["name"] || !preg_match("/^[a-zA-Z ]+$/", $_POST["name"])) {
   $er .= " <label for=\"uname\" class=\"error\"><em>*</em>Name can only contain letters</label><br />";
   }
if (!$_POST["venue"] || !preg_match("/^[a-zA-Z ]+$/", $_POST["venue"])) {
   $er .= " <label for=\"uname\" class=\"error\"><em>*</em>please only user words</label><br />";
   }
if (!$_POST["event"] || !preg_match("/^[a-zA-Z ]+$/", $_POST["event"])) {
   $er .= " <label for=\"uname\" class=\"error\"><em>*</em>please only user words</label><br />";
   }
$regexp="/^[a-z0-9]+([_\\.-][a-z0-9]+)*@([a-z0-9]+([\.-][a-z0-9]+)*)+\\.[a-z]{2,}$/i";
if (!$_POST["email"] || !preg_match($regexp, $_POST["email"])) {
   $er .= " <label for=\"uname\" class=\"error\"><em>*</em>please enter your correct email address</label><br />";
}}

           $count  = count($er);
           }
if($er === '')
{
   
if(array_key_exists('_submit_check', $_POST))?>

 

at the moment it shows all errors..

 

*name cannot be empty.

*email cannot be empty.

*event cannot be empty.

*venue cannot be empty.

*date cannot be empty.

*Name can only contain letters

*please only user words

*please only user words

*please enter your correct email address

 

but i want it to look first if field are empty, then if field are not emptycheck values in box...

 

Now i would have thought all i needed to do was this  if ($value != ''){  but it shows it even when the value is blank

Link to comment
Share on other sites

I tried

 

 if(isset($_POST['name']) && !empty($_POST['name']) || !preg_match("/^[a-zA-Z ]+$/", $_POST["name"])) {
   $er .= " <label for=\"uname\" class=\"error\"><em>*</em>Name can only contain letters!!</label><br />";

 

but still displays the error message even when post name is empty

Link to comment
Share on other sites

Surely when it is empty you want there to be an error. But it's because your logic is wrong. Assuming that $_POST['name'] contains valid text, this is essentially the same as...

 

$a = TRUE;
$b = FALSE;
$c = TRUE;

if($a && !$b || !$c) {
   echo "Error";
}

If the objective is to go into the if block when there is an error you need something more like...

 

if(!isset($_POST['field_name']) || empty($_POST['field_name']) || !preg_match("/^[a-zA-Z ]+$/", $_POST["name"])) {
   echo "Error";
}

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.