John Benson
11-20-2008, 09:27 PM
I am using text box with javascript, php and mysql to update a div and I can update the first div but the rest of the div's are left as they were
I know I need to loop thru the DOM but I dont know to set it up
here is what I have
<html>
<body>
<script language="javascript" type="text/javascript">
function ajaxFunction(){
var ajaxRequest; // The variable that makes Ajax possible!
try{
ajaxRequest = new XMLHttpRequest();
} catch (e){
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;
}
}
}
// Function to receive data sent from the server and update div's
ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState == 4){
var ajaxDisplay = document.getElementById('citystate');
ajaxDisplay.innerHTML = ajaxRequest.responseText;
}
}
var zipcode = document.getElementById('zipcode').value;
var queryString = "?zipcode=" + zipcode ;
ajaxRequest.open("GET", "ajax-example.php" + queryString, true);
ajaxRequest.send(null);
}
</script>
<form name='myForm'>
Zip Code: <input type='text' id='zipcode' /> <br />
<input type='button' onclick='ajaxFunction()' value='Query MySQL' />
</form>
<div id='citystate'>Your result will display here</div>
</body>
<div id='citystate'>Your result will display here</div>
</body>
</html>
the above code works but It only updates the first div and leaves the rest as they were
I'm sure that this is where I need to loop threw the DOM
ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState == 4){
var ajaxDisplay = document.getElementById('citystate');
ajaxDisplay.innerHTML = ajaxRequest.responseText;
}
}
Because I can use a unique ID like citystate1, citystate2 ... which works but Idealy Id like to know how to do it the right way that means looping and I don't know what I'm missing when I try...
I know I need to loop thru the DOM but I dont know to set it up
here is what I have
<html>
<body>
<script language="javascript" type="text/javascript">
function ajaxFunction(){
var ajaxRequest; // The variable that makes Ajax possible!
try{
ajaxRequest = new XMLHttpRequest();
} catch (e){
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;
}
}
}
// Function to receive data sent from the server and update div's
ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState == 4){
var ajaxDisplay = document.getElementById('citystate');
ajaxDisplay.innerHTML = ajaxRequest.responseText;
}
}
var zipcode = document.getElementById('zipcode').value;
var queryString = "?zipcode=" + zipcode ;
ajaxRequest.open("GET", "ajax-example.php" + queryString, true);
ajaxRequest.send(null);
}
</script>
<form name='myForm'>
Zip Code: <input type='text' id='zipcode' /> <br />
<input type='button' onclick='ajaxFunction()' value='Query MySQL' />
</form>
<div id='citystate'>Your result will display here</div>
</body>
<div id='citystate'>Your result will display here</div>
</body>
</html>
the above code works but It only updates the first div and leaves the rest as they were
I'm sure that this is where I need to loop threw the DOM
ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState == 4){
var ajaxDisplay = document.getElementById('citystate');
ajaxDisplay.innerHTML = ajaxRequest.responseText;
}
}
Because I can use a unique ID like citystate1, citystate2 ... which works but Idealy Id like to know how to do it the right way that means looping and I don't know what I'm missing when I try...