PHP & AJAX
What is AJAX?
AJAX stands for A synchronous J avaScript and X ML. AJAX is a new technique for creating better, faster, and more interactive web applications with the help of XML, HTML, CSS, and JavaScript.
Web applications follow the convention of transferring information to and from the Server by using synchronous requests. That is, you enter a form, click the Submit button, and be directed to a new page with new information from that Server.
With AJAX, when the Submit button is pressed, JavaScript will create a request to the Server, interpret the results and update the current screen. Users will never know what is transmitted to the Server.
Examples of PHP and AJAX
To clearly illustrate that it is easy to access information from a Database using AJAX and PHP, we build MySQL queries and display results on "ajax.html". But before proceeding, we need to do basic work. Create a Table using the following command.
CREATE TABLE `ajax_example` (
`name` varchar (50) NOT NULL,
`age` int (11) NOT NULL,
`sex` varchar (1) NOT NULL,
`wpm` int (11) NOT NULL,
PRIMARY KEY (`name`)
)
Now, pass the following data into this Table using the following SQL commands:
INSERT INTO `ajax_example` VALUES ('Male', 120, 'm', 20);
INSERT INTO `ajax_example` VALUES ('Wild', 75, 'm', 44);
INSERT INTO `ajax_example` VALUES ('Manh', 45, 'm', 87);
INSERT INTO `ajax_example` VALUES ('Huong', 22, 'f', 72);
INSERT INTO `ajax_example` VALUES ('Kien', 27, 'f', 0);
INSERT INTO `ajax_example` VALUES ('Middle', 35, 'f', 90);
HTML file on the Client side
Now we have the HTML file on the client side is ajax.html and it will have the following code:
Note - The way of passing variables in Query is to follow HTTP standard and have the following form.
URL? Variable1 = value1; & variable2 = value2;
PHP file on Server side
At this point, the client script is ready. Now we have to write the script on the Server side, which will get age, wpm and sex from the Database and will send it back to the Client. Put the following line of code in "ajax-example.php" file.
php $dbhost = "localhost" ; $dbuser = "tennguoidung" ; $dbpass = "matkhau" ; $dbname = "tencosodulieu" ; //Kết nối với MySQL Server mysql_connect ( $dbhost , $dbuser , $dbpass ); //chọn Database mysql_select_db ( $dbname ) or die ( mysql_error ()); // lấy dữ liệu $age = $_GET [ 'age' ]; $sex = $_GET [ 'sex' ]; $wpm = $_GET [ 'wpm' ]; // phần này giúp ngăn cản SQL Injection $age = mysql_real_escape_string ( $age ); $sex = mysql_real_escape_string ( $sex ); $wpm = mysql_real_escape_string ( $wpm ); //truy vấn $query = "SELECT * FROM ajax_example WHERE sex = '$sex'" ; if ( is_numeric ( $age )) $query .= " AND age <= $age" ; if ( is_numeric ( $wpm )) $query .= " AND wpm <= $wpm" ; //thực thi truy vấn $qry_result = mysql_query ( $query ) or die ( mysql_error ()); //định dạng chuỗi kết quả $display_string = "" ; $display_string .= "" ; $display_string .= "Name" ; $display_string .= "Age" ; $display_string .= "Sex" ; $display_string .= "WPM" ; $display_string .= "" ; // chèn một hàng mới vào trong bảng while ( $row = mysql_fetch_array ( $qry_result )){ $display_string .= "" ; $display_string .= "$row[name]" ; $display_string .= "$row[age]" ; $display_string .= "$row[sex]" ; $display_string .= "$row[wpm]" ; $display_string .= "" ; } echo "Truy vấn: " . $query . "
" ; $display_string .= "" ; echo $display_string ; ?>
Follow tutorialspoint
Previous article: Multithread (Multithread) in C #
Next lesson: PHP & XML
You should read it
- AJAX - the magic combination of web technology
- jQuery Ajax
- Ajax in AngularJS
- Atlas: Ajax in Microsoft style
- Ajax programming techniques
- How to structure XML for interactive Web applications?
- How to Enable Cross Origin Resource Sharing (CORS) for Sharing Resource Using Apache Servers, PHP and Jquery
- Include syntax in AngularJS
May be interested
- Object-oriented programming in PHPwe can imagine that the universe was made up of different objects like the sun, the moon, the earth, and so on. in the same way, you can imagine a car made of different objects like wheel, rudder, gear shift ... in such a way, concepts in object-oriented programming assume that everything is like an object and deploying a software by using different objects.
- PHP & XMLxml is a markup language that is quite similar to html. an xml document is plain text and contains tags limited by. there are two major differences between xml and html.
- PHP for Programmer Cthe easiest way to think of php is as c is interpreted that you can embed in html documents. php is quite similar to c, except for untyped variables, web-specific libraries are available, and everything is hooked up directly to your favorite web server.
- PHP for PERL Programmersthis chapter will list some of the major similarities and differences between php and perl. this helps perl programmers understand php faster and avoid common errors.
- Date & Time in PHPdate is a part of everyday life and it becomes easy to work with them without thinking. php also provides powerful tools for easier date manipulation.
- Great progress of PHP 5php 5.0 officially released on july 13 is one of the events that has attracted attention in the developer community. php is currently the most commonly used programming language for building dynamic websites with 16,251,453 domains, and 1,346,521 ip addresses, as of july 18, 2004.