Forum Moderators: coopster

Message Too Old, No Replies

Can you use "or" in a isset statement?

         

tonynoriega

5:55 pm on Jan 31, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



i have two fields that i want my users to at least enter one value...
the other field would be 'fname'

how can i use "or" with this statement?

if (isset($_POST['submit'])) {
if (!$_POST['lname']){

$display .= 'Error';
} else { //continue script

coopster

5:59 pm on Jan 31, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



They are called logical operators [php.net]. It would look something like this ...
if (isset($_POST['submit'])) { 
if (!isset($_POST['lname'])) ¦¦ !isset($_POST['fname'])) {
$display .= 'Error';
} else { //continue script

Do not forget that the forum breaks the pipe symbol. If you cut/paste that code you must remember to rekey the pipes

tonynoriega

6:05 pm on Jan 31, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Doesnt that statement say that you must have a value for each field?

i am trying to say one can be blank, if the other has a value...

is that more Javascript based?

coopster

8:03 pm on Jan 31, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Actually, no. It is saying that if neither one of them has been set, which typically means in a form at least, that nothing was entered for those values. I check for both. Here is an approach I often use for plain text information.
$fname = isset($_POST['fname']) ? trim($_POST['fname']) : ''; 
$lname = isset($_POST['lname']) ? trim($_POST['lname']) : '';

Here we are getting our form POST variables ready for processing. All I want to do is see if it has been set first and if it has not been set I will initialize it to a blank string. If it has been set, I trim all the whitespace from the front and back of the variable, if any. Now, if I want to know if at least one of them is filled in I can do this ...
if ($fname ¦¦ $lname) { 
// one of them is filled in, with a non-zero value anyway
}

You can do more editing from here, but hopefully this gets you started.