0

Hi All,

Im trying to get the following example to update without the need to click but cant get the bugger to work. The file its referring to is just echoing hello, im fine with setting that page up when its finished.

all im getting is a white page.

<html>
<body>

<script language="javascript" type="text/javascript">
<!-- 
//Browser Support Code
function ajaxFunction(){
	var ajaxRequest;  // The variable that makes Ajax possible!
	
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Your browser broke!");
				return false;
			}
		}
	}
	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4){
			var ajaxDisplay = document.getElementById('ajaxDiv');
			ajaxDisplay.innerHTML = ajaxRequest.responseText;
			//setTimeout('Ajax()',100);

		}
	}
	//var age = document.getElementById('age').value;
	//var wpm = document.getElementById('wpm').value;
	//var sex = document.getElementById('sex').value;
	var queryString = "?age=" + age + "&wpm=" + wpm + "&sex=" + sex;
	ajaxRequest.open("GET", "example.php" + queryString, true);
	ajaxRequest.send(null); 
}

</script>



<!--<form name='myForm'>
Max Age: <input type='text' id='age' /> <br />
Max WPM: <input type='text' id='wpm' />
<br />
Sex: <select id='sex'>
<option value='m'>m</option>
<option value='f'>f</option>
</select>
<input type='button' onclick='ajaxFunction()' value='Query MySQL' />
</form>-->
<div id='ajaxDiv'></div>
</body>
</html>
3
Contributors
2
Replies
3
Views
6 Years
Discussion Span
Last Post by vibhaJ
0

Im trying to get the following example to update without the need to click

You need to call the function somehow. If you want to do this upon page load then try:

<script type="text/javascript">>
//the stuff you currently have goes here
//...

//then right BEFORE you close the SCRIPT tag put
window.onload=ajaxFunction;//NO parenthesis
</script>

Edited by hielo: n/a

0

Why dont you try jquery.
Its easy to use.
Check this code.

<html>
<body>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.4.4.min.js">
<script language="javascript" type="text/javascript">
function ajaxFunction(age,wpm,sex)
{
	$.ajax({
	  url: 'example.php?age='+age+"&wpm="+wpm+"&sex="+sex,
	  success: function(data) {
		$('.ajaxDiv').html(data);    
	  }
	});
}
</script>
<input type='button' onclick='ajaxFunction(1,2,3)' value='Query MySQL' />
<div id='ajaxDiv'></div>
</body>
</html>
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.