Ajax server

AJAX SERVER SIDE PHP

0  0 2016-08-03 07:48:17

AJAX-server side?
• Full form of AJAX is Asynchronous JavaScript and XML.
• AJAX is a new technique for creating good, very fast, and more interactive web applications with XML, HTML, CSS and Java Script.

Server Side PHP file

Now we are looking in to server side ajax-php file. For this server side php file assume client side file.
With database and fields like name, age, mail-id, contact number, sex etc
So assume now client side script is ready.
Now for client side script, we have to write our server side script which will fetch up name, age, mail-id, contact number, sex from the database and display to the client.

Assume following table in database:-
CREATE TABLE `stud` (
   `name` varchar(50) NOT NULL,
   `age` int(11) NOT NULL,
   ‘mail_id’ varchar(50) NOT NULL,
   `contact_number` int(10) NOT NULL,
   `asex` varchar(1) NOT NULL,
   
   PRIMARY KEY  (`contact_number`)
)

Now insert following data into this table using the following SQL qurries:-
INSERT INTO ` stud ` VALUES ('Nikhil', 25, 'ex@ex.com', 1234567890, ‘male’);
INSERT INTO ` stud ` VALUES ('Rahul', 25, 'ex@ex.com', 1234567890, ‘male’);
INSERT INTO ` stud ` VALUES ('Sachin', 25, 'ex@ex.com', 1234567890, ‘male’);
INSERT INTO ` stud ` VALUES ('Mayur', 25, 'ex@ex.com', 1234567890, ‘male’);
INSERT INTO ` stud ` VALUES ('Abhi', 25, 'ex@ex.com', 1234567890, ‘male’);
INSERT INTO ` stud ` VALUES ('Kishor', 25, 'ex@ex.com', 1234567890, ‘male’);

Following code will display values back to clients:-
<?php
        $db_host = "localhost";
        $db_user = "root";
        $db_pass = "";
        $db_name = "test";
   
        //Connect to MySQL Server which is localhost
        mysql_connect($db_host, $db_user, $db_pass);
   
        //Selecting Database
        mysql_select_db($dbname) or die(mysql_error());
   
        // retrieving values from Query
        $name = $_GET['name'];
        $age = $_GET['age'];
        $mail = $_GET['mail_id'];
        $con_no = $_GET['contact_number'];
        $sex = $_GET['sex'];
   
        // Escape User Input to help prevent SQL Injection
        $name = mysql_real_escape_string($name);
        $age = mysql_real_escape_string($age);
        $mail = mysql_real_escape_string($mail);
        $con_no = mysql_real_escape_string($con_no);
        $con_no = mysql_real_escape_string($con_no);
        $sex = mysql_real_escape_string($sex);

        //create SQL query
        $query = "SELECT * FROM stud WHERE name = '$name'";
   
        if(is_numeric($age))
        $query .= " AND age <= $age";
   
        if(is_numeric($wpm))
        $query .= " AND wpm <= $wpm";
   
        //Execute SQL query
        $qry_result = mysql_query($query) or die(mysql_error());
   
        //Display Result to client in tabular format
        $display_string = "<table>";
        $display_string .= "<tr>";
        $display_string .= "<th>Name</th>";
        $display_string .= "<th>Age</th>";
        $display_string .= "<th>Mail-id</th>";
        $display_string .= "<th>Contact Number</th>";
         $display_string .= "<th>Sex</th>";
         $display_string .= "</tr>";
   
         // these are row data with actual values
          while($row = mysql_fetch_array($qry_result)){
          $display_string .= "<tr>";
          $display_string .= "<td>$row[‘name’]</td>";
          $display_string .= "<td>$row[‘Age’]</td>";
          $display_string .= "<td>$row[‘mail_id’]</td>";
          $display_string .= "<td>$row[‘contact_number’]</td>";
          $display_string .= "<td>$row[‘sex’]</td>";
          $display_string .= "</tr>";
}
echo "Query: " . $query . "<br />";
$display_string .= "</table>";
echo $display_string;
?>

 

Comments (0)

Post Your Comments

Headquarter



Hong Kong Office



Find Us On

Ziuby
Ziuby