makko Posted February 4, 2009 Share Posted February 4, 2009 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.