By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,573 Members | 907 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,573 IT Pros & Developers. It's quick & easy.

if/else statement, form-check

P: n/a
I have a form where the user has the possibility to enclose his name.
email, address and phonenumber.

I want to be able to check if some of the fields are filled - at least
one. This is so that we have some way to contact the customer.

Today I check for the existence of an email-address, and a validation
code - what I need is a way to check, eg. whether email _or_ phone is
entered, and if none is entered, display an errorpage.

Today, my code looks like this:

if (empty( $_POST["mail_adresse"])) {
header( "Location: error_mail.php" );
}
elseif ($valkode != $sjekkode) {
header( "Location: error_kode.php" );
}
else {
mail("[lots of vaiables]", "From: $adrs" );
}

What I would like is some way to _always_ check for the correct code
being entered, but allow for either email, phonenumber or both being
present.

I've tried with || and && and other ways to check for more than one
variable in the if-statement, but I can't seem to get it right...

Any tips?

--
mvh
Ørjan Langbakk
http://www.bergenpchjelp.no
http://www.cubic-design.net
Jun 28 '06 #1
Share this Question
Share on Google+
14 Replies


P: n/a
psudocode:

if ( invalid code )
redirect
elseif ( !email && !phone )
other redirect
Ørjan Langbakk wrote:
I have a form where the user has the possibility to enclose his name.
email, address and phonenumber.

I want to be able to check if some of the fields are filled - at least
one. This is so that we have some way to contact the customer.

Today I check for the existence of an email-address, and a validation
code - what I need is a way to check, eg. whether email _or_ phone is
entered, and if none is entered, display an errorpage.

Today, my code looks like this:

if (empty( $_POST["mail_adresse"])) {
header( "Location: error_mail.php" );
}
elseif ($valkode != $sjekkode) {
header( "Location: error_kode.php" );
}
else {
mail("[lots of vaiables]", "From: $adrs" );
}

What I would like is some way to _always_ check for the correct code
being entered, but allow for either email, phonenumber or both being
present.

I've tried with || and && and other ways to check for more than one
variable in the if-statement, but I can't seem to get it right...

Any tips?

--
mvh
Ørjan Langbakk
http://www.bergenpchjelp.no
http://www.cubic-design.net


Jun 28 '06 #2

P: n/a
Den 28.06.2006 22:27, skriblet BKDotCom følgende:
psudocode:

if ( invalid code )
redirect
elseif ( !email && !phone )
other redirect


Well, yes, but that doesn't really work the way I want it.

See, I have, first, a check for existing email - if there is no email
present, I want it to check if there is a phonenumber entered, and if
_both_ those conditions are false, then I want to redirect.

I do not wish to redirect after the first false, but after the second.

Have I misunderstood something, or?
--
mvh
Ørjan Langbakk
http://www.bergenpchjelp.no
http://www.cubic-design.net
Jun 29 '06 #3

P: n/a
Ørjan Langbakk wrote:
Den 28.06.2006 22:27, skriblet BKDotCom følgende:
psudocode:

if ( invalid code )
redirect
elseif ( !email && !phone )
other redirect


Well, yes, but that doesn't really work the way I want it.

See, I have, first, a check for existing email - if there is no email
present, I want it to check if there is a phonenumber entered, and if
_both_ those conditions are false, then I want to redirect.

I do not wish to redirect after the first false, but after the second.

Have I misunderstood something, or?


Isn't this just:

if( ! email && ! phone ) redirect;
if( email ) do_email
else do_phone

-david-

Jun 29 '06 #4

P: n/a
David Haynes wrote:
Ørjan Langbakk wrote:

See, I have, first, a check for existing email - if there is no email
present, I want it to check if there is a phonenumber entered, and if
_both_ those conditions are false, then I want to redirect.

I do not wish to redirect after the first false, but after the second.

Have I misunderstood something, or?

Isn't this just:

if( ! email && ! phone ) redirect;
if( email ) do_email
else do_phone


if (!email || !phone) redirect;
// if you are here, all is ok.

Redirect will happen only of both conditions fail. Ofcourse, it does
have a side-effect that if email is valid, phone will never be checked
because the condition for "if" will already satisfy.

