View Full Version : innerHTML and AJAX

06-03-2006, 03: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
@if (@_jscript_version >= 5)
try {
pageRequest = new ActiveXObject("Msxml2.XMLHTTP")
catch (e){
try {
pageRequest = new ActiveXObject("Microsoft.XMLHTTP")
catch (e2){
pageRequest = false

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

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

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


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

Where ds.php goes this way:


$staff_id = $_REQUEST['staff_id'];

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

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 "
alert('Staff not found!');

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

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

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