Hi there, I need some help actually using the ajax I've got, and defining the url in the ajax [if that makes sense] I've been trying to do it but It's not working atall and I wondered if someone could actualy show me in the code how to go about defining and using the certain parts.
The Ajax:
function loadurl(ajax) {
var ajax = false;
// Create the object:
// Choose the objecttype, depending on what is supported:
if (window.XMLHttpRequest) {
// IE 7, Mozilla, Safari, Firefox, Opera, most browsers:
ajax = new XMLHttpRequest();
} else if (window.ActiveXObject) { // Old IE-browsers
// Make the type Msxml2.XMLHTTP, if possible:
try {
ajax = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e1) { // Else use the other type:
try {
ajax = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e2) { }
}
}
// Retrieving the data from the page
if (ajax) {
alert("url:generate.php" + url);
ajax.open('GET', url);
// Sends request
ajax.send(null);
// Function that handles response
ajax.onreadystatechange=function(){
// If everything is OK:
if ( (ajax.readyState == 4) && (ajax.status == 200) ) {
// Returns the value to the document
alert(ajax.responseText);
}
}
} else { // AJAX is not useable
alert('It is not possible to connect, please update your browser.');
}
}
The PHP:
<?php
if($_GET['act'] == "generate_quotes") {
$db = mysql_connect("localhost", "root", "bonnie") or die ("Unable to connect to database.");
mysql_select_db("quote") or die ("Unable to select database.");
$result = mysql_query( " SELECT * FROM `quote` ORDER BY RAND() LIMIT 0,1 " );
$fetch = mysql_fetch_array($result);
echo "<blockquote>".$fetch['q_quote']."</blockquote>";
mysql_close($db);
} else {
echo "<img src=\"1.png\">";
}
?>
The part where I'm trying to bring it together:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Untitled Document</title>
<script type="text/javascript" src="ajax.js" ></script>
</head>
<body>
<a onClick="loadurl('generate.php'); return false;">Click Here</a>
</body>
</html>
I've tried to call the js file in the header. But can someone please just point out which bits need calling where.
I'm defining ajax, but I'm not sure where to actualy call it [use it] and I'm trying to use the url, but don’t know where to define it.
Anyone kind enough as to show me how to do this? I have 90% of it done, but putting it together is driving me mad!