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
    New to the CF scene
    Join Date
    Jan 2007
    Posts
    8
    Thanks
    4
    Thanked 0 Times in 0 Posts

    book example wont work, check code please

    Trying to use Ajax to improve some things I do with PHP. I am new to Javascript.

    I have taken a simple code sample from a Sams book and changed a few small details. I'm attempting take text outputted from an echo statement in a php document and display it in the original file using ajax.

    From the looks of the examples this should work yet nothing is displayed. If it turns out to be a simple typo I apologize.. looked over it many times and cannot fix it by myself.

    ajax.php

    Code:
    <html>
    <head>
    <title>Ajax Man!</title>
    </head>
    <script language="JavaScript" type="text/javascript">
    function getXMLHTTPRequest() {
    try {
    req = new XMLHttpRequest(); /* e.g. Firefox */
    } catch(e) {
      try {
      req = new ActiveXObject("Msxml2.XMLHTTP");  /* some versions IE */
      } catch (e) {
        try {
        req = new ActiveXObject("Microsoft.XMLHTTP");  /* some versions IE */
        } catch (E) {
          req = false;
        } 
      } 
    }
    return req;
    }
    
    var http = getXMLHTTPRequest();
      
    function callAjax() {
    	var pass = document.ajaxform.textinput.value;
    	var myRand = parseInt(Math.random()*999999999999999);
    	var myurl = "ajax_handle.php?pass=" + pass + "&rand=" + myRand;
    	
    	http.open("GET", myurl, true);
    	http.onreadystatechange = responseAjax;
    	http.send(null);
    }
    
    function responseAjax() {
    	
    	if(http.readyState == 4) {
    		if(http.status == 200) {
    		var txt = http.responseText;
    		document.getElementById('display').innerHTML = txt;
    	
    		} else {
    		alert("An error has occurred: " + http.statusText);
    		
    		}
    	}
    }
    </script>
    <body>
    <form name="ajaxform">
    <table>
    <tr><td></td><td id="display"></td></tr>
    <tr><td>Input: </td><td><input name="textinput" type="text"></td></tr>
    <tr><td></td><td><input type="submit" value="Enter" onClick="callAjax()"></td></tr>
    </table>
    </form>
    </body>
    </html>
    ajax_handle.php

    PHP Code:
    <?php
    echo "Hi there son";
    ?>

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,801
    Thanks
    160
    Thanked 2,216 Times in 2,203 Posts
    Blog Entries
    1
    I use www.modernmethod.com/sajax/, no hassles!
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #3
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    Looks like you are not canceling the submit buttons action so it will refresh the page. either add return false to the onclick or change the submit button to a plain old button.

    Eric
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]


  •  

    Posting Permissions

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