TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 02-14-2008, 03:39 PM   #1 (permalink)
The Contributor
 
Join Date: Jan 2008
Posts: 28
Thanks: 9
bmathers is on a distinguished road
Default If, else and ==

Hi, im pretty new to php, ive got some code see below from a contact form, but i need to match the email and confirm email fields, how can i do that.

php Code:
if(isValidForm($strTempName, $strTempTelephone, $strTempMobile, $strTempEmail, $strTempConfirmEmail, $strTempCity))

{
    //Mail the form.
    //Call MaiForm Function.
    MailForm($strTempName, $strTempAddress, $strTempPostcode, $strTempTelephone, $strTempMobile, $strTempEmail, $strTempConfirmEmail, $strTempCity, $strTempCelebration, $strTempStartDate, $strTempGroupSize, $strTempEnquiry, $strTempBrochure, $strTempHeardAbout) ;

    //Write out a thank you.
    //WriteMessage($strTempName, $strTempAddress, $strTempPostcode, $strTempTelephone, $strTempMobile, $strTempEmail, $strTempCity, $strTempCelebration, $strTempStartDate, $strTempGroupSize, $strTempEnquiry, $strTempBrochure, $strTempHeardAbout) ;
    echo "Thank you for your enquiry, a member of our reservation team will get back to you within 24 hours.";
}
else
{
    //Send an Error.
    echo "<div align=center" ;
    echo "It appears that you have left out one of the fields.<br>" ;
    echo "Please go back and fill in these missing fields.<br><br>" ;
    //Display the error.
    echo $strError ;

}


There is more to the code but i think that is all of the relevant code.

Thanks in advance

Last edited by Wildhoney : 02-14-2008 at 04:42 PM.
bmathers is offline  
Reply With Quote
Old 02-14-2008, 03:44 PM   #2 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

Inside the isValidForm function you just need to compare the two values. To check for equality just do: if ($strTempEmail == $strTempConfirmEmail) ...
Salathe is offline  
Reply With Quote
The Following User Says Thank You to Salathe For This Useful Post:
bmathers (02-14-2008)
Old 02-14-2008, 04:02 PM   #3 (permalink)
The Contributor
 
Join Date: Jan 2008
Posts: 28
Thanks: 9
bmathers is on a distinguished road
Default

ithought that was the case but im still having trouble, im getting this error now

Quote:
Parse error: syntax error, unexpected T_IS_EQUAL, expecting ')' in /home/sites/planetredevents.co.uk/public_html/demo/process.php on line 93

this is line 93 and its function;

php Code:
function isValidForm($strTempName, $strTempTelephone, $strTempMobile, $strTempEmail, $strTempConfirmEmail, $strTempCity, $strTempEmail == $strTempConfirmEmail)
{
    //Set strError Variable to global so we can use the Variable
    //outside of the function.
    global $strError ;
   
    //Initiate the variable by setting it eqaul to nothing.
    $strError = "" ;

    if($strTempName == "")
    {
        //Build the string error message by Cocantenating the strError Variable
        //to itself.
        $strError .= "Name field.<br>" ;
    }


    if($strTempTelephone == "")
    {
        //Build the string error message by Cocantenating the strError Variable
        //to itself.
        $strError .= "Telephone field.<br>" ;
        }
       
    if($strTempMobile == "")
    {
        //Build the string error message by Cocantenating the strError Variable
        //to itself.
        $strError .= "Mobile field.<br>" ;
        }
       
    if($strTempEmail == "")
    {
        //Build the string error message by Cocantenating the strError Variable
        //to itself.
        $strError .= "Email field.<br>" ;
        }
       
    if($strTempConfirmEmail == "")
    {
        //Build the string error message by Cocantenating the strError Variable
        //to itself.
        $strError .= "Confirm Email field.<br>" ;
        }
       
     if($strTempCity == "")
    {
        //Build the string error message by Cocantenating the strError Variable
        //to itself.
        $strError .= "Chosen City field.<br>" ;
        }
       
         if($strTempEmail != $strTempConfirmEmail)
    {
        //Build the string error message by Cocantenating the strError Variable
        //to itself.
        $strError .= "Email does not match.<br>" ;
        }
    //Test to see if the strError Variable contains a message
    //If it does then return false (not an valid form).
    //If strError Variable is empty or "" then return true (is a valid form).
    if($strError != "")
    {
        return false ;
    }
    else
    {
        return true ;
    }
}

