Click here to Skip to main content
15,894,896 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
PHP
function checkEmail ($_POST['email'])
	{
	$possible = "/^[a-z]$/i";
	if ($_POST['email'] >= 8)
		{
		if (preg_match ($possible, $_POST['email']))
			{
			return true;
			}
		else
			{
			return false;
			}
		}
	else 
		{
		return false;
		}
	}


I just get the following Error: Fatal error: Cannot re-assign auto-global variable $ _POST in /homepages/... in line 1
If I dont use the part "$_POST['email']" in "function checkEmail ()" it always return false if i have 8 or more chars from a to z but it should return true.
Where is my mistake? And how can I add @ and . to $possible?
Posted

1 solution

Simply put, you shouldn't be using $_POST in the function signature(or body) - you should be using it in the place that you call your function.

PHP
function checkEmail($emailAdr)
{
    $possible = "/^[a-z]$/i";

/* unsure about this line, your intention is unclear - i think you want the email address to be at least 8 characters. */
    if (strlen($emailAdr) >= 8)
    {
        if (preg_match ($possible, $emailAdr))
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    else 
    {
        return false;
    }
}


Followed by the call to the function:
PHP
$emailValid = checkEmail($_POST['email']);
 
Share this answer
 
Comments
Chi Ller 9-Sep-12 4:44am    
Now it is a form and i get all entries with a $_POST variable. So how can I get the content of $_POST['email'] for do the function with the variable $email?

Yes you´re right. I want tho check the email to be 8 characters
enhzflep 9-Sep-12 8:58am    
Okay sure. Er, ahrm - have a closer look at my previous post. Perhaps this will be more explicit:

$enteredEmail = $_POST['email'];
$emailValid = checkEmail($enteredEmail);
Chi Ller 9-Sep-12 11:30am    
Yes but the next problem is preg_match. Cause of it I always get false.
for example:
$enteredEmail is testmailhere (12 letters, just letters from a-z) and preg_match returns false
enhzflep 9-Sep-12 11:53am    
Yes I see that, the problem is with the regex you're using. Simply put, it's just wrong. I got put onto Expresso by OriginalGriff a while ago for working with RegExps - it's well worth the download.

I just grabbed the expression for email validation from it, it a _little_ longer than yours was. :grin:

Just drop this line into your code and it will work:


$possible = "/([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})/";
Chi Ller 9-Sep-12 12:18pm    
sorry but it also doesn´t work :S

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900