1

After spending hours I still cant figure it out. I'm trying to validate an email address but every time and no matter what Ii type it gives me the message as if it was empty "You did not enter an email address!".

Edit: Fixed invalid email address error because I did not add a name to my input text. Now when I enter a correct email, it keeps loading "please wait". Any ideas? Is it because its not connecting to mysql?

$email = mysql_real_escape_string($_POST['signup-email']);

if(empty($email)){
     $status = "error";
     $message = "You did not enter an email address!";
}
else if(!preg_match('/^[^\W][a-zA-Z0-9_]+(\.[a-zA-Z0-9_]+)*\@[a-zA-Z0-9_]+(\.[a-zA-        Z0-9_]+)*\.[a-zA-Z]{2,4}$/', $email)){ //validate email address - check if is a valid email     address
        $status = "error";
        $message = "You have entered an invalid email address!";
}
else {
    $existingSignup = mysql_query("SELECT * FROM signups WHERE signup_email_address='$email'");   
    if(mysql_num_rows($existingSignup) < 1){

        $date = date('Y-m-d');
        $time = date('H:i:s');

        $insertSignup = mysql_query("INSERT INTO signups (signup_email_address, signup_date, signup_time) VALUES ('$email','$date','$time')");
        if($insertSignup){ //if insert is successful
            $status = "success";
            $message = "You have been signed up!";  
        }
        else { //if insert fails
            $status = "error";
            $message = "Ooops, Theres been a technical error!"; 
        }
    }
    else { //if already signed up
        $status = "error";
        $message = "This email address has already been registered!";
    }
}

HTML:

<div id="signupform">
<form id="newsletter-signup" action="?action=signup" method="post">  
     <fieldset>  
        <input type="text" name="signup-email" id="signup-email" size="20" value="Email Address" onfocus="if(this.value==this.defaultValue)this.value='';" onblur="if(this.value=='')this.value=this.defaultValue;"/>
        <input type="submit" id="signup-button" value="+"/>
        </br></br><p id="signup-response"></p>  
     </fieldset>  
</form>

Art
  • 17
  • 2
  • 7
  • 5
    Please share the code where you define `$email`. – Willem Nov 22 '11 at 17:14
  • 2
    http://ex-parrot.com/~pdw/Mail-RFC822-Address.html This is the only correct e-mail RegEx. – Brad Nov 22 '11 at 17:17
  • "name" is a required attribute on input elements, and is necessary to get the parameter in the request. To elaborate: id="signup-email" is useless in a form context (still useful for CSS of course). – Viruzzo Nov 22 '11 at 17:17
  • http://stackoverflow.com/questions/201323/what-is-the-best-regular-expression-for-validating-email-addresses – Brad Nov 22 '11 at 17:17
  • forgot to post up define $email. edited – Art Nov 22 '11 at 17:33

4 Answers4

2
if(strlen($_POST['signup-email'])) == 0){
  $status = "error";
  $message = "You did not enter an email address!";
}

and then give your post variable a name:

<input type="text" name='signup-email' id="signup-email" size="20" value="Email Address" onfocus="if(this.value==this.defaultValue)this.value='';" onblur="if(this.value=='')this.value=this.defaultValue;"/>
abcde123483
  • 3,885
  • 4
  • 41
  • 41
  • Ok it was because i did not give my post variable a name. but now when i type in a valid email address. it displays Please wait and stays there forever. – Art Nov 22 '11 at 17:41
  • We better handle that in a new question and not in the comments :-) – abcde123483 Nov 22 '11 at 17:46
0

You don't have a field named email in your form...

Also you should do: if(!isset($_POST['email'])) {

If I understand you correctly (unless you haven't posted all code).

PeeHaa
  • 71,436
  • 58
  • 190
  • 262
0

Try adding name="email" to the email input's html... Also, $email should be defined something like $email = $_POST['email'];

deviousdodo
  • 9,177
  • 2
  • 29
  • 34
0

Your input fields do not have names, PHP retrieves $_POST variables from the name attribute of the input. So in other words, no name, no access to that input's value. Take draevor's advice and add the name attribute to your inputs. Also you have a large amount of spaces in your regex between [a-zA- and Z0-9_]

'/^[^\W][a-zA-Z0-9_]+(\.[a-zA-Z0-9_]+)*\@[a-zA-Z0-9_]+(\.[a-zA- Z0-9_]+)*\.[a-zA-Z]{2,4}$/'

Not sure if that's from copying and pasting, but if those spaces are in your source code it may cause the regex not to work. I've never heard of an "ignore whitespace" flag for the preg_match() function, correct me if I'm wrong.

k4t434sis
  • 519
  • 4
  • 17