jquery Ajax Delete Detailed _jquery

Source: Internet
Author: User

First look at the HTML interface code:

<a href= "javascript:;" onclick= "Delete_order (' <?php echo $item [order_id];?> ')" ></a> this one picture link, which means delete. It does not jump to an interface, but instead clicks off the Click event.

The Click event executes the Delete_order () method. This method passes a parameter, the order number. With this parameter, you can perform the appropriate delete operation.

The following jquery code:

Copy Code code as follows:

<script>
function Delete_order (order_id) {
confirm_ = Confirm (' This action would delete current order! Are you sure? ');
if (confirm_) {
$.ajax ({
Type: "POST",
URL: ' index.php/admin/order/del/' +order_id,
Success:function (msg) {
Alert ("Test order");
All the delete is Success,this can execute
$ ("#tr_" +order_id). Remove ();
}
});
}
};
</script>

First set out a prompt box, confirm delete?

If confirmed, perform AJAX operations.

In jquery, there is a $.ajax () method.

Here are three parameters, one is type, and here is the post,

The second is the address, which is critical, and it is responsible for transferring the data to the backend server for execution.

The third parameter is a callback function, and if the deletion succeeds, the action is performed. At this point, you can perform some actions, such as removing this deleted record. The combination is the following line of code. For no record, give a dynamic ID, which is used as the basis for deletion. Note here that the callback function will not be executed until all that must be done in the background is successful.

<tr id= "tr_<?php echo $item [' order_id '];?>" ></tr>

Below is the code that executes in the background

Copy Code code as follows:

Function del () {
$order _id = $this->uri->segment (4);
if ($order _id > 0) {
$this->db->delete (' billing ', Array (' order_id ' => $order _id));
$this->db->delete (' shipping_address ', Array (' order_id ' => $order _id));
$this->db->delete (' order_products ', Array (' order_id ' => $order _id));
$this->db->delete (' comments ', array (' order_id ' => $order _id));
}
$this->db->delete ($this->tbname, array (' ID ' => $order _id));
}

One of the methods in this background controller, by $this->uri->segment (), gets the parameter and assigns the parameter to the variable order_id.

You can then perform the appropriate delete operation in the background. If the deletion succeeds, there will be a default message passed to the success method.

The reason for the unsuccessful execution of the success method encountered today is that a delete operation was unsuccessful and that the hidden information was not passed to the success method. Why is it not successful? Because $this->db->delete (' shipping_address ', Array (' order_id ' => $order _id)) does not correspond to the name of the table in the database, it is probably modified by someone else.

After the correction, the success method was successfully executed.


This is a simple example of Ajax. Can simply illustrate the role of Ajax. Do not need to refresh the interface, directly secretly to the background to operate, the successful operation, you can also perform the corresponding action, through jquery to complete

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.