Ofcourse, it doesn't hurt to have a small "isset" test on the whole
thing, especially when we were talking about $_POST. If someone
accesses the page via GET, condition would fail, and under some
circumstances, just fall through.
Jun 29 '06 #5

P: n/a
Marcin Dobrucki wrote:
David Haynes wrote:
Ørjan Langbakk wrote:

See, I have, first, a check for existing email - if there is no email
present, I want it to check if there is a phonenumber entered, and if
_both_ those conditions are false, then I want to redirect.

I do not wish to redirect after the first false, but after the second.

Have I misunderstood something, or?

Isn't this just:

if( ! email && ! phone ) redirect;
if( email ) do_email
else do_phone


if (!email || !phone) redirect;
// if you are here, all is ok.

Redirect will happen only of both conditions fail. Ofcourse, it does
have a side-effect that if email is valid, phone will never be checked
because the condition for "if" will already satisfy.

Ofcourse, it doesn't hurt to have a small "isset" test on the whole
thing, especially when we were talking about $_POST. If someone
accesses the page via GET, condition would fail, and under some
circumstances, just fall through.


I don't see how 'if (!email || !phone) redirect;'
handles this case:

"if there is no email present, I want it to check if there is a
phonenumber entered, and if _both_ those conditions are false,
then I want to redirect."

The || would say that you will redirect if *either* email or phone is
not set.

With my code, if both are not set, then the redirect occurs.
If you continue past the if() then you know either email or phone (or
both) is set.
So you just take the action based upon that knowledge with a precedence
given to the email.

-david-

Jun 29 '06 #6

P: n/a
David Haynes wrote:
I don't see how 'if (!email || !phone) redirect;'
handles this case:

"if there is no email present, I want it to check if there is a
phonenumber entered, and if _both_ those conditions are false,
then I want to redirect."

The || would say that you will redirect if *either* email or phone is
not set.


Yes, indeed. I guess I misinterpreted. Or maybe not. The original
post said:

"Today I check for the existence of an email-address, and a validation
code - what I need is a way to check, eg. whether email _or_ phone is
entered, and if none is entered, display an errorpage."

So if you take it that both email and phone must be present and
valid, then "&&", if you take it that either of the email or phone must
be valid, then its "||". In either case, the rest of it is the same.

/m
Jun 29 '06 #7

P: n/a
Ørjan Langbakk <pu******@start.no> wrote:
See, I have, first, a check for existing email - if there is no email
present, I want it to check if there is a phonenumber entered, and if
_both_ those conditions are false, then I want to redirect.


Try this scheme:

$err = ""; # here we collect all the error msgs

if( invalid email )
$err .= "<li>Invalid email.";
if( invalid xxxx )
$err .= "<li>Invalid xxxx.";
if( invalid yyyy )
$err .= "<li>Invalid yyyy.";
/* ...and so on for the other fields to be validated */

Now we check the resulting error message $err:

if( length($err) > 0 ){
echo "<html><body><h1>Error</h1><ul>$err</ul></body></html>";
exit();
}
....here save the data or display the results...

or:

if( length($err) > 0 ){
redirect to http://www.xyz.xx/somewhere.php?err=" . rawurlencode($err)
so that the page somewhere.php can display the reason of the failure
exit();
}
....here save the data or display the results...
But the simplest, better way to handle the input/validation/error cycle
is using a single PHP page:

<?php

class MyForm {
public $field1 = "";
public $field2 = "";
/* ... */
}

function myform_display(MyForm $f = NULL, $err = "")
{
if( $f === NULL ){
# request to display an empty FORM:
$f = new MyForm();
}
echo "<html><body>";
if( length($err) > 0 )
echo "<ul>$err</ul>";
echo "<form method=post action='", $_SERVER['PHP_SELF'], "'>";
echo "<input type=text name=field1 value=\"",
htmlspecialchar($f->field1), "\">";
/* ...and so on for the other fields */
echo "<input type=hidden name=form_name value=myform>";
echo "<input type=submit name=b value=OK></form></body></html>";
}

