Form validation: First input effecting second
Moderator: General Moderators
Form validation: First input effecting second
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
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
Code: Select all
if(strcmp($formar['title'], "Mr") == 0)
{
$formar['Gender'] = "Male";
}Shawn
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
Re: Form validation: First input effecting second
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';
}Re: Form validation: First input effecting second
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!!
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
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
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
Re: Form validation: First input effecting second
Ok, so I have this
When I fill in Mr & Female in the fields Title & Gender respectively no error is returned??
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;
}
}
Re: Form validation: First input effecting second
Scrap that, I left the ==0 of the end.
Thanks for you help.
Phil
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
No problem, you put the == 0 after both strcmp functions right? Not just the last one? It wasn't clear from your last post.
Re: Form validation: First input effecting second
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
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
Re: Form validation: First input effecting second
I have this
but it isn't working
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;
}-
shawngoldw
- Forum Contributor
- Posts: 212
- Joined: Mon Apr 05, 2010 3:38 pm
Re: Form validation: First input effecting second
Is it because you define $date1 and $date2 but use $date1["payday"] and $date2["Nextpayday'], rather than just $date1 and $date2?