Go Back   CodingForums.com > :: Client side development > JavaScript programming > Ajax and Design

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-02-2012, 09:54 PM   PM User | #1
paddyfields
Regular Coder

 
Join Date: Dec 2010
Location: London
Posts: 283
Thanks: 60
Thanked 11 Times in 11 Posts
paddyfields is an unknown quantity at this point
Smile Replacing DIV content

Hi there, I'm very new to AJAX and can't manage to work this out, so any help would be fantastic.

I want to be able to change the content of a DIV (the text within it) once a link is pressed. I was doing it with an onclick method but I don't want the page to jump to the top.

I found this which I'm guessing is along the same lines, but i don't want to call a text/XML document... i would like a php string to be echoed instead. Is this possible?

Code:
<script type="text/javascript">
function loadXMLDoc()
{
var xmlhttp;
if (window.XMLHttpRequest)
  {// 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("myDiv").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","ajax_info.txt",true);
xmlhttp.send();
}
</script>
</head>
<body>

<div id="myDiv"><h2>Let AJAX change this text</h2></div>
<button type="button" onclick="loadXMLDoc()">Change Content</button>

It is beyond my abilities and I have been trying this for the last few hours so any advice would be great.

thank you!!

Pat.

Last edited by paddyfields; 04-02-2012 at 11:17 PM..
paddyfields is offline   Reply With Quote
Old 04-02-2012, 10:43 PM   PM User | #2
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,261
Thanks: 10
Thanked 533 Times in 527 Posts
devnull69 will become famous soon enough
So did you try to replace the string "ajax_info.txt" with the name (and path if necessary) of your PHP file that echoes some string? Because that would have worked ...
devnull69 is offline   Reply With Quote
Users who have thanked devnull69 for this post:
paddyfields (04-02-2012)
Old 04-02-2012, 11:16 PM   PM User | #3
paddyfields
Regular Coder

 
Join Date: Dec 2010
Location: London
Posts: 283
Thanks: 60
Thanked 11 Times in 11 Posts
paddyfields is an unknown quantity at this point
I didn't, but thank you. That works perfectly.
paddyfields is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:25 PM.


Advertisement
Log in to turn off these ads.