On my registration form I use preg match so people can not just enter random text to register, they have to enter in a email format. For example if you just entered "This is my email" it will reject it and show and error. The only problem is it doesn't show any error. It will reject it and it works like it is suppose to but it doesn't show the user that it is an invalid email like it is suppose to. All the other errors on the page works and I get no errors that nothing is wrong. Everything works like it is suppose to with the exception the error "Invalid Email" doesn't show when it rejects a non email format.

Here is the code I am referring to.

  }

    if (!preg_match("/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i",$product_name)||!mail($product_name)) 

      $error = "Invalid email";

    else {

Here is the full code.

<?php

// here, we check if the form has been submitted, because we need to handle
// redirection before we handle outputting the HTML stuff.
if (isset($_POST['submit']))
{
    if (empty($_POST['email']))
    {
        $error = 'Please fill in email field.';
    }
if (empty($_POST['password']))
{
$error = 'Please fill in desired password field.';
}
    }
    else
    {
        // MAKE CONNECTION
        include ('db_connect.php');

    // connect to the mysql server
    $link = mysql_connect($host, $username, $password) or die ("Could not connect to mysql because ".mysql_error());

    // select the database
    mysql_select_db($database) or die ("Could not select database because ".mysql_error());

    $error = "";
    $email = $_POST['email'];
    $pwd = $_POST['password'];

    // check if the email is taken (safe query):
    $query = sprintf("SELECT `email` FROM `users` WHERE `email` = '%s'",
            mysql_real_escape_string($_POST['email']));
    $qry = mysql_query($query) or die ("Could not match data because ".mysql_error());
    $num_rows = mysql_num_rows($qry);
    if ($num_rows < 1)
    {
        // Reverse magic_quotes_gpc/magic_quotes_sybase effects on those vars if ON.
        if(get_magic_quotes_gpc())
        {
            $product_name        = stripslashes($_POST['email']);
            $product_description = stripslashes($_POST['password']);
        }
        else
        {
            $product_name        = $_POST['email'];
            $product_description = $_POST['password'];
        }

    if (!preg_match("/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i",$product_name)||!mail($product_name)) 

      $error = "Invalid email";

    else {

        // Make a safe query
        $query = sprintf("INSERT INTO users (`email`, `password`) VALUES ('%s', '%s')",
                mysql_real_escape_string($email, $link),
                mysql_real_escape_string($password, $link));
        $result = mysql_query($query, $link);

        // If there is no result, or there was not at least 1 row affected, die...
        if(!$result || mysql_affected_rows() < 1)
        {
            $error = 'Could not insert user because ' . mysql_error();
        }
        else
        {
            // redirect them to the user account page, because we successfully ran the SQL
            // notice how we haven't output ANYTHING to the browser yet- header() works
            header('Location: user.php');
            exit();
        }
    }
    }
    else
    {
        $error = 'That email is already in use, please select a different one.';
    }

}



// If they've posted but there was an error, kindly show their email address for them again.
if(isset($_POST['email']))
    $email = $_POST['email'];
else
    $email = '';

?>

Any help would be great.

-Thanks

    If that is the full code, I don't see where you display any errors. All I see is you setting $error.

      $error is a variable so I can display the errors in general neatly in my HTML while keeping the php flow together. It is embedded in my HTML like below. That wouldn't have anything to do with why the code I posted isn't working

      <td colspan="2" align="right" class="errorText">
      <?PHP
      // then we check for the error message
      if (isset($error)) {
         echo $error . '<br />';
      }
      ?>
      </td>

        Ok I have got the error to show but now it won't go away as you can see here http://mesquitechristmas.com/local/register.php

        The code that produces this error is this.

        }
        
            if (!preg_match("/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i",$product_name)||!mail($product_name)) 
        
              $error = "Invalid email";
        
            else {

        It is suppose to show the error if you enter a non email format. For example "This is my email" instead of email@email.com It does work and reject no email formats but now the error will not go away. For the full code look at my first post.

        If anyone can help that would be great

        -Thanks

          What is "!mail($product_name) " supposed to do? Check that mail wasn't properly sent to this address?

          Clearly if your regexp is working, this may be your issue.

          EDIT: I guess what I'm saying is that it doesn't look like you have anything checking to see if $product_name is not blank. So, it is trying to send an email to a blank address... could be a problem! The only thing it is checking is is SUMBIT is set and that the EMAIL doesn't already exist.

            Another note: When I entered a bogus email, it stopped showing the "error"...

              Looks like you also have s syntax error:

              if (!preg_match("/[A-Z0-9._%+-]+@[A-Z0-9.-]+.[A-Z]{2,4}$/i",$product_name)||!mail($product_name))

              You need another parenthesis in the front of "(!"...

                I think this will work...

                <?php
                
                // here, we check if the form has been submitted, because we need to handle
                // redirection before we handle outputting the HTML stuff.
                if(isset($_POST['submit']))
                    {
                    if(empty($_POST['email']))
                        $error = 'Please fill in email field.';
                    elseif(empty($_POST['password']))
                        $error = 'Please fill in desired password field.';
                    else
                        {
                        // MAKE CONNECTION
                        include ('db_connect.php');
                
                    // connect to the mysql server
                    $link = mysql_connect($host, $username, $password) or die ("Could not connect to mysql because ".mysql_error());
                
                    // select the database
                    mysql_select_db($database) or die ("Could not select database because ".mysql_error());
                
                    $error = "";
                    $email = $_POST['email'];
                    $pwd = $_POST['password'];
                
                    // check if the email is taken (safe query):
                    $query = sprintf("SELECT `email` FROM `users` WHERE `email` = '%s'", mysql_real_escape_string($_POST['email']));
                    $qry = mysql_query($query) or die ("Could not match data because ".mysql_error());
                    $num_rows = mysql_num_rows($qry);
                    if ($num_rows < 1)
                       {
                        // Reverse magic_quotes_gpc/magic_quotes_sybase effects on those vars if ON.
                        if(get_magic_quotes_gpc())
                            {
                            $product_name        = stripslashes($_POST['email']);
                            $product_description = stripslashes($_POST['password']);
                            }
                        else
                            {
                            $product_name        = $_POST['email'];
                            $product_description = $_POST['password'];
                            }
                
                        if((!preg_match("/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i",$product_name)||!mail($product_name))
                          $error = "Invalid email";
                        else
                            {
                            // Make a safe query
                            $query = sprintf("INSERT INTO users (`email`, `password`) VALUES ('%s', '%s')", mysql_real_escape_string($email, $link), mysql_real_escape_string($password, $link));
                            $result = mysql_query($query, $link);
                
                            // If there is no result, or there was not at least 1 row affected, die...
                            if(!$result || mysql_affected_rows() < 1)
                                $error = 'Could not insert user because ' . mysql_error();
                            else
                                {
                                // redirect them to the user account page, because we successfully ran the SQL
                                header('Location: user.php');
                                exit();
                                }
                            }
                        }
                    else
                        $error = 'That email is already in use, please select a different one.';
                    }
                }
                
                
                
                // If they've posted but there was an error, kindly show their email address for them again.
                if(isset($_POST['email']))
                    $email = $_POST['email'];
                else
                    $email = '';
                
                ?>

                  Doesn't work, Parse error: syntax error, unexpected T_VARIABLE

                  Figured as much.

                  Next?

                    Did you search for the syntax error?

                    Looks like this statement might be the culprit:

                    if((!preg_match("/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i",$product_name)) ||!mail($product_name))

                      Something like this works for me maybe it will help you...

                      if (!eregi('^[_a-zA-Z0-9-]+[@][a-zA-Z0-9-]+[\.](com|net|org|edu|gov|biz|name|info){1}$', $email_address)) {
                      echo 'BAD';
                      }
                      else {
                      echo 'GOOD';
                      }
                      

                        Guys, I don't know how many times I have to say this over and over again and explain myself. Does anyone actually read these posts before they reply? Their is nothing wrong with the code itself. I just can not get the error to go away or display when someone tries to register with a non email. Bpat gave me the existing code I am using and it does what it is suppose to do with the exception of the error not going away or displaying when someone tries to register with a non email format.

                        Pretty simple here. You enter something other then text that is not in an email format display error. That is all I am needing help with. The code itself works, just not the error. Hopefully I do not have to explain this again.

                          Hopefully I won't have to explain myself - the code is, in fact, the problem.

                          Here is what you posted:

                          if (isset($_POST['submit']))
                              {
                              if (empty($_POST['email']))
                                  $error = 'Please fill in email field.';
                              if (empty($_POST['password']))
                                  $error = 'Please fill in desired password field.';
                              }
                              else
                              {
                             //Check the database for duplicate email
                              }

                          The problem is that if $_POST['submit'] is not set, then your code doesn't check the variables to see if they're empty, but does checks the database to see if the "email" exists. How can you check the "email" against the database if it hasn't been submitted yet?

                          You need to see if $_POST['submit'] is set and if so then check for the EMPTY variables and then check the database.

                          if(isset($_POST['submit']))
                              {
                              if(empty($_POST['email']))
                                  $error = 'Please fill in email field.';
                              elseif(empty($_POST['password']))
                                  $error = 'Please fill in desired password field.';
                              else
                                  {   
                          //Check the database for duplicate email } }[/

                          Who's not reading the responses?

                            Excuse me but please do not reply to my thread again. It is obvious you do not have any idea what you are talking about. Where before now have you ever mentioned that code above you posted before now?

                            Once again you are not reading or listening to what I am saying. My problem is not with it checking the email for duplicate entries. It does that just fine and why you are bring that up and what that has to do with the problem I AM having I have no clue. My problem is not with that bit of code.

                            I am not even going to bother to explain to you anymore.

                            Next?

                              I would expect this condition to result in an invalid email error every time, since the call to mail() will always fail due to the fact that it requires a minimum of 3 parameters.

                              if (!preg_match("/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i", $product_name) || !mail($product_name))
                                          $error = "Invalid email";
                              

                                Ok people I am going to say this one more time for the hundredth time since no one is listening.

                                This code right here

                                if (!preg_match("/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i", $product_name) || !mail($product_name)) 

                                ITS WORKS, IT WORKS, IT WORKS!!!!!

                                It works like a charm, when someone doesn't enter an email in an email format I.E. "This is my email" instead of putting email@email.com it will reject it. THAT IS WHAT ITS SUPPOSE TO DO and it DOES THAT.

                                WHAT IT IS NOT DOING.... is when it does reject a non email format it DOES NOT show the ERROR.

                                Other then that it works fine.

                                So what I am asking for help with is why is the ERROR NOT showing when it rejects a non email format?

                                -Thanks

                                  Dada78 wrote:

                                  Ok people I am going to say this one more time for the hundredth time since no one is listening.

                                  This code right here

                                  if (!preg_match("/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i", $product_name) || !mail($product_name)) 

                                  ITS WORKS, IT WORKS, IT WORKS!!!!!

                                  It works like a charm, when someone doesn't enter an email in an email format I.E. "This is my email" instead of putting email@email.com it will reject it. THAT IS WHAT ITS SUPPOSE TO DO and it DOES THAT.

                                  WHAT IT IS NOT DOING.... is when it does reject a non email format it DOES NOT show the ERROR.

                                  Other then that it works fine.

                                  So what I am asking for help with is why is the ERROR NOT showing when it rejects a non email format?

                                  -Thanks

                                  In post #4, you wrote:

                                  It is suppose to show the error if you enter a non email format. For example "This is my email" instead of email@email.com It does work and reject no email formats but now the error will not go away.

                                  That is what my reply just above your last rant was referring to. But now you seem to be contradicting that post, and saying that no error is being displayed. Which is it?

                                  If you want to continue to rant and rave because I did not read your mind some how and realize that that issue is not longer an issue and you are back to the original supposed issue of no error at all being output, then excuuuuuuuse me for living. And if you keep railing at everyone who makes an honest effort to help (out of his/her own good will and own time, mind you), then don't expect many people to be interested in trying to help you any more.

                                    Had you read the entire thread you would see the "rant" is and was not generally directed to you. If you want to think it was then that is you problem.

                                    My last line of my last post

                                    "So what I am asking for help with is why is the ERROR NOT showing when it rejects a non email format?"

                                    What you quote from post 4

                                    It is suppose to show the error if you enter a non email format.

                                    So had you read it and understood it then you would have understood the problem. No mind reading involved. If you didn't understand the problem I was having you shouldn't have responded. I didn't twist your arm for you to reply to me nor did I complain. If I take the time to give a detailed explanation of the problem then I expect the same consideration to at least read the entire problem and comprehend it before replying .

                                    Doesn't matter I have seem to fix it now no help to anyone here.