function myform_validate()
{
/* Acquires the POSTed data: */
$f = new MyForm();
$f->field1 = (string) $_POST['field1'];
$f->field2 = (string) $_POST['field2'];
/* .... */

/* Validation: */
$err = "";
if( invalid $f->field1 )
$err .= "<li>Invalid xxxxxxxxxxxx";
if( invalid $f->field2 )
$err .= "<li>Invalid yyyyyyyyyyyy";
/* ....... */
if( length($err) > 0 ){
myform_display($f, $err);
exit(0);
}

/* Save or otherwise use the resulting, valid data $f: */
...save the data...
...redirect to the next page...
}

# main()

if( isset($_POST) && isset($_POST['form_name'])
&& $_POST['form_name'] === 'myform' ){
# that's just a POST from this FORM:
myform_validate();
} else {
# displays an empty FORM:
myform_display(NULL, "");
}

?>
Best regards,
___
/_|_\ Umberto Salsi
\/_\/ www.icosaedro.it

Jun 29 '06 #8

P: n/a
David Haynes wrote:
Marcin Dobrucki wrote:
David Haynes wrote:
Ørjan Langbakk wrote:


See, I have, first, a check for existing email - if there is no
email present, I want it to check if there is a phonenumber entered,
and if _both_ those conditions are false, then I want to redirect.

I do not wish to redirect after the first false, but after the second.

Have I misunderstood something, or?

Isn't this just:

if( ! email && ! phone ) redirect;
if( email ) do_email
else do_phone

if (!email || !phone) redirect;
// if you are here, all is ok.

Redirect will happen only of both conditions fail. Ofcourse, it
does have a side-effect that if email is valid, phone will never be
checked because the condition for "if" will already satisfy.

Ofcourse, it doesn't hurt to have a small "isset" test on the whole
thing, especially when we were talking about $_POST. If someone
accesses the page via GET, condition would fail, and under some
circumstances, just fall through.

I don't see how 'if (!email || !phone) redirect;'
handles this case:

"if there is no email present, I want it to check if there is a
phonenumber entered, and if _both_ those conditions are false,
then I want to redirect."

The || would say that you will redirect if *either* email or phone is
not set.

With my code, if both are not set, then the redirect occurs.
If you continue past the if() then you know either email or phone (or
both) is set.
So you just take the action based upon that knowledge with a precedence
given to the email.

-david-


David,

If you want to redirect if both are invalid, just do:

if(!email && !phone)...
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Jun 29 '06 #9

P: n/a
Jerry Stuckle wrote:
David Haynes wrote:
Marcin Dobrucki wrote:
David Haynes wrote:

Ørjan Langbakk wrote:
> See, I have, first, a check for existing email - if there is no
> email present, I want it to check if there is a phonenumber
> entered, and if _both_ those conditions are false, then I want to
> redirect.
>
> I do not wish to redirect after the first false, but after the second.
>
> Have I misunderstood something, or?
>
Isn't this just:

if( ! email && ! phone ) redirect;
if( email ) do_email
else do_phone
if (!email || !phone) redirect;
// if you are here, all is ok.

Redirect will happen only of both conditions fail. Ofcourse, it
does have a side-effect that if email is valid, phone will never be
checked because the condition for "if" will already satisfy.

Ofcourse, it doesn't hurt to have a small "isset" test on the whole
thing, especially when we were talking about $_POST. If someone
accesses the page via GET, condition would fail, and under some
circumstances, just fall through.

I don't see how 'if (!email || !phone) redirect;'
handles this case:

"if there is no email present, I want it to check if there is a
phonenumber entered, and if _both_ those conditions are false,
then I want to redirect."

The || would say that you will redirect if *either* email or phone is
not set.

With my code, if both are not set, then the redirect occurs.
If you continue past the if() then you know either email or phone (or
both) is set.
So you just take the action based upon that knowledge with a
precedence given to the email.

-david-


David,

If you want to redirect if both are invalid, just do:

if(!email && !phone)...

Jerry:
That's what I said... ;-)

-david-

Jun 29 '06 #10

P: n/a
Den 29.06.2006 17:43, skriblet David Haynes følgende:
Jerry Stuckle wrote:
David Haynes wrote:
Marcin Dobrucki wrote:

David Haynes wrote:

> Ørjan Langbakk wrote:

