Welcome Guest!

If you do not have an account yet on The Web Squeeze forums, please Register! It’s FREE and there are many benefits:

  • Receive Fast Advice
  • Learn Programming Languages
  • Get Professional Website Reviews
  • Quick Troubleshooting Assistance

> Simple Form / If Statement Help

This is a discussion on Simple Form / If Statement Help, within the PHP section. This forum and the thread "Simple Form / If Statement Help" are both part of the Programming Your Website category.

 
Reply to this topicStart new topic
> Simple Form / If Statement Help
edd
post Apr 2 2008, 06:05 AM
Post #1


Fresh Squeezed
**

Group: Members
Posts: 15
Joined: 18-March 08
Member No.: 203



I've got a very simple problem, I just can't figure out how to write it correcty. I want to test for inputted text and also an "@" symbol in the email field. Here's the code I've tried:

if(!empty($_POST['name']) &&
($_POST['email']) &&
($_POST['phone']))
and (strstr( $email, "@"))
{

But I'm getting the error:

Parse error: syntax error, unexpected T_LOGICAL_AND

I'm sure it's very simple to fix, any idea guys?

Cheers thank_you2.gif
Go to the top of the page
 
+Quote Post
Jason
post Apr 2 2008, 07:07 AM
Post #2


Master of the Universe
******

Group: Advisors
Posts: 1,157
Joined: 15-February 08
From: London, England
Member No.: 141



Why is there an 'and' instead of a &&?

A better way to check for an email is as follows.

CODE
$email = $_POST['email'];

if (all fields exist)
{
   if(preg_match("/^[a-z0-9\å\ä\ö._-]+@[a-z0-9\å\ä\ö.-]+\.[a-z]{2,6}$/i", $email)) {
       // Continue
   }
   else
   {
       print 'The e-mail is invalid';
   }
}
else
{
   print 'The form is incomplete';
}


--------------------
Go to the top of the page
 
+Quote Post
Rakuli
post Apr 2 2008, 07:08 AM
Post #3


Squeeze Machine
*****

Group: Administrators
Posts: 649
Joined: 13-February 08
From: Catching the squeezed drips downunder.
Member No.: 13



There's a problem in your brackets and logic

try:
CODE
if(!empty($_POST['name']) && ($_POST['email'] && strstr($_POST['email'], '@')) &&  !empty($_POST['phone']))
{
}


--------------------
Luke Dingle . com

Turn Over a Playful Leaf on Web Design -- read about the javascript cat
Go to the top of the page
 
+Quote Post
edd
post Apr 2 2008, 07:56 AM
Post #4


Fresh Squeezed
**

Group: Members
Posts: 15
Joined: 18-March 08
Member No.: 203



Thanks for the replies.

Jason, my php is basic and I don't really understand what any of that means, and I prefer not to use code I don't understand.

Rakuli, I get a syntax error with your code on this line:

strstr($_POST['email']), '@') &&

Any idea why?
Go to the top of the page
 
+Quote Post
Rakuli
post Apr 2 2008, 08:28 AM
Post #5


Squeeze Machine
*****

Group: Administrators
Posts: 649
Joined: 13-February 08
From: Catching the squeezed drips downunder.
Member No.: 13



Woops sorry mate, have amended my above post


--------------------
Luke Dingle . com

Turn Over a Playful Leaf on Web Design -- read about the javascript cat
Go to the top of the page
 
+Quote Post
Jason
post Apr 2 2008, 08:31 AM
Post #6


Master of the Universe
******

Group: Advisors
Posts: 1,157
Joined: 15-February 08
From: London, England
Member No.: 141



pregmatch() looks for for something within a string.

CODE
"/^[a-z0-9\å\ä\ö._-]+@[a-z0-9\å\ä\ö.-]+\.[a-z]{2,6}$/i"


This is a regular expression. Regular expressions is a syntax used to find character combinations. They are complicated but I will try to break it down for you.

Take the email's someone@gmail.com and dave@hotmail.co.uk

/^ - Start of string
[a-z0-9\å\ä\ö._-] - Look for any valid character's. a-z, 0-9, as well as the other characters specified. (someone, dave)

+@[a-z0-9\å\ä\ö.-] - Look for the @ sign and any other valid characters. (@gmail, @hotmail)

+\.[a-z]{2,6}$/i - Add a '.' then any a-z characters. The {2,6} specifies that this should be between 2-6 characters long. (.com, .co.uk)

So basically this confirms that the e-mail address contains.. [something]@[something].[something which is 2-6 characters long]

You are currently looking for an @ sign. This means that if someone just enters @ it will validate. This is no good.

There is a cheat sheet for regular expressions here.
http://www.ilovejackdaniels.com/regular_ex...cheat_sheet.png


--------------------
Go to the top of the page
 
+Quote Post
c010depunkk
post Apr 2 2008, 12:51 PM
Post #7


Rapid Squeezer
****

Group: Advisors
Posts: 178
Joined: 14-February 08
From: Willich, Germany
Member No.: 56



Just a little encouragement for "PHP beginners".... Don't let regular expressions scare you off. I know they look like big scary, cryptic, jumbled runes, but investing a little time and effort to learn the basics is worth it!!! REGEX is a very powerful tool, and not only implemented in PHP.

This site has a very nice tutorial and some simple examples: http://www.regular-expressions.info/

