0

I'd like to have a textbox validate that the text in it is in email format using javascript. Once it is validated, I'd like the email address to be sent to a MySQL database (preferably using PHP).

The code I have so far for the email validation in javascript is:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0         Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>Untitled document</title>
</head>
<body>
<p>
<script language="Javascript"><!--
/**
 * DHTML email validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */

function echeck(str) {

        var at="@"
        var dot="."
    var lat=str.indexOf(at)
    var lstr=str.length
    var ldot=str.indexOf(dot)
    if (str.indexOf(at)==-1){
       alert("Invalid E-mail ID")
       return false
    }

    if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
       alert("Invalid E-mail ID")
       return false
    }

    if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
        alert("Invalid E-mail ID")
        return false
    }

     if (str.indexOf(at,(lat+1))!=-1){
        alert("Invalid E-mail ID")
        return false
     }

     if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
        alert("Invalid E-mail ID")
        return false
     }

     if (str.indexOf(dot,(lat+2))==-1){
        alert("Invalid E-mail ID")
        return false
     }

     if (str.indexOf(" ")!=-1){
        alert("Invalid E-mail ID")
        return false
     }

     return true                    
}

function ValidateForm(){
var emailID=document.frmSample.txtEmail

if ((emailID.value==null)||(emailID.value=="")){
    alert("Please Enter your Email ID")
    emailID.focus()
    return false
}
if (echeck(emailID.value)==false){
    emailID.value=""
    emailID.focus()
    return false
}
return true
 }
// --></script>
</p>
<form name="frmSample" method="post" action="#" onsubmit="return ValidateForm()">
<p>Enter an Email Address : 
                  <input name="txtEmail" type="text" /></p>
<p><input name="Submit" value="Submit" type="submit" /></p>
</form>
</body>
</html>

How do I incorporate the PHP code within this?

Toto
  • 89,455
  • 62
  • 89
  • 125
sharataka
  • 5,014
  • 20
  • 65
  • 125
  • You might want to use Regular Expressions to match an email pattern, can be done with a few lines of code: http://stackoverflow.com/questions/46155/validate-email-address-in-javascript – craig1231 Dec 05 '11 at 14:12
  • The javascript that you got from that site is not relyable, it doesnt even terminate each line by ; – craig1231 Dec 05 '11 at 14:13
  • @craig1231 and the first if is redundant since it is included in the second... – macjohn Dec 05 '11 at 14:20

2 Answers2

1

You need to learn how to do this, there is, and you should learn about sql injection prevention as well. below a few links to help guide you.

Community
  • 1
  • 1
david
  • 4,218
  • 3
  • 23
  • 25
0

Put this at the top of the page... (and change to suit)

<?php
if (isset($_POST['txtEmail']))
{
    $email = mysql_real_escape_string($_POST['txtEmail']);

    mysql_connect($domain, $user, $pass);
    mysql_select_db("mydb");

    $sql = "INSERT INTO emails (email) VALUES ('".$email."')";
    mysql_query($sql);
}
?>
craig1231
  • 3,769
  • 4
  • 31
  • 34
  • How do I incorporate this into the existing code I have? Do I have to change the form attributes in the html? – sharataka Dec 05 '11 at 14:57
  • The $sql = "INSERT INTO emails (email) VALUES ('".$email."')"; line isn't working. It inputs an empty row in the table on the database. Is there a correct way to bring in $email? – sharataka Dec 05 '11 at 19:11
  • This is the correct way. The fact the query is executing means $_POST['txtEmail'] is set, but contains nothing... I cant debug your code for you... With POST, if you edit the page and REFRESH you have to RESEND the posted data. – craig1231 Dec 06 '11 at 07:57