Reply to Re: if/else statement, form-check

Your name:

Reply:


Posted by David Haynes on 06/30/06 00:49

Ø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'] = '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-

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация