if/else statement, form-check 
June 28th, 2006, 08:25 PM
| | | if/else statement, form-check
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 | 
June 28th, 2006, 08:25 PM
| | | Re: if/else statement, form-check
psudocode:
if ( invalid code )
redirect
elseif ( !email && !phone )
other redirect
Ørjan Langbakk wrote:[color=blue]
> 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[/color] | 
June 29th, 2006, 11:25 AM
| | | Re: if/else statement, form-check
Den 28.06.2006 22:27, skriblet BKDotCom følgende:[color=blue]
> psudocode:
>
> if ( invalid code )
> redirect
> elseif ( !email && !phone )
> other redirect[/color]
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 | 
June 29th, 2006, 12:55 PM
| | | Re: if/else statement, form-check
Ørjan Langbakk wrote:[color=blue]
> Den 28.06.2006 22:27, skriblet BKDotCom følgende:[color=green]
>> psudocode:
>>
>> if ( invalid code )
>> redirect
>> elseif ( !email && !phone )
>> other redirect[/color]
>
> 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?
>
>[/color]
Isn't this just:
if( ! email && ! phone ) redirect;
if( email ) do_email
else do_phone
-david- | 
June 29th, 2006, 01:15 PM
| | | Re: if/else statement, form-check
David Haynes wrote:[color=blue]
> Ørjan Langbakk wrote:[/color]
[color=blue][color=green]
>> 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?
>>[/color]
> Isn't this just:
>
> if( ! email && ! phone ) redirect;
> if( email ) do_email
> else do_phone[/color]
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. | 
June 29th, 2006, 01:25 PM
| | | Re: if/else statement, form-check
Marcin Dobrucki wrote:[color=blue]
> David Haynes wrote:[color=green]
>> Ørjan Langbakk wrote:[/color]
>[color=green][color=darkred]
>>> 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?
>>>[/color]
>> Isn't this just:
>>
>> if( ! email && ! phone ) redirect;
>> if( email ) do_email
>> else do_phone[/color]
>
> 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.[/color]
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- | 
June 29th, 2006, 01:55 PM
| | | Re: if/else statement, form-check
David Haynes wrote:
[color=blue]
> 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.[/color]
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 | 
June 29th, 2006, 01:55 PM
| | | Re: if/else statement, form-check
Ørjan Langbakk <pubblicc@start.no> wrote:
[color=blue]
> 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.[/color]
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 | 
June 29th, 2006, 03:35 PM
| | | Re: if/else statement, form-check
David Haynes wrote:[color=blue]
> Marcin Dobrucki wrote:
>[color=green]
>> David Haynes wrote:
>>[color=darkred]
>>> Ørjan Langbakk wrote:[/color]
>>
>>[color=darkred]
>>>> 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[/color]
>>
>>
>> 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.[/color]
>
>
> 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-
>[/color]
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. jstucklex@attglobal.net
================== | 
June 29th, 2006, 03:45 PM
| | | Re: if/else statement, form-check
Jerry Stuckle wrote:[color=blue]
> David Haynes wrote:[color=green]
>> Marcin Dobrucki wrote:
>>[color=darkred]
>>> 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.[/color]
>>
>>
>> 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-
>>[/color]
>
> David,
>
> If you want to redirect if both are invalid, just do:
>
> if(!email && !phone)...
>
>[/color]
Jerry:
That's what I said... ;-)
-david- | 
June 29th, 2006, 06:35 PM
| | | Re: if/else statement, form-check
Den 29.06.2006 17:43, skriblet David Haynes følgende:[color=blue]
> Jerry Stuckle wrote:[color=green]
>> David Haynes wrote:[color=darkred]
>>> 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-
>>>[/color]
>> David,
>>
>> If you want to redirect if both are invalid, just do:
>>
>> if(!email && !phone)...[/color][/color]
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 | 
June 29th, 2006, 06:45 PM
| | | Re: if/else statement, form-check
Ørjan Langbakk wrote:
[snip][color=blue]
> 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
>
>[/color]
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- | 
June 29th, 2006, 11:35 PM
| | | Re: if/else statement, form-check
Den 29.06.2006 20:47, skriblet David Haynes følgende:[color=blue]
> Ørjan Langbakk wrote:
> [snip][color=green]
>> 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
>>
>>[/color]
> 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.[/color]
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 | 
June 30th, 2006, 12:45 AM
| | | Re: if/else statement, form-check
Ørjan Langbakk wrote:[color=blue]
> Den 29.06.2006 20:47, skriblet David Haynes følgende:[color=green]
>> Ørjan Langbakk wrote:
>> [snip][color=darkred]
>>> 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
>>>[/color]
>> 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.[/color]
>
> 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.
>[/color]
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'] = 'EMAIL@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- | 
June 30th, 2006, 07:25 AM
| | | Re: if/else statement, form-check
Ørjan Langbakk wrote:
[color=blue]
> 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[/color]
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. | | Thread Tools | Search this Thread | | | |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | | | | What is Bytes?
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 220,989 network members.
|