PHP and AJAX database connection -


my problem whenever run index.html file result of ajax fast , page reloads .thus, no output shows in div element.

html page (index.html)

 <!doctype html>  <head>  <script src="ajax.js"></script>  </head>  <body>   <form onsubmit='sendempid(this.roll)' action='index.html'>   <input id='roll' type='text'>  <button type="submit">submit</button>  </form>  <p>  <div id="emp"><b>employee info listed here.</b></div>  </p>  </body>   </html> 

thus follows ajax code:

var xmlhttp; function sendempid(str) {      xmlhttp=getxmlhttpobject();     if (xmlhttp==null)     {         alert ("browser not support http request")         return;     }     var url="getemployee.php";     url=url+"?q="+str;     url=url+"&sid="+math.random();     xmlhttp.onreadystatechange=statechanged;      xmlhttp.open("get",url,true);     xmlhttp.send(null); } function statechanged()  {      if (xmlhttp.readystate==4 || xmlhttp.readystate=="complete")     {          document.getelementbyid("emp").innerhtml=xmlhttp.responsetext;      }  } function getxmlhttpobject() {     var xmlhttp=null;     xmlhttp=new xmlhttprequest();     return xmlhttp;  } 

and php part(getemployee.php)

<html> <head>  <?php $q=$_get["q"]; $con = mysql_connect('localhost', 'root'); if (!$con)  {     die('not able connect: ' . mysql_error()); } mysql_select_db("emp", $con); $query="select * ajax_example id = '".$q."'"; $result = mysql_query($query); echo "<table border='1'> <tr> <th>name</th> <th>age</th> <th>gender</th> <th>wpm</th> </tr>";  while($row = mysql_fetch_array($result)) {     echo "<tr>";     echo "<td>" . $row['name'] . "</td>";     echo "<td>" . $row['age'] . "</td>";     echo "<td>" . $row['sex'] . "</td>";     echo "<td>" . $row['wpm'] . "</td>";     echo "</tr>"; } echo "</table>";  mysql_close($con); ?> </head> </html> 

database content:

id  name    age sex     wpm 1   frank   45  male    87 2   jerry   23  male    20 3   jill    22  female  72 4   julie   35  female  90 5   regis   75  male    44 6   tracy   27  female  0 

so whenever run index.html , insert 1 in input , press submit. no result displayed. after few tries noticed table appearing disappers quickly. please tell shall have timer set in ajax code or there problem in code.

thanks.


Comments