Jump to content

simple ajax getting an error on page problem


kwstephenchan

Recommended Posts

Hi!

I have the following php program :

<start of program >

<?php

$editFormAction = $_SERVER['PHP_SELF'];

?>

<html>

<head>

<title>Test Ajax</title>

<meta http-equiv="Content-Type" content="text/html; charset=gb2312">

<!-- Fireworks MX Dreamweaver MX target.  Created Sat May 03 12:18:20 GMT+0800 (x_?DCRE?!) 2003-->

<script src="testajax.js"></script>

 

<LINK REL=STYLESHEET HREF="apponwebstyle.css" TYPE="text/css">

 

</head>

<body bgcolor="#ffffff" link="#990000" vlink="#990000">

 

 

<table width="800" border="0" align="center" cellpadding="4" cellspacing="0">

  <tr>

    <td>

 

      <form action="<?php echo $editFormAction; ?>" method="POST" name="form1" style="margin:0px;" >

                    <table width="100%" border="0" cellspacing="0" cellpadding="1">

                      <tr valign="baseline">

                        <td width="40%" height="20" align="right" class="lgtxt">Company ID : </td>

                        <td height="20" class="lgtxt">

<input class="text" id="comid" type="text" size="20" >            

                        <button class="ebtn" onClick="showHint()">Hint</button>

 

 

  </td>

  </tr>

  </table>

 

  </td>

              </tr>

            </table>

         

 

 

</body>

</html>

 

<end of program>

 

and an ajax script as follow :

 

<start of ajax script>

 

var xmlHttp

 

function showHint()

{

/*

if (str.length==0)

  {

  document.getElementById("comid").innerHTML="";

  return;

  }

*/

xmlHttp=GetXmlHttpObject()

if (xmlHttp==null)

  {

  alert ("Your browser does not support AJAX!");

  return;

  }

var url="ajaxfindcompany.php";

url=url+"?sid="+Math.random();

xmlHttp.onreadystatechange=stateChanged;

xmlHttp.open("GET",url,true);

xmlHttp.send(null);

}

 

function stateChanged()

{

if (xmlHttp.readyState==4)

{

document.getElementById("comid").innerHTML=xmlHttp.responseText;

}

}

 

function GetXmlHttpObject()

{

var xmlHttp=null;

try

  {

  // Firefox, Opera 8.0+, Safari

  xmlHttp=new XMLHttpRequest();

  }

catch (e)

  {  alert ("Your browser does not support AJAX!");

 

  // Internet Explorer

  try

    {

    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");

    }

  catch (e)

    {

    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");

    }

  }

return xmlHttp;

}

 

<end of ajax script>

 

the js function "showHint" was executed alright until "xmlHttp.send(null);"

it shows a message at the bottom saying error on page without loading the ajaxfindcompany.php program.

Can someone help ?

 

 

Link to comment
Share on other sites

Hi, I was still not getting any error message except "error on page" at the bottom left of the brower.

I was working on IE 7.

I have trimmed down the code, basically what the following codes are doing is to open an URL object and send it  and all the the url php program does is to display a few label (nothing shows up though). So I really don't know what have I done wrong.

The following are all the codes I have, hope the trimming can help isolate the problem. By the way, I am completely new to both Javascript and Ajax and am really trying to learn it.  It can be a very stupid mistake or I didn't quite grasp the concept. Thanks.

 

Testajax.php

 

<?php

$editFormAction = $_SERVER['PHP_SELF'];

?>

<html>

<head>

<title>Test Ajax</title>

<meta http-equiv="Content-Type" content="text/html; charset=gb2312">

<!-- Fireworks MX Dreamweaver MX target.  Created Sat May 03 12:18:20 GMT+0800 (x_?DCRE?!) 2003-->

<script src="testajax.js"></script>

</head>

<body bgcolor="#ffffff" link="#990000" vlink="#990000">

<table width="800" border="0" align="center" cellpadding="4" cellspacing="0">

  <tr>

    <td>

      <form action="<?php echo $editFormAction; ?>" method="GET" name="form1" style="margin:0px;" >

                    <table width="100%" border="0" cellspacing="0" cellpadding="1">

                      <tr valign="baseline">

                        <td width="40%" height="20" align="right" >Company ID : </td>

                        <td id="main" height="20" >

