Jump to content

What is the problem?


villabob

Recommended Posts

Hi, i am learning AJAX and Javascript, so im new.

 

So whats wrong with this?

 

ajax_test.php:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Script-Type" content="text/javascript">
<title>Untitled Document</title>
<SCRIPT language="JavaScript" SRC="ajax.js">

function print_table() {
//document.write("Printing Table...");
document.getElementById("result").innerHTML=xmlHttp.responseText;
}
function search_tbl () {
//	ajaxCallback = print_table;
alert("Before ajax_php");
document.write("Before ajax_php");
ajaxRequest("ajax_php.php"); 
document.write("After ajax_php.");

}
</script>
</head>
<body>
<p>
<form id="form1" name="form1" method="post" action="javascript:search_tbl();">
  <label>
  <input type="submit" name="button" id="button" value="Go" />
  </label>
</form>
<p>
<p>  </p>
<div id="result"></div>
<p> </p>
</body>
</html>

 

 

ajax.js:

var ajaxreq=false, ajaxCallback;
// ajaxRequest: Sets up a request
function ajaxRequest(filename) {
	alert("hi");
   try {
    // Firefox / IE7 / Others
    ajaxreq= new XMLHttpRequest();
   } catch (error) {
    try {
      // IE 5 / IE 6
      ajaxreq = new ActiveXObject("Microsoft.XMLHTTP");
    } catch (error) {
      return false;
    }
   }
   ajaxreq.open("GET",filename);
   ajaxreq.onreadystatechange = ajaxResponse;
   ajaxreq.send(null);
}
// ajaxResponse: Waits for response and calls a function
function ajaxResponse() {
   if (ajaxreq.readyState !=4)  {
   		document.write(ajaxreq.readyState);
   return;
   }
   if (ajaxreq.status==200) {
   alert("Request success: " + ajaxreq.statusText);
      // if the request succeeded...
      if (ajaxCallback) ajaxCallback();
   } else alert("Request failed: " + ajaxreq.statusText);
   return true;
}

 

ajax_php.php:

<?php
$host= "localhost";

$user= "*******";

$pass="*******";

$db= "*******";

echo "Connect DB has been called";

$cid = mysql_connect($host, $user, $pass);
mysql_select_db($db);

$sql = "SELECT * FROM *****";
$query = mysql_query($sql);
$num = mysql_numrows($query);
$i = 0
echo "You have reached the php file.";
echo "<table border='1'>
<tr>
<th>City</th>
</tr>";

while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['city'] . "</td>";
echo "</tr>";
}
echo "</table>";

mysql_close($con);
?>

 

So when ajax_php.php is called shouldn't print the table, and "PHP has been called" and "Connect DB has been called"?

 

So what am i doing wrong here?

 

Thanks for any help!

Link to comment
Share on other sites

ajax_test.php  page.....

 

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Script-Type" content="text/javascript">

<title>Untitled Document</title>

<SCRIPT language="JavaScript" SRC="ajax.js"></script>

<script type="text/javascript">

function print_table() {

//document.write("Printing Table...");

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

}

function search_tbl () {

// ajaxCallback = print_table;

alert("Before ajax_php");

document.write("Before ajax_php");

ajaxRequest("ajax_php.php");

document.write("After ajax_php.");

 

}

</script>

</head>

<body>

<p>

<form id="form1" name="form1" method="post"  >

  <label>

  <input type="submit" name="button" id="button" onclick="search_tbl()" value="Go" />

  </label>

</form>

<p>

<p>  </p>

<div id="result"></div>

<p> </p>

</body>

</html>

 

 

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.