>> See, I have, first, a check for existing email - if there is no
>> email present, I want it to check if there is a phonenumber
>> entered, and if _both_ those conditions are false, then I want to
>> redirect.
>>
>> I do not wish to redirect after the first false, but after the second.
>>
>> Have I misunderstood something, or?
>>
> Isn't this just:
>
> if( ! email && ! phone ) redirect;
> if( email ) do_email
> else do_phone

if (!email || !phone) redirect;
// if you are here, all is ok.

Redirect will happen only of both conditions fail. Ofcourse, it
does have a side-effect that if email is valid, phone will never be
checked because the condition for "if" will already satisfy.

Ofcourse, it doesn't hurt to have a small "isset" test on the whole
thing, especially when we were talking about $_POST. If someone
accesses the page via GET, condition would fail, and under some
circumstances, just fall through.

I don't see how 'if (!email || !phone) redirect;'
handles this case:

"if there is no email present, I want it to check if there is a
phonenumber entered, and if _both_ those conditions are false,
then I want to redirect."

The || would say that you will redirect if *either* email or phone is
not set.

With my code, if both are not set, then the redirect occurs.
If you continue past the if() then you know either email or phone (or
both) is set.
So you just take the action based upon that knowledge with a
precedence given to the email.

-david-

David,

If you want to redirect if both are invalid, just do:

if(!email && !phone)...


I might be dense, but it still isn't the problem. Of course I can check
if both are empty (invalid), that's easy.

Why doesn't PHP have an if/then/else way of doing things... or, can I do
a check for criteria two _before_ the redirect?

something like:
if (not valid)
then (check criteria two)
if both = invalid, then redirect
if both = valid, then -> check code
if code valiates, then send mail
--
mvh
Ørjan Langbakk
http://www.bergenpchjelp.no
http://www.cubic-design.net
Jun 29 '06 #11

P: n/a
Ørjan Langbakk wrote:
[snip]
I might be dense, but it still isn't the problem. Of course I can check
if both are empty (invalid), that's easy.

Why doesn't PHP have an if/then/else way of doing things... or, can I do
a check for criteria two _before_ the redirect?

something like:
if (not valid)
then (check criteria two)
if both = invalid, then redirect
if both = valid, then -> check code
if code valiates, then send mail

You seem to think this is a short-coming of the PHP language.
Do you have this coded in another language? If so, can you share that?

I profess that there has been so many messages back and forth about
this, that I am no longer sure what you are asking for.

Can you supply a set of input conditions (using email and phone) and
what you expect the outcome to be? From you latest post, it sounds like
you want to evaluate a quad-state logic condition with if-then-else
logic in a single pass, but I am so confused at the moment, that would
be a 'best guess' as to what you want.

-david-

Jun 29 '06 #12

P: n/a
Den 29.06.2006 20:47, skriblet David Haynes følgende:
Ørjan Langbakk wrote:
[snip]
I might be dense, but it still isn't the problem. Of course I can check
if both are empty (invalid), that's easy.

Why doesn't PHP have an if/then/else way of doing things... or, can I do
a check for criteria two _before_ the redirect?

something like:
if (not valid)
then (check criteria two)
if both = invalid, then redirect
if both = valid, then -> check code
if code valiates, then send mail

You seem to think this is a short-coming of the PHP language.
Do you have this coded in another language? If so, can you share that?

I profess that there has been so many messages back and forth about
this, that I am no longer sure what you are asking for.

Can you supply a set of input conditions (using email and phone) and
what you expect the outcome to be? From you latest post, it sounds like
you want to evaluate a quad-state logic condition with if-then-else
logic in a single pass, but I am so confused at the moment, that would
be a 'best guess' as to what you want.


Okay - I don't have access to the code right now, but I can try to
explain a little better.

I have a form, with several fields: name, address, email and phone.

When the user submits the form, I need him to have put down at least his
name and _either_ his email, address or phone number, so we have a sure
way to get in contact with him.

