942,956 Members | Top Members by Rank

Ad:
Aug 10th, 2010
0

AJAX PHP Function

Expand Post »
Hey guys, I'm having some AJAX troubles that hopefully someone can help me with. I have some PHP code, which works fine. And I'm trying to call the PHP function with AJAX. But I've never used AJAX at all so I'm a little confused. I've tried to fiddle around with some code but it's just not working. Wondered if anyone could help. The code is bellow:

PHP:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <?php
  2.  
  3. if($_GET['act'] == "generate_quotes") {
  4.  
  5. $db = mysql_connect("*", "*", "*") 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 link which I want to call the function:

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <div id="ajaxlink"><a href='gengen.php?act=generate_quotes'>Generate</a></div>

The AJAX:

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. function loadurl(dest) {
  2. try {
  3. ("Microsoft.XMLHTTP");
  4. } catch (e) {
  5. }
  6. xmlhttp.onreadystatechange = triggered;
  7. xmlhttp.open("GET", dest);
  8. xmlhttp.send("null");
  9. }
  10. function triggered() {
  11. if ((xmlhttp.readyState == 4) (xmlhttp.status == 200)) {
  12. document.getElementById("ajaxlink").innerHTML = xmlhttp.responseText;
  13. }
  14. }

The AJAX maybe completley wrong, I'm really not sure, but if so could somone show me how to go about doing this?

Thanks alot for any help
Similar Threads
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
ello is offline Offline
66 posts
since Aug 2010
Aug 10th, 2010
0
Re: AJAX PHP Function
Well the AJAX is completely wrong , let me explain how AJAX works:

- You create a XMLHTTP request object
- You define a function that is triggered when the readystate is changed of the XMLHTTP object
- You open a url using the XMLHTTP object
- You send some data via the XMLHTTP object
- Everytime the readystate and status change, the function you defined at step 2 is called

Here is an example of an AJAX function (that loads the url):

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. function loadurl(url) {
  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: " + 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. }

~G
Reputation Points: 82
Solved Threads: 74
Posting Pro in Training
Graphix is offline Offline
400 posts
since Aug 2009
Aug 10th, 2010
0
Re: AJAX PHP Function
Hey thanks so much, I would have put money on that code being wrong, but thanks alot, clears it up wonderfully. But would the link remain the same on the HTML side of things?

E.g:

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <div id="ajaxlink"><a href='gengen.php?act=generate_quotes'>Generate</a></div>
?

Or would you need something more like:

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <div id="ajaxlink" onclick="loadurl('ajax_function.php')">Click Here</div>

?
Last edited by ello; Aug 10th, 2010 at 1:52 pm.
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
ello is offline Offline
66 posts
since Aug 2010
Aug 10th, 2010
0
Re: AJAX PHP Function
You need the second one, the href isn't going to work. However when a <a> element is clicked the page is reloaded. But not if you do it like the following example:

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <div id="ajaxlink"><a onclick="loadurl('ajax_function.php'); return false;">Click Here</a></div>

~G
Reputation Points: 82
Solved Threads: 74
Posting Pro in Training
Graphix is offline Offline
400 posts
since Aug 2009
Aug 10th, 2010
0
Re: AJAX PHP Function
Thanks alot for all your help, worked a treat
Last edited by ello; Aug 10th, 2010 at 3:06 pm.
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: POST vs GET i cant get either to work
Next Thread in JavaScript / DHTML / AJAX Forum Timeline: Edit selected text in textarea.





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


Follow us on Twitter


© 2011 DaniWeb® LLC