Jump to content

Validating form field


myndcraft

Recommended Posts

Logic and reasoning have left me for a warmer climate so I'm turning to forum members.

 

I have two variables that have been set from a form lets call them  $item1  and $item1_color

 

What I am trying to do is append to an error string ($errmsg) if either variable has been set or is not empty AND the other variable has. 

 

So basically if a user selects an item, but not the color set the error.  Or if they have set the color, but not selected an item set the error.  Can someone help me with the best way to do this?

 

Thanks!

 

 

Link to comment
https://forums.phpfreaks.com/topic/127468-validating-form-field/
Share on other sites

Hi Thorpe, so I am encountering a problem here somewhere.

 

So here is the code…

echo $bumper_left . " " . $bumper_left_color;

if ((isset($bumper_left) && !isset($bumper_left_color)) || (!isset($bumper_left) && isset($bumper_left_color))) {
	$errmsg .= "Mismatch error";
}
echo $errmsg;

 

If I set both values in the form I see them both in the initial echo.  If I refresh the page and enter just one field I see only that field, but the errmsg echo never appears.

 

Any thoughts?

 

 

well we are almost there now!  So it works in that if I enter text in both fields there is no error AND if I leave one field blank it errors as well which is correct.  The problem is it also errors if I leave both fields blank which should be acceptable.

 

Thanks btw for helping out thus far :D

Well I thank you tremendously because it works, but could you walk thru what's happening there?  I am trying to reason myself thru it and am getting hung up.

 

Basically it is split up into three parts. All of which need to be proved true in order for the error message to be shown.

 

IF

 

1: Both variables are set.

AND

2: One is empty

AND

3: Both are not empty

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.