Topic
IC4NOTICE: developerWorks Community will be offline May 29-30, 2015 while we upgrade to the latest version of IBM Connections. For more information, read our upgrade FAQ.
5 replies Latest Post - ‏2014-09-20T11:04:18Z by jitupkp
kallal
kallal
3 Posts
ACCEPTED ANSWER

Pinned topic Please find the bug out in this Ajax subscription (newsletter)

‏2012-04-27T11:38:49Z |
This code working but to give the error message :
Error:Duplicate entry 'pppppppp@gmail.com' for key 'email'

Please Anyone help me find the bug out...

<!--index.php-->
<script type="text/javascript">

//GET PROPER XMLTTTP
function getXMLHttp()
{
var xmlHttp

try
{
//Firefox, Opera 8.0+, Safari
xmlHttp = new XMLHttpRequest();
}
catch(e)
{
//Internet Explorer
try
{
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e)
{
try
{
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e)
{
alert("Your browser does not support AJAX!")
return false;
}
}
}
return xmlHttp;
}
// MAKE THE REQUEST TO THE SERVER
function MakeRequest(no,name)
{

var xmlHttp = getXMLHttp();

xmlHttp.onreadystatechange = function()
{
if(xmlHttp.readyState == 4 && xmlHttp.status==200)
{
HandleResponse(xmlHttp.responseText);
}
}

xmlHttp.open("GET", "newsletter.php?q="no"&name="+name, true);
xmlHttp.send(null);
}

//HANDALE THE RESPONSE....
function HandleResponse(response)
{
document.getElementById('message').innerHTML = response;
}

/* Newsletter Form Validation */
/*========Email==============*/
function checkEmail() {
var name=document.getElementById('name').value;
var valid=nameValidation(name);

if(valid==1)
{
var message="Please Enter the Name";
display_message(message);
document.getElementById('name').focus();
return false;
}
else if(valid==2)
{
var message="Please Enter valid name";
display_message(message);
document.getElementById('name').value="";
document.getElementById('name').focus();
return false;
}

var email = document.getElementById('emailaddress').value;
email = email.replace(/^\s+|\s+$/g, '') ;
var filter = /^(http://a-zA-Z0-9_\.\-)+\@((a-zA-Z0-9\-)+\.)+(a-zA-Z0-9{2,4})+$/;
if (!filter.test(email)) {
//var message=document.getElementById('message').innerHTML="Please provide a valid email address";
var message="Please provide a valid email address";
display_message(message);
//message(message);
//alert('Please provide a valid email address');
email.focus;
return false;
}
document.getElementById('hemailaddress').value=email;
//view(email,name);
MakeRequest(email,name);
}
function display_message(m){ document.getElementById('message').innerHTML=m; }

/* Next Stage */
function phoneNo(ph_no){

var targ=ph_no.replace(/^\d/g,''); // remove all non-digits
if(targ && targ.length===10) {
return ture;// targ is a valid phone number
}
else{return false;}
}
function nameValidation(name){

NameTxt=name;
var namevalid=/^a-zA-Z+\s/;
if(NameTxt=="")
{
//var message="Please Enter the Name";
//document.getElementById('name').focus();
return 1;
}
else if(NameTxt.search(namevalid)==-1)
{
// var message="Please Enter valid name";
//document.getElementById('name').value="";
//document.getElementById('name').focus();
return 2;
}
else{return 3}

}
//window.onload = init; //At loading we load the 1st set of images to prevent errors
</script>
<style type="text/css">
.newsletter_message{
display:block;
background-color:#F90;
color:red;
font-weight:bold;
line-height:10px;"
}
.newsletter_ph_no{
margin:2px;
}
.newsletter_name{
margin:2px;
}
.newsletter_subscribed{
display:block;
background-color:#000;
color: #6F3;
font-weight:bold;
font-size:16;
line-height:16px;"
}
</style>

<input type="hidden" name="hemailaddress" id="hemailaddress" value="">


<form name="newsletter" method="post" action="">


<input type="button" src="images2/subcribe-btn.jpg" value="Subscribe" onclick="checkEmail();">

</form>
<!
newsletter.php-->
<?php

//if(isset($_GET))$email = $_GET; //Check weather a variable is passed to page, if so it become the current page number.
if(isset($_GET))$extra = $_GET;

//Number of pictures that should be shown per page
//Get the offset to be used in Mysql query, dont bother about it Grin
if(isset($_GET)){

$email = $_GET;
$name = $_GET;
$conn = mysql_connect("localhost","root",""); //Connecting to the databse, put the necessary variables. Ex: db_host replace with "127.0.0.1"

if(!($conn)) echo "Failed To Connect To The Database!";
else{

if(mysql_select_db("phpcms",$conn))
{

//Check weather databse exists
$query='SELECT * FROM fb_newsletter where email="'.$email.'"';
//From here we first do the page breaking as it takes the big deal of the script
$result = mysql_query($query,$conn);
$rows = mysql_fetch_array($result);
if($rows){
//$rows = mysql_fetch_array($result);
//echo $stored_email= $rows;
echo "exist";
}
else
{

$query="INSERT INTO fb_newsletter (email, firstname, lastname) VALUES ('".$email."', '".$name."', '1234567890')" ;
$result = mysql_query($query);
if (!mysql_query($query,$conn))
{
die('Error:' . mysql_error());
}

//echo $email;
echo "Thank You ".$name."! ".$email." has been added to our Mailing List.";
}
}
}

mysql_close($conn);//ENDS DB CONNECTON
}
/*if(isset($_GET)){
$email = $_GET;
$ph_no = $_GET;
$name = $_GET;

$conn = mysql_connect("localhost","indianel","oK_X3oRva"); //Connecting to the databse, put the necessary variables. Ex: db_host replace with "127.0.0.1"

if(!($conn)) echo "Failed To Connect To The Database!";
else{

if(mysql_select_db("indianel_coza",$conn))
{

$query="UPDATE fb_newsletter SET firstname='".$name."',lastname='".$ph_no."' WHERE email='".$email."'";
$result = mysql_query($query);
if (!mysql_query($query,$conn))
{
die('Error: ' . mysql_error());
}
echo "Thank You ".$name."! ".$email." has been added to our Mailing List.";

}
}

mysql_close($conn);//ENDS DB CONNECTON

}
*/?>
<!-- db table -->
CREATE TABLE IF NOT EXISTS `fb_newsletter` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`email` varchar(255) NOT NULL,
`firstname` varchar(250) NOT NULL,
`lastname` varchar(250) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `email` (`email`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=63 ;
Updated on 2012-04-27T14:18:36Z at 2012-04-27T14:18:36Z by kallal