Last edited by Wildhoney : 02-14-2008 at 04:42 PM.
bmathers is offline  
Reply With Quote
Old 02-14-2008, 04:14 PM   #4 (permalink)
Nor
The Addict
 
Join Date: Nov 2007
Posts: 282
Thanks: 61
Nor is on a distinguished road
Default

Read below not thinking straight
----

Reason is, you can't compare variables inside function arguments.
__________________
PHP/XHTML Freelancer:
Cleanscript.com v3 - Programming starting at just $5 act now!

Last edited by Nor : 02-14-2008 at 04:50 PM. Reason: thx sketch for clearning it up typing to fast will get to me.
Nor is offline  
Reply With Quote
Old 02-14-2008, 04:27 PM   #5 (permalink)
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
sketchMedia is on a distinguished road
Default

you cant have comparisons in the functions args, i believe salathe meant inside your function definition like so:

PHP Code:
function isValidForm($strTempName$strTempTelephone$strTempMobile$strTempEmail$strTempConfirmEmail$strTempCity$strTempEmail$strTempConfirmEmail)
{
    if(
$strTempEmail == $strTempConfirmEmail)
    { 
        
//etc
    
}

__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)

Last edited by sketchMedia : 02-14-2008 at 04:35 PM. Reason: fingers are not connected to my brain atm :(
sketchMedia is offline  
Reply With Quote
The Following User Says Thank You to sketchMedia For This Useful Post:
bmathers (02-14-2008)
Old 02-14-2008, 05:21 PM   #6 (permalink)
The Contributor
 
Join Date: Jan 2008
Posts: 28
Thanks: 9
bmathers is on a distinguished road
Default

cheers guys, sorry for being a fool.

Ive got it now, was trying to rush it and not thinking about it
bmathers is offline  
Reply With Quote
Old 02-15-2008, 12:25 PM   #7 (permalink)
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
sketchMedia is on a distinguished road
Default

hehe np m8, all part of the learning process
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
sketchMedia is offline  
Reply With Quote
Old 02-15-2008, 02:13 PM   #8 (permalink)
The Frequenter
Zend Certified 
 
Join Date: Sep 2007
Location: Denmark
Posts: 352
Thanks: 8
Kalle is on a distinguished road
Default

You might want to use empty() to check if a string is empty, it will consider 0 as empty though.

Else its looking good ;)
Send a message via MSN to Kalle Send a message via Skype™ to Kalle
Kalle is offline  
Reply With Quote
Old 02-15-2008, 02:21 PM   #9 (permalink)
The Frequenter
 
ReSpawN's Avatar
 
Join Date: Nov 2007
Location: Netherlands
Posts: 460
Thanks: 49
ReSpawN is on a distinguished road
Default

Quote:
Originally Posted by Kalle View Post
You might want to use empty() to check if a string is empty, it will consider 0 as empty though.

Else its looking good ;)
Why use a tremendous slow function (if used too much) if you can use if ($var == '') { continue; }.
__________________
"Life is a bitch, take that bitch on a ride"
Send a message via MSN to ReSpawN
ReSpawN is offline  
Reply With Quote
Old 02-15-2008, 07:26 PM   #10 (permalink)
The Frequenter
Newcomer 
 
xenon's Avatar
 
Join Date: Dec 2007
Location: Bucharest, Romania
Posts: 438
Thanks: 3
xenon is on a distinguished road
Default

Quote:
Originally Posted by ReSpawN View Post
Why use a tremendous slow function (if used too much) if you can use if ($var == '') { continue; }.
Because empty doesn't check only if the argument is 0 (zero), it checks for other empty values, as well. This would be the rewriting of the empty function:

php Code:
function custom_empty( $val )
{
    if( $val === '' ||
        $val === 0 ||
        $val === '0' ||
        $val === null ||
        $val === false ||
        $val === array() )
    {
        return true;
    }
   
    return false;
}

So...why is it a so 'tremendous slow function'?
__________________
I have optimistic thoughts, even though sometimes (if not always) life's a bitch.

Last edited by Wildhoney : 02-15-2008 at 08:20 PM.
xenon is offline  
Reply With Quote
Old 02-15-2008, 07:58 PM   #11 (permalink)
The Frequenter
 