<input class="text" id="txtHint" type="text" size="20" >            

                        <button class="ebtn" onClick="showHint()">Hint</button>

</td>

</tr>

</table>

 

</td>

</tr>

</table>

</body>

</html>

 

testajax.js

 

var xmlHttp

 

function showHint()

{

xmlHttp=GetXmlHttpObject()

if (xmlHttp==null)

  {

  alert ("Your browser does not support AJAX!");

  return;

  }

var url="ajaxfindcompany.php";

//  alert ("AJAX 01!");

url=url+"?sid="+Math.random();

xmlHttp.onreadystatechange=stateChanged;

//  alert ("AJAX 02!");

xmlHttp.open("GET",url,true);

//  alert ("AJAX 03!");

xmlHttp.send(null);

//  alert ("AJAX 04!");

}

 

function stateChanged()

{

if (xmlHttp.readyState==4)

{

document.getElementById("txtHint").innerHTML=xmlHttp.responseText;

}

}

 

function GetXmlHttpObject()

{

var xmlHttp=null;

try

  {

  // Firefox, Opera 8.0+, Safari

  xmlHttp=new XMLHttpRequest();

  }

catch (e)

  {  alert ("Your browser does not support AJAX!");

 

  // Internet Explorer

  try

    {

    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");

    }

  catch (e)

    {

    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");

    }

  }

return xmlHttp;

}

 

 

findajaxcompany.php

 

<html>

<head>

<title></title>

<meta http-equiv="Content-Type" content="text/html; charset=gb2312">

<!-- Fireworks MX Dreamweaver MX target.  Created Sat May 03 12:18:20 GMT+0800 (x_?DCRE?!) 2003-->

<script language="JavaScript">

<!--

//-->

</script>

</head>

<?php

?>

<table width="800" border="0" align="center" cellpadding="4" cellspacing="0">

  <tr valign="top">

    <td width="800">

  <table width="100%" border="0" cellspacing="4" cellpadding="0">

        <tr valign="top">

          <td>

        <table width="98%" border="0" align="center" cellpadding="0" cellspacing="0" >

              <tr>

                <td width ="80%" ><font color="#FFFFFF"><strong><font size="3">

                  Company</font></strong></td>

                  </td>

              </tr>

        </table>

 

<table width="98%" border="0" align="center" cellpadding="4" cellspacing="1">  

              <tr>

                <td>

<table width="100%" border="0" cellspacing="2" cellpadding="2">

                    <tr valign="baseline" >

                      <td width="15%" align="center"><strong><font color="#FFFFFF" size="2">Company ID</font></strong></td>

                      <td width="75%" align="center"><strong><font color="#FFFFFF" size="2">Company Name</font></strong></td>

                      <td width="10%" height="16" align="center"><strong><font color="#FFFFFF" size="2"> </font></strong></td>

                    </tr>

 

                  </table></td>

              </tr>

    </table>  

  </td>

  </tr>

  </table>  

  </td>

  </tr>

  </table>

</body>

</html>

 

Link to comment
Share on other sites

Hi, I have installed firefox, firebug, add firebug to firefox but I am still struggling to learn how to use firebug to debug. As I said, I did not see any error. The closest I got is the following :

 

错误: jsd is undefined

源文件:file:///C:/Documents%20and%20Settings/stephenchan/Application%20Data/Mozilla/Firefox/Profiles/cuutziqz.default/extensions/firebug@software.joehewitt.com/components/firebug-service.js

行:781

 

that is when I press ctrl+shift+j . I do not see how it is related to the program testajax nor the testajax.js .

 

I may be asking too much, but could you copy the code I previously posted and see what you get.  Those are all the codes that I have.

 

Thanks a million.

 

Link to comment
Share on other sites

That's exactly the information you need but I agree it seems to be coming from Firebug not your code.

 

My problem is that when i look at your code i don't see what it is supposed to do.  Normally AJAX calls a PHP page which returns (echoes) some data back.  Yours does not seem to do that so I can't see what it is supposed to do.  I am certainly not an AJAX expert but the concept seems easy enough:

 

Javascript sends a request to a PHP page perhaps with data being passed through GET.  The PHP uses an echo statement to send data back to Javascript.  Javascript puts that data on the page.  I did an ultra simplified example of AJAX (http://www.yourwebskills.com/ajaxintro.html) complete with pastable code a while back.  Have a look.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.