+ Reply to Thread
Results 1 to 4 of 4

Thread: Need simple ajax/php help

  1. #1
    thenewprogrammer is offline x10Hosting Member thenewprogrammer is an unknown quantity at this point
    Join Date
    Jul 2009
    Posts
    45

    Need simple ajax/php help

    Trying to echo out a javascript from a javascript when i press button on html page which sounds stupid but just trying to get it to work. Nothing happens and im new to this and in search of answers.

    html page
    Code:
    <html>
    <head>
    <script type="text/javascript" src="clienthint.js"></script>
    </head>
    <body>
    <form>
    First Name: <input type="button" id="txt1" value="Click Me" onclick="sub()" />
    </form>
    <div id="txtHint"></div><p><a href="http://www.w3schools.com">
    <img border="0" alt="Visit W3Schools!" src="b_pink.png" id="b1" /></a></p>
    </body>
    </html>
    clienthint.js
    Code:
    var xmlhttp
    function sub(){
     
    xmlhttp=GetXmlHttpObject();
    var url="gethint.php";
    url=url+"&sid="+Math.random();
    xmlhttp.open("GET",url,true);
    xmlhttp.send(null);
    }
    function GetXmlHttpObject()
    {
    if (window.XMLHttpRequest)
      {
      // code for IE7+, Firefox, Chrome, Opera, Safari
      return new XMLHttpRequest();
      }
    if (window.ActiveXObject)
      {
      // code for IE6, IE5
      return new ActiveXObject("Microsoft.XMLHTTP");
      }
    return null;
    }
    gethint.php
    Code:
    <?php
    echo "<script type=\"text/javascript\">
     document.getElementById('txtHint').innerHTML = 'jimmy jones';
    </script>";
    ?>
    What suppose to happen is when i click the button it will say jimmy jones underneath it without refreshing where div txthint is at. Knew to this so point out how i can get this to work been stuck for while

  2. #2
    Scoochi2's Avatar
    Scoochi2 is offline x10 Sophmore Scoochi2 is an unknown quantity at this point
    Join Date
    Aug 2008
    Location
    Southport!
    Posts
    185

    Re: Need simple ajax/php help

    Replace the line in your HTML with the link to:
    HTML Code:
    First Name: <input type="button" id="txt1" value="Click Me" onclick="javascript:sub()" />
    ...might help?
    If anyone can see it, my post was meant for anyone who reads it. Don't take it personally or think I'm being condescending... :nuts:

  3. #3
    descalzo's Avatar
    descalzo is offline Grim Squeaker descalzo has a brilliant futuredescalzo has a brilliant futuredescalzo has a brilliant future
    Join Date
    Jul 2009
    Location
    Ankh-Morpork
    Posts
    7,636

    Re: Need simple ajax/php help

    Two problems.....

    The first, you are sending a request for a file but you are doing nothing with it.
    You have to include code to take the returned text and do something.


    clienthint.js
    Code:
    var xmlhttp
    function sub(){
     
    xmlhttp=GetXmlHttpObject();
    var url="gethint.php";
     
    #### MINOR ERROR...RUNS ANYWAY, BUT..
    #        url=url+"&sid="+Math.random();  # SHOULD BE
     
    url=url+"?sid="+Math.random();  # SINCE YOUR URL DOES NOT YET HAVE 
                                            # A QUERY STRING
     
    ## ALSO, IF YOU ARE ONLY USING THE FUNCTION ONCE,
    ##  OR IF THE INFORMATION RETURNED
    ## DOES NOT CHANGE, YOU DO NOT NEED TO ADD THE RANDOM NUMBER 
    ## TO PREVENT CACHING
     
    xmlhttp.open("GET",url,true);
     
    #####
    ##### STANDARD WAY TO HANDLE RETURNED INFORMATION
    #####  ASSIGN A FUNCTION TO THE onreadystatechange  OF YOUR REQUEST
    ##### THIS WILL BE CALLED WHEN IT CHANGES STATE
     
            xmlhttp.onreadystatechange = function(){
     
    ##### YOU WANT THE STATE TO BE '4' (COMPLETION)
    ##### AND YOU WANT THE STATUS TO BE 200 (IE GOOD RESPONSE FROM SERVER)
     
                if( xmlhttp.readyState == 4 && xmlhttp.status == 200 ){
     
    #####    IF YOU ARE RETURNING JAVASCRIPT, JUST eval() IT 
     
                    eval( xmlhttp.responseText ) ;
                }
             } ;
     
     
    xmlhttp.send(null);
    }

    Since you are going to eval() the returned Javascript, you do not need the script tags...

    gethint.php
    Code:
    <?php
    echo "document.getElementById('txtHint').innerHTML = 'jimmy jones';" ;
     
    ?>
    Nothing is always absolutely so.

  4. #4
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: Need simple ajax/php help

    In cases where eval() isn't safe, have the response handler set the content:
    Code:
    xmlhttp.onreadystatechange = function(){
        if( xmlhttp.readyState == 4 && xmlhttp.status == 200 ){
            document.getElementById('txtHint').innerHTML = xmlhttp.responseText;
        }
    } ;
    gethint.php (note: <?php and ?> tags unnecessary in this instance):
    PHP Code:
    jimmy jones 
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

+ Reply to Thread

Similar Threads

  1. Simple Page Updates w/Template?
    By rickybray in forum Graphics & Webdesign
    Replies: 2
    Last Post: 08-06-2009, 06:32 PM
  2. Simple problem: Where to upload??
    By tiedup in forum Free Hosting
    Replies: 3
    Last Post: 12-20-2008, 12:09 PM
  3. Simple PHP Email Sending Form
    By dquigley in forum Programming Help
    Replies: 10
    Last Post: 12-13-2008, 02:33 PM
  4. earn money,very simple
    By beandab in forum Earning Money
    Replies: 0
    Last Post: 07-13-2008, 06:26 PM
  5. Two very simple php file upload scripts-Free
    By Symbian.Ankit in forum Scripts & 3rd Party Apps
    Replies: 2
    Last Post: 02-20-2008, 09:55 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers