942,956 Members | Top Members by Rank

Ad:
Aug 11th, 2010
0

Calling AJAX...

Expand Post »
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:

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. function loadurl(ajax) {
  2.  
  3. var ajax = false;
  4.  
  5. // Create the object:
  6.  
  7. // Choose the objecttype, depending on what is supported:
  8. if (window.XMLHttpRequest) {
  9.  
  10. // IE 7, Mozilla, Safari, Firefox, Opera, most browsers:
  11. ajax = new XMLHttpRequest();
  12.  
  13. } else if (window.ActiveXObject) { // Old IE-browsers
  14.  
  15. // Make the type Msxml2.XMLHTTP, if possible:
  16. try {
  17. ajax = new ActiveXObject("Msxml2.XMLHTTP");
  18. } catch (e1) { // Else use the other type:
  19. try {
  20. ajax = new ActiveXObject("Microsoft.XMLHTTP");
  21. } catch (e2) { }
  22. }
  23.  
  24. }
  25.  
  26. // Retrieving the data from the page
  27.  
  28. if (ajax) {
  29.  
  30. alert("url:generate.php" + url);
  31.  
  32. ajax.open('GET', url);
  33.  
  34. // Sends request
  35. ajax.send(null);
  36.  
  37. // Function that handles response
  38. ajax.onreadystatechange=function(){
  39. // If everything is OK:
  40. if ( (ajax.readyState == 4) && (ajax.status == 200) ) {
  41. // Returns the value to the document
  42. alert(ajax.responseText);
  43. }
  44. }
  45.  
  46.  
  47. } else { // AJAX is not useable
  48. alert('It is not possible to connect, please update your browser.');
  49. }
  50.  
  51. }

The PHP:

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <?php
  2.  
  3. if($_GET['act'] == "generate_quotes") {
  4.  
  5. $db = mysql_connect("localhost", "root", "bonnie") or die ("Unable to connect to database.");
  6. mysql_select_db("quote") or die ("Unable to select database.");
  7.  
  8.  
  9. $result = mysql_query( " SELECT * FROM `quote` ORDER BY RAND() LIMIT 0,1 " );
  10. $fetch = mysql_fetch_array($result);
  11.  
  12.  
  13. echo "<blockquote>".$fetch['q_quote']."</blockquote>";
  14. mysql_close($db);
  15.  
  16. } else {
  17.  
  18. echo "<img src=\"1.png\">";
  19.  
  20. }
  21. ?>

The part where I'm trying to bring it together:

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  5. <title>Untitled Document</title>
  6. <script type="text/javascript" src="ajax.js" ></script>
  7. </head>
  8.  
  9. <body>
  10. <a onClick="loadurl('generate.php'); return false;">Click Here</a>
  11. </body>
  12. </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!
Last edited by ello; Aug 11th, 2010 at 1:08 pm.
Similar Threads
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
ello is offline Offline
66 posts
since Aug 2010
Aug 11th, 2010
0
Re: Calling AJAX...
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.
Last edited by Taywin; Aug 11th, 2010 at 1:21 pm.
Reputation Points: 229
Solved Threads: 238
Posting Virtuoso
Taywin is offline Offline
1,723 posts
since Apr 2010
Aug 11th, 2010
0
Re: Calling AJAX...
Click to Expand / Collapse  Quote originally posted by Taywin ...
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...
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
ello is offline Offline
66 posts
since Aug 2010

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JavaScript / DHTML / AJAX Forum Timeline: Simple PHP form to Ajax
Next Thread in JavaScript / DHTML / AJAX Forum Timeline: Javascript Show/hide form doesnt work....





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC