Jump to content

Using Onclick To Make Ajax Call


The Stig

Recommended Posts

Hi allI have been using the code at W3schools for ajax using PHP as the server side code http://www.w3schools.com/php/php_ajax_responsexml.aspI had some trouble getting it to work, but a few mods (the header content type and response Format) and it works perfectly.What I want to do is instead of the html page with the select box, I would like instead to have 4 images and clicking on each would update the page. So the same as the example in the link above, but instead of the select with an onchange value, it would be an onclick of an image that made the ajax call.I have tried but I cant seem to get it to work, I am having trouble passing the value to showUser().Firebug is reporting xmlDoc is null. I am not sure how to code the part with the image and an onclick call to showUser.Many thanks in advance for any helpThe Stig

Link to comment
Share on other sites

You just call showUser with whatever value you want for each image:<img onclick="showUser('image1');" ...If you want to pass the filename of the image you can use the src attribute:<img onclick="showUser(this.src);" ...

Link to comment
Share on other sites

You just call showUser with whatever value you want for each image:<img onclick="showUser('image1');" ...If you want to pass the filename of the image you can use the src attribute:<img onclick="showUser(this.src);" ...
Thank you for your reply. I had tried something like that but no luck. Here is my code:
<html><head><script type="text/javascript" src="responsexml.js"></script></head><body><form><a href=""><img id="1" src="image1.jpg" onclick="showUser(this.id)" /></a><a href=""><img id="2" src="image2.jpg" onclick="showUser(this.id)" /></a><a href=""><img id="3" src="image3.jpg" onclick="showUser(this.id)" /></a><a href=""><img id="4" src="image4.jpg" onclick="showUser(this.id)" /></a></form><div id="seeifthisworks"></div></body></html> 

The div "seeifthisworks" is from when I was trying to get the script to work originally with the select drop down. I have alerted the url in the responsexml.js file and it will read what I am expecting, eg, clicking on image 1 will show "responsexml.php?q=1&sid=0.11832496278312443", but the xmlDoc is showing as null in firebug.As the version with the select drop down works and this one doesn't and this is the only thing I have changed I thought I was probably doing something wrong with passing the value to showUser in the responsexml.js file.I will keep trying things, if any one has any ideas I would appreciate it.

Link to comment
Share on other sites

Look at the request going out with Firebug, if the request contains all of the correct data for the server but the response is empty, then the server (PHP) isn't handling the request correctly. Maybe it's not finding any data to return for the data that it was sent.

Link to comment
Share on other sites

Checked firebug, showUser is showing as undefined, so to xmlhttp and GetXMLHttpObject.I have checked that the original script is still working, and it is, so I am guessing it is only as a result of this way that I am not getting anything.Frustrating, but I will keep trying...........

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...