3 Replies - 8361 Views - Last Post: 01 January 2012 - 05:31 PM

#1 guyfromri  Icon User is offline

  • D.I.C Addict

Reputation: 46
  • View blog
  • Posts: 836
  • Joined: 16-September 09

JScript/AJAX -- Trying to update a div on link click but no luck

Posted 01 January 2012 - 11:14 AM

Hey guys!!

I have a question, I have a piece of code that I've used before that works fine but in this case it doesn't seem to work and I can't really figure out why --

Here's the script
function get_eBdy(email_id){
    if (window.XM/LHttpRequest)
      {// code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp=new XMLHttpRequest();
      }
    else
      {// code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    xmlhttp.onreadystatechange=function()
      {
      if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
        document.getElementById("content").innerHTML=xmlhttp.responseText;
        }
      }
    xmlhttp.open("GET","http://stuff.co/scripts/email/read.php?e=" + email_id, true); 
    xmlhttp.send();
}



Here's the HTML output -- This is contained in the "Content" div
  <div class='email_subject'>
   <a href='javascript:get_eBdy(1);'>db test</a>
  </div>



The PHP is simple -- at the moment I'm just trying to get it to echo a line -- here's the script on the other end
<?
echo "Read is working!!";
?>




I can't seem to figure out why it's not working so any help would be greatly appreciated!! As always, thanks in advance!!!

Is This A Good Question/Topic? 0
  • +

Replies To: JScript/AJAX -- Trying to update a div on link click but no luck

#2 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 963
  • View blog
  • Posts: 2,382
  • Joined: 15-February 11

Re: JScript/AJAX -- Trying to update a div on link click but no luck

Posted 01 January 2012 - 11:37 AM

For Javascript issues always check your error console. 90% of the time you'll see the error there.

On line 2 you've got a slash in XMLHttpRequest.

For the sake of neatness and keeping your code organized I'd suggest using a self invoking function to assign the correct instance of XMLHttpRequest to your xmlhttp variable.
var xmlhttp = (function(){
    if(window.XMLHttpRequest)
        return new XMLHttpRequest();
    else
        return new ActiveXObject('Microsoft.XMLHTTPREQUEST');
})();


Was This Post Helpful? 2
  • +
  • -

#3 guyfromri  Icon User is offline

  • D.I.C Addict

Reputation: 46
  • View blog
  • Posts: 836
  • Joined: 16-September 09

Re: JScript/AJAX -- Trying to update a div on link click but no luck

Posted 01 January 2012 - 11:39 AM

lmao!! I can't believe I missed that typo -- cp thank you so much!!! after looking at it for so long it all kind of blends I guess lol thanks!!!!!!!
Was This Post Helpful? 0
  • +
  • -

#4 Atli  Icon User is offline

  • Enhance Your Calm
  • member icon

Reputation: 4238
  • View blog
  • Posts: 7,216
  • Joined: 08-June 10

Re: JScript/AJAX -- Trying to update a div on link click but no luck

Posted 01 January 2012 - 05:31 PM

View Postcodeprada, on 01 January 2012 - 06:37 PM, said:

For the sake of neatness and keeping your code organized I'd suggest using a self invoking function to assign the correct instance of XMLHttpRequest to your xmlhttp variable.

I can't really agree with this in this instance. Self-invoking functions do have their uses, but when it comes to a purely cosmetic change, I wouldn't use them. It's basically just an extra, unneeded function call. And as far as neatness goes, I wouldn't consider this any worse:
var xmlhttp = null;
if(window.XMLHttpRequest)
    xmlhttp = new XMLHttpRequest();
else
    xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');


If you want the assignment of xmlhttp to appear indented and as if it actually belongs to the original declaration (like with your self-indented example) you could simply do this for a similar effect:
var xmlhttp = null; {
    if(window.XMLHttpRequest)
        xmlhttp = new XMLHttpRequest();
    else
        xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
}


Was This Post Helpful? 1
  • +
  • -

Page 1 of 1