insert record with ajax and php
Posted: Tue Nov 04, 2008 12:30 am
hi i am trying to insert records with ajax.
I inserted records in database but it only works with input type button and when i change input type to image it does not work.i also try the validation but it doesn't seem to be working.
please help me.
I am here giving my code to give u clear idea.
mainpage.php
email.php
Please help me.
Thanks in advance.
I inserted records in database but it only works with input type button and when i change input type to image it does not work.i also try the validation but it doesn't seem to be working.
please help me.
I am here giving my code to give u clear idea.
mainpage.php
Code: Select all
<html>
<head>
<script language="javascript" type="text/javascript">
<!--
//Browser Support Code
function ajaxFunction(){
var ajaxRequest; // The variable that makes Ajax possible!
try{
// Opera 8.0+, Firefox, Safari
ajaxRequest = new XMLHttpRequest();
} catch (e){
// Internet Explorer Browsers
try{
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try{
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e){
// Something went wrong
alert("Your browser broke!");
return false;
}
}
}
// Create a function that will receive data sent from the server
ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState == 4){
var ajaxDisplay = document.getElementById('ajaxDiv');
ajaxDisplay.innerHTML = ajaxRequest.responseText;
}
}
var news = document.getElementById('news').value;
document.getElementById('ajaxDiv').innerHTML = 'Loading content - please wait';
var queryString = "?email=" + news ;
ajaxRequest.open("GET", "email.php" + queryString, true);
ajaxRequest.send(null);
}
//-->
function ValidateForm(){
var emailID=document.myform1.text1
if ((emailID.value==null)||(emailID.value=="")){
alert("Please enter your E-mail Address")
emailID.focus()
return false
}
if (echeck(emailID.value)==false){
emailID.value=""
emailID.focus()
return false
}
return true
}
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("Please enter a proper E-mail Address")
return false
}
if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
alert("Please enter a proper E-mail Address")
return false
}
if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
alert("Please enter a proper E-mail Address")
return false
}
if (str.indexOf(at,(lat+1))!=-1){
alert("Please enter a proper E-mail Address")
return false
}
if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
alert("Please enter a proper E-mail Address")
return false
}
if (str.indexOf(dot,(lat+2))==-1){
alert("Please enter a proper E-mail Address")
return false
}
if (str.indexOf(" ")!=-1){
alert("Please enter a proper E-mail Address")
return false
}
return true
}
</script>
</head>
<body>
<table>
<tr>
<td >
<form name="myform1" >
<input type="text" name="text1" class="store4" id="news" Value="Email Address >>" >  <sub>
<input type="button" onclick='ajaxFunction()' onSelect="return ValidateForm();"/></sub>
</form>
</td>
</tr>
<tr> <td><div id="ajaxDiv" class="header_help1">
</div></td>
</tr>
</table>
</body>
</html>
Code: Select all
<?php
$db_host="localhost";
$db_name="ajax";
$username="root";
$password="root";
// DON'T CHANGE THE FOLLOWING CODE!
$db_con=mysql_connect($db_host,$username,$password);
$connection_string=mysql_select_db($db_name);
mysql_connect($db_host,$username,$password);
mysql_select_db($db_name);
if(isset($_GET['email']))
{
$email=$_GET['email'];
$InSql = "insert into User(Email) values ('$email')";
$ExQry1 = mysql_query($InSql) or die(mysql_error());
if($ExQry1)
{
$mode="You have successfully inserted record!";
}
else
{
$mode = "Sorry!!There is some mulfunction.";
}
echo $mode;
}
?>
Thanks in advance.