Ajax implements page loading and content deletion, and ajax implements page loading

Source: Internet
Author: User

Ajax implements page loading and content deletion, and ajax implements page loading

The biggest advantage of ajax is that it does not jump to pages during loading or deletion. Most of the current web pages are written using ajax, which reduces the amount of code compared to embedded PHP code, at the same time, loading the page will be faster. The following is how to use ajax to write the loading page and delete the fruit using the database fruit table as an example. At the beginning, ajax may be quite easy to write, it should be a good exercise.

Here is the fruit table:

The following is the homepage code. First, create a php file main. php.

<Body> 

I chose to display only the fruit name Price and origin columns in the fruit table on the page. Next we will write the loading processing page and create a php file named jiazaiym. php.

<? Phpinclude ("DADB. class. php "); $ db = new DADB (); $ SQL =" select * from fruit "; $ arr = $ db-> Query ($ SQL ); $ str = ""; foreach ($ arr as $ v) {$ str = $ str. implode ("^", $ v ). "|"; // use "|" to connect each row, so that an extra "|"} $ str = substr ($ str, 0, strlen ($ str) -1); // Delete the last extra "|" in the string truncation mode echo $ str;?>

After loading the Page code, you can officially write ajax, which should be written in main. php.

<Script type = "text/javascript"> $. ajax ({url: "jiazaiym. php ", dataType:" TEXT ", success: function (data) {var str =" "; var hang = data. split ("|"); for (var I = 0; I 

Note:When writing ajax, pay special attention to the semicolons and commas in them. I always write them as semicolons and the results cannot be output. After checking that the code is correct, this is a headache.

After loading the page, we will start writing and deleting the page. It is very easy to delete the page by creating the php file shanchu. php, which is similar to embedding php directly.

<?php$ids=$_POST["ids"];include("DADB.class.php");$db=new DADB();$sql="delete from fruit where ids={$ids}";if($db->Query($sql,0)){ echo"OK";}else{ echo"flase";} 

Next, when I want to re-write an ajax file, I will find that the file is not run after it is written, because the class in the file is not recognized when the page is loaded, in this case, I need to put the deletion in the loaded ajax, and encapsulate the loading into a method. I can call the method when deleting it.

<Script type = "text/javascript"> Load (); function Load () {$. ajax ({url: "jiazaiym. php ", dataType:" TEXT ", success: function (data) {var str =" "; var hang = data. split ("|"); for (var I = 0; I 

In this way, there is no problem.

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.