Jump to content

Write to DB without reload page


makko

Recommended Posts

From index.php when I click the bid image I need to write to DB. I have an solution at the moment and although it doesn't refresh the page it still moves to the top of the page.

 

Code from index.php :

 ... 

<form method="post" action="bids.php" onsubmit="sendBid(); return false;"><input type="hidden" name="name" value="" /><input type="hidden" name="address" value="" /><input type="hidden" name="city" value="" /><input type="submit" value="" style="width:69px; height:20px; background:url('images/bid.png'); border:none; background-repeat:no-repeat;"  /></form> 

...  

 

Code from JavaScript:

 

function getRequestBody(oForm) {
var aParams = new Array();
for (var i=0 ; i < oForm.elements.length; i++) {
	var sParam = encodeURIComponent(oForm.elements[i].name);
	sParam += "=";
	sParam += encodeURIComponent(oForm.elements[i].value);
	aParams.push(sParam);
}
return aParams.join("&");
}
function sendBid() {
var oForm = document.forms[0];
var sBody = getRequestBody(oForm);
var oXmlHttp = zXmlHttp.createRequest();
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);
} 

 

Code from Bids.php:

 

<?php
$name = $_POST["name"];
$address = $_POST["address"];
$city = $_POST["city"];
$sStatus = "";
$sDBServer = "localhost";
$sDBName = "ajax";
$sDBUsername = "root";
$sDBPassword = "";
$sSQL = "insert into bids (name,address,city) values ('$name','$address','$city')";
$oLink = mysql_connect($sDBServer,$sDBUsername,$sDBPassword);
@mysql_select_db($sDBName) or $sStatus = "Unable to open database";
if($oResult = mysql_query($sSQL)) {
$sStatus = "Added customer; customer ID is ".mysql_insert_id();
} else {
$sStatus = "An error occurred while inserting; customer not saved.";
}
mysql_close($oLink);
header("Location:index.php");
?>

 

I need an solution that doesn't loads again the page. I need something done in background ...

 

Can someone please guide me and tell me what I do wrong.

 

Thanks.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.