ReSpawN's Avatar
 
Join Date: Nov 2007
Location: Netherlands
Posts: 460
Thanks: 49
ReSpawN is on a distinguished road
Default

Quote:
Originally Posted by xenon View Post
Because empty doesn't check only if the argument is 0 (zero), it checks for other empty values, as well. This would be the rewriting of the empty function:

Code:
function custom_empty( $val )
{
    if( $val === '' ||
        $val === 0 ||
        $val === '0' ||
        $val === null ||
        $val === false ||
        $val === array() )
    {
        return true;
    }
    
    return false;
}
So...why is it a so 'tremendous slow function'?
I did not mean the == (why 3?) operator was slow, I meant empty() was slow. And next to that, like you said, it doesn't check all sorts of vars and arrays. You can check if the var contains something but it won't work on an array.
__________________
"Life is a bitch, take that bitch on a ride"
Send a message via MSN to ReSpawN
ReSpawN is offline  
Reply With Quote
Old 02-15-2008, 10:14 PM   #12 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

Who says that empty() is slow and on what grounds? Also, empty does check all sorts of vars and arrays which basic comparison will not do. In the end it's about knowing what method to use for what circumstances.

P.S. The three equals symbols makes sure that the values are IDENTICAL. In other words, the comparison evaluates to TRUE if the left side is equal to the right, and they are both of the same type (string, integer, etc). For example:
PHP Code:
if (== 0.00) {
    echo 
'0 is equal to 0.00';
} else {
    echo 
'0 is not equal to 0.00';
}

if (
=== 0.00) {
    echo 
'and 0 is identical to 0.00';
} else {
    echo 
'but 0 is not identical to 0.00';

Salathe is offline  
Reply With Quote
The Following User Says Thank You to Salathe For This Useful Post:
ReSpawN (02-15-2008)
Old 02-15-2008, 11:01 PM   #13 (permalink)
The Frequenter
 
ReSpawN's Avatar
 
Join Date: Nov 2007
Location: Netherlands
Posts: 460
Thanks: 49
ReSpawN is on a distinguished road
Default

Thanks Salathe, yet again you proved me wrong. I heard about the empty() function being slow but you've proven me right, since I sincerely thought the other guy was right.

About the double and triply =, thanks for that. I didn't know that either. Learning a lot.
__________________
"Life is a bitch, take that bitch on a ride"
Send a message via MSN to ReSpawN
ReSpawN is offline  
Reply With Quote
Old 02-16-2008, 03:04 PM   #14 (permalink)
Alan @ CIT
Member of the Month
The Frequenter
Member of the Month Top Contributor 
 
Alan @ CIT's Avatar
 
Join Date: Apr 2005
Location: South UK
Posts: 483
Thanks: 51
Alan @ CIT is on a distinguished road
Default

Something that I find handy - BlueShoes: PHP Cheat Sheet - a little cheat sheet that helps you figure out what will match what when writing comparisons

Alan
Send a message via MSN to Alan @ CIT
Alan @ CIT is offline  
Reply With Quote
The Following User Says Thank You to Alan @ CIT For This Useful Post:
ReSpawN (02-16-2008)
Old 02-16-2008, 03:39 PM   #15 (permalink)
The Frequenter
 
ReSpawN's Avatar
 
Join Date: Nov 2007
Location: Netherlands
Posts: 460
Thanks: 49
ReSpawN is on a distinguished road
Default

Thanks Alan, that's awesome. Seriously.
__________________
"Life is a bitch, take that bitch on a ride"
Send a message via MSN to ReSpawN
ReSpawN is offline  
Reply With Quote
Old 02-18-2008, 02:11 PM   #16 (permalink)
The Frequenter
Zend Certified 
 
Join Date: Sep 2007
Location: Denmark
Posts: 352
Thanks: 8
Kalle is on a distinguished road
Default

Quote:
Originally Posted by ReSpawN View Post
Thanks Salathe, yet again you proved me wrong. I heard about the empty() function being slow but you've proven me right, since I sincerely thought the other guy was right.

About the double and triply =, thanks for that. I didn't know that either. Learning a lot.
To quick learn you one more thing about empty, its a language construct and not a function, meaning it cannot be called though variable functions if you are know that term =)
Send a message via MSN to Kalle Send a message via Skype™ to Kalle
Kalle is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 01:02 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design