That's why, while validating the form-data, I would like to process
_first_ the name, and if there is no name entered, then I'll show an
error - if there _is_ a name entered, I go to the next part, which is to
check if there is an email-address entered - if there isn't, I want to
go to the _next_ part, check if there is a phonenumber entered, if there
isn't a phonenumber either, I want to show an errormessage - if there
_is_ a phonenumber entered, then that will be sufficient, and the final
check, for the valid numbers from a code-field will be checked - if
erroneus, I'll display an error, if correct, the form will be processed.

As of now, I have chosen to have name, email and phonenumber as
obligatory fields - they all get checked, and they all need input or the
error-page displays. That works, of course, but I would like to have the
above solution, as that would be more flexible towards the users.

--
mvh
Ørjan Langbakk
http://www.bergenpchjelp.no
http://www.cubic-design.net
Jun 29 '06 #13

P: n/a
Ørjan Langbakk wrote:
Den 29.06.2006 20:47, skriblet David Haynes følgende:
Ørjan Langbakk wrote:
[snip]
I might be dense, but it still isn't the problem. Of course I can
check if both are empty (invalid), that's easy.

Why doesn't PHP have an if/then/else way of doing things... or, can I
do a check for criteria two _before_ the redirect?

something like:
if (not valid)
then (check criteria two)
if both = invalid, then redirect
if both = valid, then -> check code
if code valiates, then send mail

You seem to think this is a short-coming of the PHP language.
Do you have this coded in another language? If so, can you share that?

I profess that there has been so many messages back and forth about
this, that I am no longer sure what you are asking for.

Can you supply a set of input conditions (using email and phone) and
what you expect the outcome to be? From you latest post, it sounds like
you want to evaluate a quad-state logic condition with if-then-else
logic in a single pass, but I am so confused at the moment, that would
be a 'best guess' as to what you want.


Okay - I don't have access to the code right now, but I can try to
explain a little better.

I have a form, with several fields: name, address, email and phone.

When the user submits the form, I need him to have put down at least his
name and _either_ his email, address or phone number, so we have a sure
way to get in contact with him.

That's why, while validating the form-data, I would like to process
_first_ the name, and if there is no name entered, then I'll show an
error - if there _is_ a name entered, I go to the next part, which is to
check if there is an email-address entered - if there isn't, I want to
go to the _next_ part, check if there is a phonenumber entered, if there
isn't a phonenumber either, I want to show an errormessage - if there
_is_ a phonenumber entered, then that will be sufficient, and the final
check, for the valid numbers from a code-field will be checked - if
erroneus, I'll display an error, if correct, the form will be processed.

As of now, I have chosen to have name, email and phonenumber as
obligatory fields - they all get checked, and they all need input or the
error-page displays. That works, of course, but I would like to have the
above solution, as that would be more flexible towards the users.


So, you are looking for flexible input validation scheme based upon an
arbitrary set of values where some values have an 'either of' relationship.

Okey-dokey...

How about this?

<?php
$_POST['name'] = 'NAME';
$_POST['email'] = 'E****@DOMAIN.COM';
$_POST['phone'] = '123-456-1234';

function name_validate($name) {
return true;
}

function email_validate($email) {
return true;
}

function phone_validate($phone) {
return true;
}

$requires = array('name', 'email|phone');

$valid = true;
foreach( $requires as $required ) {
$options = explode('|', $required);
$one_of = false;
foreach( $options as $option ) {
if( isset($option) ) {
$func = $option.'_validate';
if( $func($_POST[$option]) ) {
$one_of = true;
}
}
}
if( ! $one_of ) {
$valid = false;
break;
}
}

printf('The form is %svalid\n', $valid ? '' : 'not ');
?>

-david-

Jun 30 '06 #14

P: n/a
Ørjan Langbakk wrote:
I might be dense, but it still isn't the problem. Of course I can check
if both are empty (invalid), that's easy.

Why doesn't PHP have an if/then/else way of doing things... or, can I do
a check for criteria two _before_ the redirect?

something like:
if (not valid)
then (check criteria two)
if both = invalid, then redirect
if both = valid, then -> check code
if code valiates, then send mail


I think David provided a solution already, but if you have probelms
digging through the if/else stuff in the future, pen and paper always
works for me. Draw a flowchart of it, and then walk through it a couple
of times.
Jun 30 '06 #15

This discussion thread is closed

Replies have been disabled for this discussion.