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!

Recommended Answers

All 2 Replies

The loadurl() accept a php page, right? Why did you overridden the page coming in? The argument should be used in composing where you want your ajax to call to. Then, in your calling, you did not initialize what URL ajax needs to call but attempt to send it out. Therefore, try to change your function prototype ('function loadurl(ajax)' to 'function load(url)') and see what happens.

The loadurl() accept a php page, right? Why did you overridden the page coming in? The argument should be used in composing where you want your ajax to call to. Then, in your calling, you did not initialize what URL ajax needs to call but attempt to send it out. Therefore, try to change your function prototype ('function loadurl(ajax)' to 'function load(url)') and see what happens.

Thanks for the reply.

So you mean change it to:

function loadurl(generate.php) {

?

But could you please enlighten me as to why

<a onClick="loadurl('generate.php'); return false;">Click Here</a>

is un clickable? I was reading somewhere you shouldn't use a hef, but without one, it doesn't appear as a link...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.