Forum Moderators: open

Message Too Old, No Replies

basic ajax with php & mysql

basic ajax with php & mysql

         

drooh

4:07 am on May 6, 2007 (gmt 0)

10+ Year Member



i am new to ajax but rather familiar with php & mysql.

i have data in a mysql db that is simply catalgued by an id integer. i would like to creat an html navigation (a basic set of links) that would use ajax to load data into a div.

this is for simple content management system for clients sites.

how might I alter this code so that I don't have to add a hidden input, but rather include the id in the actual link (the <a href="" some javascript>link 1</a>)

here is my current code


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<html>
<head>
<title>Customer Account Information</title>
<script type="text/javascript">
var url = "GetCustomerData.php?id="; // The server-side script
function handleHttpResponse() {
if (http.readyState == 4) {
if(http.status==200) {
var results=http.responseText;
document.getElementById('divCustomerInfo').innerHTML = results;
}
}
}
function requestCustomerInfo() {
var sId = document.getElementById("txtCustomerId").value;
http.open("GET", url + escape(sId), true);
http.onreadystatechange = handleHttpResponse;
http.send(null);
}
function getHTTPObject() {
var xmlhttp;
if(window.XMLHttpRequest){
xmlhttp = new XMLHttpRequest();
}
else if (window.ActiveXObject){
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
if (!xmlhttp){
xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
}

}
return xmlhttp;
}
var http = getHTTPObject(); // We create the HTTP Object
</script>
</head>
<body>

<a href="javascript:requestCustomerInfo('1')">request 1</a>
<input type="hidden" value="1" id="txtCustomerId">

<div id="divCustomerInfo"></div>
</body>
</html>

whoisgregg

1:18 pm on May 7, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It's pretty simple actually. You just needed to define a parameter for your requestCustomerInfo function. All the rest of your code stays the same, just edit that function as shown below. :)

function requestCustomerInfo(sId) { 
if(! sId ) { return false; } // to prevent sending bad data to script
http.open("GET", url + escape(sId), true);
http.onreadystatechange = handleHttpResponse;
http.send(null);
}

Drag_Racer

3:22 pm on May 8, 2007 (gmt 0)

10+ Year Member



splitting it into 3 functions seems confusing...

function requestCustomerInfo(id) {
if(! id ¦¦ id == ''){return}
var URL = "GetCustomerData.php?id="+id+"&"+new Date().getTime(); //time stops cache by browser
var container = document.getElementById("divCustomerInfo");
var request = false;
if (window.XMLHttpRequest) {
request = new XMLHttpRequest();
}
else if (window.ActiveXObject) {
try {
request = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e){
try{
request = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e){}
}
}
request.open("GET", URL, true);
request.onreadystatechange = function() {
if (request.readyState == 4) {
container.innerHTML = request.responseText;
}
}
request.send(null);
}