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
- Summary - How to play Pokemon GO, virtual reality game catch Pokemon on smartphonepokemon go game (styled, raising and training virtual animals) has become popular, the online community with new movements also immediately responded to this mobile game in the direction positive direction
- Things you didn't know about 26 types of Pokeball - Part 2to be able to catch pokemon, players need to use pokeball. the higher the ball, the higher the success rate of pokemon
- Stardust's effective way in Pokemon GOpokemon go stardust is used to evolve pokemom with candy. therefore, if there is more stardust, it will make the evolution easier.
- AZ word about eggs in Pokemon Gothere are many different pokemon eggs that can hatch into many pokemon, but to make these eggs hatch into pokemon, you will have to perform some tasks. each player begins with an egg incubator, which allows the user to incubate an egg after performing certain steps, gps and pedometer tracking on the mobile device of you and the game.
- Things you didn't know about 26 types of Pokéball - Part 1pokéball is the most important item in pokémon go, allowing gamers to quickly catch pokémon back to their team. the higher the pokéball, the more effective the ability to catch pokémon.
- 10 tips to help you become a great Pokemon trainerin the article below, tipsmake.com will introduce to you some tips to easily become a great trainer in this pokemon go game offline ...
- How to use GoChat application in Pokémon GOpokémon go has become a very hot phenomenon since its debut. any information or tricks related to the game are read and applied by players during the process of catching pokémon. and the gochat chat app for pokémon go players brings space to capture pokémon much more interesting.
- How to play Pokemon GO in Landscape Mode on the iPhonealthough players can play pokemon go in portrait mode. however, if you want to watch and play games on a large and eye-catching screen, players can switch to playing games in landscape mode.
- The secret to controlling Pokemon Go employees at workthese days, hr managers are faced with an extremely painful problem that is the status of priority employees playing pokemon go more than work. this has caused a small impact on productivity and efficiency.
- Check out the 'buffalo' Pokémon in Pokémon Goeach type of pokemon has hp, cp, ability to attack and endure differently. based on these indicators, players can determine as well as choosing the most powerful pokemon for their offensive tactics.