PHP & AJAX

AJAX stands for Asynchronous JavaScript and XML. AJAX is a new technique for creating better, faster, and more interactive web applications with the help of XML, HTML, CSS, and JavaScript.

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:

  language = "javascript" type = "text/javascript" >   name = 'myForm' > Max Age:  type = 'text' id = 'age' />  /> Max WPM:  type = 'text' id = 'wpm' />  /> Sex:  id = 'sex' >  value = "m" > m  value = "f" > f  type = 'button' onclick = ' ajaxFunction () ' value = 'Query MySQL' />  id = 'ajaxDiv' > Kết quả của bạn sẽ hiển thị ở đây!!! 

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

5 ★ | 1 Vote