PHP-AJAX-MY SQL updating a div tag?

Wolf

New member
can any one tell me where my problem is : i want to update the "divstatus" div tag with out refreshing the page
this one works but it refreshes the page and shows the result
tanx

HTML CODE:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script language="javascript">
function getRequestBody(oForm) {
var aParams = new Array();
for (var i=0; i<oForm.elements.length; i++) {
var sParam = encodeURIComponent(oForm.elements.name);
sParam += "=";
sParam += encodeURIComponent(oForm.elements.value);
aParams.push(sParam);
}
return aParams.join("&");
}
function sendRequest() {
var oForm = document.forms[0];
var sBody = getRequestBody(oForm);
var oXmlHttp = createXMLHttp();
oXmlHttp.open("post", oForm.action, true);
//oXmlHttp.setRequestHeader("Content-Type", "application/x-
//www-form-urlencoded");
oXmlHttp.onreadystatechange = function () {
if (oXmlHttp.readyState == 4) {
if (oXmlHttp.status == 200) {
saveResult(oXmlHttp.responseText);
} else {
saveResult("An error occurred: "+
oXmlHttp.statusText);
}
}
};
oXmlHttp.send(sBody);
}
function saveResult(sMessage) {
var divStatus = document.getElementById("divStatus");
divStatus.innerHTML = "Request completed: "+ sMessage;
}
</script>
</head>

<body>
<form method="post" action="Save2.php" onsubmit="sendRequest();return false;">
<p>Enter customer information to be saved:</p>
<p>Customer Name: <input type="text" name="txtname" value="" /><br />
Password: <input type="password" name="txtpass" value="" /><br />
Phone: <input type="text" name="txtphone" value="" /><br />
Address: <input type="text" name="txtadd" value="" /><br />
Mail: <input type=" text" name="txtemail" value="" /><br />
Gender: <input type="text" name="txtgender" value="" /><br />
Age: <input type="text" name="txtage" value=""/></p>
<p><input type="submit" value="Save Customer Info" /></p>
<div id="divStatus"></div>
</form>

</body>
</html>


PHP CODE :

<?php
header("Content-Type: text/plain");
$name = $_POST["txtname"];
$pass = $_POST["txtpass"];
$phone = $_POST["txtphone"];
$add = $_POST["txtadd"];
$gender = $_POST["txtgender"];
$age = $_POST["txtage"];
$email = $_POST["txtemail"];
$Status = "";
$sDBName = "grocery";
$check="unset";
$check1="unset";
$sqlcheck = "SELECT * FROM registration";
$sql = "Insert into registration(user_name,user_pass,user_phone,user_add,user_email,user_gender,user_age) "." values('$name','$pass','$phone','$add','$email','$gender','$age')";
$oLink = mysql_connect("localhost","root","") or die(mysql_error());
mysql_select_db("grocery") or die(mysql_error());
$result = mysql_query ($sqlcheck) or die(mysql_error());
while($sina=mysql_fetch_assoc($result))
{

if( $sina['user_name'] == $name )
$check = "set";
if( $sina['user_email'] == $email )
$check1="set";

}

if($check1 == "unset")
{
if($check == "set" ) { echo "#ERROR: Username Exists in the database";}
else
{


mysql_query ($sql) or die(mysql_error());

echo "!!!!Thank you, your infotmation has been received!!!!";
}
}
else
{
echo "your email address is already registered";
}
mysql_close($oLink);
echo $Status;
?>
 
Back
Top