Upcoming SlideShare
Loading in...5







Total Views
Views on SlideShare
Embed Views



0 Embeds 0

No embeds



Upload Details

Uploaded via as Microsoft Word

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
Post Comment
Edit your comment

Ajax.doc.doc Ajax.doc.doc Document Transcript

  • [PHP and AJAX MySQL Database] With the help of Ajax we can retrieve any value or records from database without refreshing a PHP page. Example of PHP and AJAX Mysql database Records in database table id Name Address Class 1 Gaurav Singh Fatehpur M. Tech Chandel 2 Syed Faiz EWS 67 KDA Colony Jajmau MCA Akhatar S T E P 1 :::: Take a simple HTML Form :::: <html> <head> <script src="choosename.js"></script> </head> <body> <form> Select a name: <select name="name" onchange="showname(this.value)"> <option value="1">faiz</option> <option value="2">gaurav</option> </select> </form> <p> <div id="place"><b>Information will display
  • here</b></div> </p> </body> </html> Explanation of HTML Form This HTML form includes a drop down box "name" with “names” and "id" from the database. There is a form that involves a div name "place". it is used here for records that retrieved from the server. there is a function used name "showname()" executes on Onchange event. S T E P 2 :::: Java Script Coding :::: The name of the file that you need to save is choosename.js (This is the javascript that you need to save in the name choosename.js) Choosename.js var xmlHttp function showname(str) { xmlHttp=GetXmlHttpObject() if (xmlHttp==null) { alert ("Browser does not support HTTP Request") return } var url="hello.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("place").innerHTML=xmlHttp.responseTex t } } function GetXmlHttpObject() { var xmlHttp=null; try { xmlHttp=new XMLHttpRequest(); } catch (e) { try { xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); } } return xmlHttp; } S T E P 3 :::: The PHP Page :::: The PHP page used by the above javascript page name "hello.php". This page includes HTML and PHP coding to retrieve & print values. hello.php <?php $q=$_GET["q"];
  • $con = mysql_connect('localhost', 'root', ''); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("tutorial", $con); $sql="SELECT * FROM details WHERE id = '".$q."'"; $result = mysql_query($sql); echo "<table border='1'> <tr> <th>Name</th> <th>Address</th> <th>Class</th> </tr>"; while($row = mysql_fetch_array($result)) { echo "<tr>"; echo "<td>" . $row['name'] . "</td>"; echo "<td>" . $row['address'] . "</td>"; echo "<td>" . $row['class'] . "</td>"; echo "</tr>"; } echo "</table>"; mysql_close($con); ?> In this form (hello.php) get the id of the selected option <?php $q=$_GET["q"]; after that the coding given below help to get connection with database. As we told you earlier that the name of database is tutorial
  • $con = mysql_connect('localhost', 'root', ''); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("tutorial", $con); This is query to select the table. $sql="SELECT * FROM details WHERE id = '".$q."'"; $result = mysql_query($sql); This form includes HTML coding also. It is used here for table creation of table. echo "<table border='1'> <tr> <th> Name </th> <th> Address </th> <th> Class </th> </tr>"; With the help of this syntax we will fetch the records. while($row = mysql_fetch_array($result)) { in the table that is below
  • echo "<tr>"; echo "<td>" . $row['name'] . "</td>"; echo "<td>" . $row['address'] . "</td>"; echo "<td>" . $row['class'] . "</td>"; echo "</tr>"; } echo "</table>"; mysql_close($con); ?> When you select an option from database the other values will echo like this. Ajax is simply used retrieve data or records from the database or xml files. So please do practice carefully if you do any mistake then it will not an easy task you to handle errors. [End]