...

View Full Version : innerHTML and AJAX



kehers
06-03-2006, 02:00 PM
Im trying to use Ajax to validate a form field as soon as the user blurs from the field.
Here is what the code looks like:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD><TITLE>Demo 2 - Getting a page's content</TITLE>
<META http-equiv=Content-Type content="text/html; charset=windows-1252">
<SCRIPT type="text/javascript">
function alrt(){
alert("sup baby!")
}
function HttpRequest(url){
var pageRequest = false //variable to hold ajax object
/*@cc_on
@if (@_jscript_version >= 5)
try {
pageRequest = new ActiveXObject("Msxml2.XMLHTTP")
}
catch (e){
try {
pageRequest = new ActiveXObject("Microsoft.XMLHTTP")
}
catch (e2){
pageRequest = false
}
}
@end
@*/

if (!pageRequest && typeof XMLHttpRequest != 'undefined')
pageRequest = new XMLHttpRequest()

if (pageRequest){ //if pageRequest is not false
pageRequest.open('GET', url, false) //get page synchronously
pageRequest.send(null)
embedpage(pageRequest)
}
}

function embedpage(request){
//if viewing page offline or the document was successfully retrieved online (status code=2000)
if (window.location.href.indexOf("http")==-1 || request.status==200)
document.getElementById("contentdiv").innerHTML = request.responseText
//document.write(request.responseText)
}

</SCRIPT>

</HEAD>
<BODY>
<DIV id=contentdiv></DIV><INPUT onclick="HttpRequest('ds.php?staff_id=7');" type=button value="Get content">
</BODY></HTML>

Where ds.php goes this way:

<?php
include("db.inc");

$staff_id = $_REQUEST['staff_id'];

$query = "select staff_id from personnel_master where staff_id='$staff_id'";
$result = mysql_query($query);

if(!$result)
echo mysql_error();

if(mysql_num_rows($result) < 1)
echo "<div style='border: 1px solid red'>Staff with staff id: <b>$staff_id</b> not found!</div>";
?>

This works fine but when i tried echoing back a script
this way,


if(mysql_num_rows($result) < 1)
echo "
<script>
alert('Staff not found!');
</script>
";

it doesnt work. does anyone know how to get round this?

felgall
06-04-2006, 12:18 AM
Try putting the echo statement on one line and use \n to represent the new line characters in the output.

kehers
06-04-2006, 04:43 PM
doesnt work. :-(



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum