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 03-17-2008, 06:30 AM   PM User | #1
pphill2
New to the CF scene

 
Join Date: Jan 2007
Posts: 8
Thanks: 4
Thanked 0 Times in 0 Posts
pphill2 is an unknown quantity at this point
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";
?>
pphill2 is offline   Reply With Quote
Old 03-17-2008, 08:16 AM   PM User | #2
abduraooft
Supreme Master coder!

 
abduraooft's Avatar
 
Join Date: Mar 2007
Location: N/A
Posts: 14,689
Thanks: 158
Thanked 2,184 Times in 2,171 Posts
abduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really nice
I use www.modernmethod.com/sajax/, no hassles!
__________________
Quote:
The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)
abduraooft is offline   Reply With Quote
Old 03-17-2008, 03:42 PM   PM User | #3
A1ien51
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
A1ien51 will become famous soon enough
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]
A1ien51 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 09:34 AM.


Advertisement
Log in to turn off these ads.