runnerjp Posted November 11, 2009 Share Posted November 11, 2009 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 Quote Link to comment Share on other sites More sharing options...
cags Posted November 11, 2009 Share Posted November 11, 2009 Ideally to check values from the $_POST array you would use... if(isset($_POST['field_name']) && !empty($_POST['field_name'])){ Quote Link to comment Share on other sites More sharing options...
runnerjp Posted November 11, 2009 Author Share Posted November 11, 2009 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 Quote Link to comment Share on other sites More sharing options...
cags Posted November 11, 2009 Share Posted November 11, 2009 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"; } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.