Clear, practical technology insights About · Contact

PHP && AJAX

Explore PHP && AJAX with clear explanations, practical examples, and useful tips.

Published: 4 minutes read
Table of Contents

This guide covers PHP && ajax with practical context and easy-to-follow details. Use it to understand the subject and apply the information confidently.

What is AJAX?

Web apps follow the convention of transferring information to and from the Server by using synchronous requests. That is, you enter a form, select 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

Frequently Asked Questions

What should I check before following these steps?

Confirm device and software compatibility, save important data, and make sure you have the required permissions, files, and account access.

Why might the process not work?

Common causes include outdated software, missing permissions, incompatible hardware, an unstable connection, or completing a step in the wrong order.

Can I undo the changes if necessary?

That depends on the tool or setting. Use built-in restore options when available, keep a backup, and record the original configuration first.

Was this article helpful?

Your feedback helps us improve.

Discussion

Reader Comments 0

Sign in with email or Google to join the discussion.