Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Dec 2010
    Location
    London
    Posts
    333
    Thanks
    63
    Thanked 11 Times in 11 Posts

    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.

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,355
    Thanks
    11
    Thanked 558 Times in 551 Posts
    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 ...

  • Users who have thanked devnull69 for this post:

    paddyfields (04-02-2012)

  • #3
    Regular Coder
    Join Date
    Dec 2010
    Location
    London
    Posts
    333
    Thanks
    63
    Thanked 11 Times in 11 Posts
    I didn't, but thank you. That works perfectly.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •