www.webdeveloper.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 21

Thread: Email validation on submit

  1. #1
    Join Date
    Jun 2011
    Posts
    141

    Email validation on submit

    I have a form on my home page for people to subscribe to a mailing list, this works and the content of my email address field gets stored correctly within my sqlsrv database.

    Now I am trying to add email validation so have copied a bit of code from my registration script but this doesn't work and still allows me to just submit "test".

    The code is as follows:

    I have added this function just under the database connection details:

    Code:
    function isEmail($email){
      return preg_match('/^\S+@[\w\d.-]{2,}\.[\w]{2,6}$/iU', $email) ? TRUE : FALSE;
    }
    I have then added the following in the process of what happens when submit is clicked:

    Code:
    // Validate Email
    if(!isEmail($data['emailAD'])) {
    $err[] = "ERROR - Invalid email address.";
    //header("Location: index.php?msg=$err");
    //exit();
    }
    And finally, just under my subscribe box on the home page I have inserted the following to display the errors:

    Code:
    <?php	
    	 if(!empty($err))  {
    	   echo "<div class=\"msg\">";
    	  foreach ($err as $e) {
    	    echo "* $e <br>";
    	    }
    	  echo "</div>";	
    	   }
    	 ?>
    Thanks

  2. #2
    Join Date
    Apr 2012
    Posts
    15
    The problem is definitively not in the parts of code you provided. Everything is correct there. The function isEmail is probably called in the wrong place or time. You should provide more code to locate the problem

  3. #3
    Join Date
    Jun 2011
    Posts
    141
    OK, my input within index.php is:

    Code:
    <input type="text" name="emailAD">
    on submit, the action is within emailsubscribe.php and the full content of this file is:

    Code:
    <?php
    
    include("sqlsrvdbc.php");
    
    $emailAD=$_POST['emailAD'];
    
    include ("connection.php");
    
    // Validate Email
    if(!isEmail($data['emailAD'])) {
    $err[] = "ERROR - Invalid email address.";
    //header("Location: index.php?msg=$err");
    //exit();
    }
    
    header("Location: subscribed.php");
    
    $query="INSERT INTO enquiries (EmailAddress, DateTimeEntered) VALUES ('$emailAD',GetDate())";
    
    $result=sqlsrv_query($conn, $query);
    
    if (!$result) {
    	echo $result;
    	die("Query failed");
    
    	} else {
    
    	echo "Success";
    	} 
    	
    sqlsrv_free_stmt( $result);
    
    sqlsrv_close($conn);;
    
    ?>
    and the sqlsrvdbc.php include contains:

    Code:
    <?php
    
    $server="#"; // set database host
    $username="#"; // set database user
    $password="#"; // set database password
    $database="#"; // set database name
    
    function isEmail($emailAD){
      return preg_match('/^\S+@[\w\d.-]{2,}\.[\w]{2,6}$/iU', $emailAD) ? TRUE : FALSE;
    }
    
    ?>
    Thanks

  4. #4
    Join Date
    Apr 2012
    Posts
    15
    Ok, this line is clear, you just create a variable $emailAD which stores the email from the input
    PHP Code:
    $emailAD=$_POST['emailAD']; 
    And here you validate the email. Where is that array $data taken from? If it's full code of the file then that array may simply not exist
    PHP Code:
    if(!isEmail($data['emailAD'])) { 
    And if you actually created a variable which stores the email, why don't you use it like this?
    PHP Code:
    if(!isEmail($emailAD)) { 

  5. #5
    Join Date
    Jun 2011
    Posts
    141
    Code:
    $emailAD=$_POST['emailAD'];
    was just for the benefit of
    Code:
    $query="INSERT INTO enquiries (EmailAddress, DateTimeEntered) VALUES ('$emailAD',GetDate())";
    but yes I agree that I could change
    Code:
    if(!isEmail($data['emailAD'])) {
    to
    Code:
    if(!isEmail($emailAD)) {
    And i'm not too sure what the $data is from, I just copied this bit of code from my registration page, not sure if it has anything to do with the following which I just found within an include from the registration page, not sure what this is doing though:

    Code:
    function filter($data) {
    	$data = trim(htmlentities(strip_tags($data)));
    	
    	if (get_magic_quotes_gpc())
    		$data = stripslashes($data);
    	
    	$data = mysql_real_escape_string($data);
    	
    	return $data;
    }

  6. #6
    Join Date
    Apr 2012
    Posts
    15
    try this code and tell me the output you get

    PHP Code:
    <?php

    include("sqlsrvdbc.php");

    $emailAD=$_POST['emailAD'];

    include (
    "connection.php");

    echo 
    '<pre>';
    print_r($data);
    echo 
    '</pre>';

    // Validate Email
    if(!isEmail($emailAD) {
    $err[] = "ERROR - Invalid email address.";
    echo 
    'Email ' $emailAD ' is wrong';
    }else{
    echo 
    'Email ' $emailAD ' is correct';
    }

    //header("Location: subscribed.php");
    exit('<br />stop point');

    $query="INSERT INTO enquiries (EmailAddress, DateTimeEntered) VALUES ('$emailAD',GetDate())";

    $result=sqlsrv_query($conn$query);

    if (!
    $result) {
        echo 
    $result;
        die(
    "Query failed");

        } else {

        echo 
    "Success";
        } 
        
    sqlsrv_free_stmt$result);

    sqlsrv_close($conn);;

    ?>

  7. #7
    Join Date
    Jun 2011
    Posts
    141
    I had to add an extra ) within this line:
    Code:
    if(!isEmail($emailAD) {
    but after that and on submitting, I get:

    Notice: Undefined variable: data in C:\HostingSpaces\JoPoTa\jopota.passingbuys.co.uk\wwwroot\emailsubscribe.php on line 10

    Email ade test 2 is wrong
    stop point

  8. #8
    Join Date
    Apr 2012
    Posts
    15
    In your original code replace
    PHP Code:
    if(!isEmail($data['emailAD'])) { 
    with
    PHP Code:
    if(!isEmail($emailAD)) { 
    It should work fine then

  9. #9
    Join Date
    Jun 2011
    Posts
    141
    Have tried this but I still just keep getting redirected to the subscribed page with the content getting put in the database, and im only typing "test".

  10. #10
    Join Date
    Apr 2012
    Posts
    15
    and did you uncomment these lines of code?
    PHP Code:
    //header("Location: index.php?msg=$err");
    //exit(); 

  11. #11
    Join Date
    Jun 2011
    Posts
    141
    Right, done that and it works (i think) because if I type in what looks like an email address, I go through to the subscribed page, and if I do test, it looks like its refreshing my index page but the error message doesnt display?

  12. #12
    Join Date
    Apr 2012
    Posts
    15
    Well, that would be a second question Basically you assign an error message to an array $err[] but then you redirect to another page - and that means all the variables, arrays etc are lost
    You need to find a way to pass the value (error message) to another page, either by using $_GET and passing it via URL, or using sesions, which is a bit more complicated

  13. #13
    Join Date
    Jun 2011
    Posts
    141
    So would it be easier to just have a prompt pop up when the user clicks submit to say "your email address is not valid" and then the user has to click OK and correct their email, and if so, how is this done?

    The code I have taken from my registration script to use for this works there so there must be a way, when someone fills out the registration page it uses the same validation stuff and if the email is not right, returns the registration page with a message under the input sayin "email is not valid", this is near enough to same script but doesnt display the message.

  14. #14
    Join Date
    Jun 2011
    Posts
    141
    I have just removed the [] from
    Code:
    $err[] = "ERROR - Invalid email address.";
    and submitted the black field which then refreshed the index page and I noticed the error message was displaying in the address bar as this:

    http://www.mydomain.co.uk/index.php?...mail%20address.

    How can I now get this to display where I have put:

    Code:
    <?php
    if(!empty($err))  {
    echo "<div class=\"msg\">";
    	  foreach ($err as $e) {
    	    echo "* $e <br>";
    	    }
    	  echo "</div>";	
    	   }
    ?>

  15. #15
    Join Date
    Apr 2012
    Posts
    15
    Like this

    PHP Code:
    echo $_GET['msg']; 

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles