Form validation: First input effecting second

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
J_Mo
Forum Newbie
Posts: 10
Joined: Mon Aug 09, 2010 7:02 pm

Form validation: First input effecting second

Post by J_Mo »

Hi there.

How can I say:

If $formar['title'] is equal to 'Mr' then $formar['Gender'] must be 'Male'
else
If $formar['title'] is equal to 'Mrs' then $formar['Gender'] must be 'Female'
else
If $formar['title'] is equal to 'Ms' then $formar['Gender'] must be 'Male'
else
If $formar['title'] is equal to 'Miss' then $formar['Gender'] must be 'Female'

I am a new to all this php and am struggling!! Thanks
Phil
shawngoldw
Forum Contributor
Posts: 212
Joined: Mon Apr 05, 2010 3:38 pm

Re: Form validation: First input effecting second

Post by shawngoldw »

Code: Select all

if(strcmp($formar['title'], "Mr") == 0)
{
    $formar['Gender'] = "Male";
}
This is not tested but should work if everything is balanced properly

Shawn
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Form validation: First input effecting second

Post by John Cartwright »

I always prefer lookup tables compared to several if() or a switch() statement.

Code: Select all

$titles = array(
   'mr' => 'male',
   'ms' => 'female',
   'mrs' => 'female',
   'miss' => 'female'
);

$mytitle = strtolower($formar['title']); //lower all letters to keep things consistent in comparison
if (array_key_exists($mytitle, $titles)) {
   $formar['gender'] = $titles[$mytitle];
} else {
   $formar['gender'] = 'N/A';
}
J_Mo
Forum Newbie
Posts: 10
Joined: Mon Aug 09, 2010 7:02 pm

Re: Form validation: First input effecting second

Post by J_Mo »

Apologies (and thanks) but I asked the wrong question....

What I meant was

"If $formar['title'] is equal to 'Mr' and $formar['Gender'] is equal to 'Male' {
stuff }

Sorry about that!!
shawngoldw
Forum Contributor
Posts: 212
Joined: Mon Apr 05, 2010 3:38 pm

Re: Form validation: First input effecting second

Post by shawngoldw »

I like John's solution better than my suggestion, but why don't you try to expand on mine to accomplish what you want?

Give it a try and if you can't figure it out, I'll help you out. You should be able to build off of mine to do it though.

Shawn
J_Mo
Forum Newbie
Posts: 10
Joined: Mon Aug 09, 2010 7:02 pm

Re: Form validation: First input effecting second

Post by J_Mo »

Ok, so I have this

Code: Select all

class MyValidator extends CustomValidator
{
    function DoValidate(&$formars,&$error_hash)
    {
      if(strcmp($formars['Title'], "Mr") && strcmp($formars['Gender'], "Female"))
          {
            $error_hash['Title']="TEST";
            return false;
           } 
return true;
}
}
When I fill in Mr & Female in the fields Title & Gender respectively no error is returned??
J_Mo
Forum Newbie
Posts: 10
Joined: Mon Aug 09, 2010 7:02 pm

Re: Form validation: First input effecting second

Post by J_Mo »

Scrap that, I left the ==0 of the end.

Thanks for you help.
Phil
shawngoldw
Forum Contributor
Posts: 212
Joined: Mon Apr 05, 2010 3:38 pm

Re: Form validation: First input effecting second

Post by shawngoldw »

No problem, you put the == 0 after both strcmp functions right? Not just the last one? It wasn't clear from your last post.
J_Mo
Forum Newbie
Posts: 10
Joined: Mon Aug 09, 2010 7:02 pm

Re: Form validation: First input effecting second

Post by J_Mo »

Yes I did...thanks.

I wonder if you could help with a new problem, which I have no clue about! I have two date inputs, "Payday" & "Nextpayday". They are validated so the date displays like this: dd/mm/yyyy

The problem is that $formars['Payday'] cannot be greater than $formars['Nextpayday']

How can I achieve this? I will try myself but a little help would be well appreciated.
Thanks
Phil
J_Mo
Forum Newbie
Posts: 10
Joined: Mon Aug 09, 2010 7:02 pm

Re: Form validation: First input effecting second

Post by J_Mo »

I have this

Code: Select all

$date1 = "dd/mm/yyyy";
$date2 = "dd/mm/yyyy";

if((strtotime($date1['Payday']) - strtotime($date2['Nextpayday'])) < 0){
	$error_hash['Nextpayday']="Next payday must be later than payday'";
            return false;
}
but it isn't working
shawngoldw
Forum Contributor
Posts: 212
Joined: Mon Apr 05, 2010 3:38 pm

Re: Form validation: First input effecting second

Post by shawngoldw »

Is it because you define $date1 and $date2 but use $date1["payday"] and $date2["Nextpayday'], rather than just $date1 and $date2?
Post Reply