villabob Posted April 7, 2008 Share Posted April 7, 2008 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! Quote Link to comment Share on other sites More sharing options...
villabob Posted April 8, 2008 Author Share Posted April 8, 2008 hey so can anyone help me please? im really stuck... Quote Link to comment Share on other sites More sharing options...
zenag Posted April 8, 2008 Share Posted April 8, 2008 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> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.