And I can't resist sharing this: http://xkcd.com/208/


--------------------
www.c010depunkk.com ~ the hangout of a web developer
Go to the top of the page
 
+Quote Post
Jason
post Apr 2 2008, 01:12 PM
Post #8


Master of the Universe
******

Group: Advisors
Posts: 1,157
Joined: 15-February 08
From: London, England
Member No.: 141



QUOTE (c010depunkk @ Apr 2 2008, 06:51 PM) *
And I can't resist sharing this: http://xkcd.com/208/


That just made the geeky side of me so happy. Great link i-m_so_happy.gif


--------------------
Go to the top of the page
 
+Quote Post
Itsumishi
post May 13 2008, 08:48 PM
Post #9


Squeezing
***

Group: Members
Posts: 78
Joined: 5-March 08
From: Melbourne, Australia
Member No.: 195



QUOTE (Jason @ Apr 2 2008, 11:31 PM) *
So basically this confirms that the e-mail address contains.. [something]@[something].[something which is 2-6 characters long]


This doesn't seem the best idea to me. What if the person has the email address.

studentname@student.rmit.edu.au

Which just so you know is how most of the Universities in Australia have their email addresses.

yes the studentname@student. part will validate however the rmit.edu.au is certainly longer than 2-6 characters. I'd make sure it can accept at least 2-20 characters. Remember that rmit is the uni name and it might be a fair bit longer.

This post has been edited by Itsumishi: May 13 2008, 08:51 PM
Go to the top of the page
 
+Quote Post
c010depunkk
post May 14 2008, 01:11 AM
Post #10


Rapid Squeezer
****

Group: Advisors
Posts: 178
Joined: 14-February 08
From: Willich, Germany
Member No.: 56



This is the regex i use for validating emails:
CODE
/^([A-Z0-9._%-]+)@([A-Z0-9.-]+)\.([A-Z]{2,6})$/i

would also match "studentname@student.rmit.edu.au"

and, btw, there isn't a perfect regex for validating emails..... the only failsafe way is to query the server is this address exists and that is usually way too complicated


--------------------
www.c010depunkk.com ~ the hangout of a web developer
Go to the top of the page
 
+Quote Post
Antti
post May 14 2008, 02:03 AM
Post #11


Rapid Squeezer
****

Group: Advisors
Posts: 275
Joined: 15-February 08
From: Finland
Member No.: 139



How about requiring email confirmation? If I have to be sure that the email is "real", I send a confirmation email to that address and the user needs to validate it by clicking the link on that email. Only after that I accept the new email address. I think that's used pretty much so users can expect it and it's fairly easy to do.


--------------------
anttisimonen.com - .NET developer

anttis.wordpress.com - Blog
Go to the top of the page
 
+Quote Post
c010depunkk
post May 14 2008, 05:38 AM
Post #12


Rapid Squeezer
****

Group: Advisors
Posts: 178
Joined: 14-February 08
From: Willich, Germany
Member No.: 56



Yep, also a good idea.... But a quick email verification before allowing the form to submit prevents people or bots from spamming with "asdf...." It just makes sure that the characters they entered are a valid address.....


--------------------
www.c010depunkk.com ~ the hangout of a web developer
Go to the top of the page
 
+Quote Post
Antti
post May 14 2008, 06:23 AM
Post #13


Rapid Squeezer
****

Group: Advisors
Posts: 275
Joined: 15-February 08
From: Finland
Member No.: 139



If you want to prevent spambots from using the form then there are other solutions available, for example this one which is also used by Twitter.


--------------------
anttisimonen.com - .NET developer

anttis.wordpress.com - Blog
Go to the top of the page
 
+Quote Post
c010depunkk
post May 15 2008, 01:43 AM
Post #14


Rapid Squeezer
****

Group: Advisors
Posts: 178
Joined: 14-February 08
From: Willich, Germany
Member No.: 56



I'm not talking about spam bots... I'm talking about real people who don't even bother to enter an email address with the correct format.... I know this isn't much, but it's just one more precaution one can take....


--------------------
www.c010depunkk.com ~ the hangout of a web developer
Go to the top of the page
 
+Quote Post
Antti
post May 15 2008, 01:59 AM
Post #15


Rapid Squeezer
****

Group: Advisors
Posts: 275
Joined: 15-February 08
From: Finland
Member No.: 139



Ok, Then you propably don't have any other real solution than just use regex to match the email. Email confirmation is not good in this situation. Better with user accounts.


--------------------
anttisimonen.com - .NET developer

anttis.wordpress.com - Blog
Go to the top of the page
 
+Quote Post
If you found The Web Squeeze to be helpful, please donate so we can keep this site FREE, FRESH, and fortified with Web Design & Development info!
Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

Collapse

> Similar Topics

    Topic Title Replies Topic Starter Views Last Action
No New Posts   1 Daniela 372 15th February 2008 - 06:58 AM
Last post by: craig
No New Posts 12 xkatx21x 530 28th February 2008 - 11:41 PM
Last post by: Rakuli
No New Posts   4 thesealportalteam 352 20th February 2008 - 09:00 AM
Last post by: thesealportalteam
No new   14 902 479 22nd February 2008 - 02:45 AM
Last post by: c010depunkk
No New Posts   5 thesealportalteam 250 22nd February 2008 - 11:48 PM
Last post by: Monie