jQuery Ajax发布-如何获取数据? [英] jquery ajax post - how to get data back?

查看:75
本文介绍了jQuery Ajax发布-如何获取数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个个人资料页面,其中包含一系列图像.我想使用jQuery允许用户从服务器删除图像并更新页面而无需重新加载整个页面.成功后,它将从页面中删除包含图像的div.我的删除功能是PHP;很简单:

I have a profile page that contains a series of images. I want to use jQuery to allow the user to delete an image from the server and have the page update without reloading the entire page. When it's successful, it will remove the image's containing div from the page. My delete function is PHP; fairly simple:

delete.php

<?php 
if (isset($_POST['id'])) {     
    if (unlink($_POST['id'])) { 
        echo "success";
    } 
    else { 
        echo "failure"; 
    } 
} 
?>

(已经进行了用户身份验证,只是为了使他们进入调用delete.php的页面.)

(There's already user authentication in place just to get them to the page that calls delete.php.)

这是一个显示的图像的html-最多可以有5个这样的块:

Here's the html of one displayed image - there can be up to 5 of these chunks one after another:

<div class="box">
  <img src="uploads/t_10DOT_22C_1111_1300370702_3.jpg" /> 
  <h5><a rel="external" href="uploads/10DOT_22C_1111_1300370702_3.jpg">See full version</a></h5> 
  <a href="#" id="10DOT_22C_1111_1300370702_3.jpg" class="delete" onclick="return ConfirmDelete();" >x</a>
  <div class="clear"></div> 
</div>

到目前为止,我的jQuery如下所示:

My jQuery so far looks like this:

$(document).ready(function() {
$('#load').hide();
});

$(function() {
$(".delete").click(function() {
$('#load').fadeIn();
var commentContainer = $(this).parent();
var id = $(this).attr("id");
var string = 'id='+ id ;

$.ajax({
   type: "POST",
   url: "delete.php",
   data: string,
   cache: false,
   success: function(data){
    commentContainer.slideUp('slow', function() {$(this).remove();});
    $('#load').fadeOut();
  }

 });

return false;
    });
});

我关心的部分是ajax帖子.成功部分实际上是如何工作的?我需要在我的php文件中做什么,以便ajax知道删除是成功还是失败?

The part I'm concerned with is the ajax post. How does the success part actually work? What do I need to do in my php file so that ajax knows whether the delete was a success or failure?

推荐答案

一旦ajax发布请求完成了将请求发送到的文件的执行,如果没有错误,则在成功"部分中添加的代码为在这种情况下被执行

Once an ajax post request has finished executing the file you sent the request to, if there was no error, the code you add in the "success" section is executed, in this case

success: function(data){
  /*The code you need*/
});

如果在前面的代码中执行代码,则"data"变量包含您从php文件返回的任何内容,它可以是数据,可以是简单的"true"或"false",您可以选择发送什么让您的jQuery知道它是否成功.

The previous part if where the code is executed, the "data" variable contains anything you return from your php file, it can be data, it can be a simple "true" or "false", you choose what to send to let your jQuery know if it was successful.

希望这会有所帮助.

编辑说明:

function(applyData){
  if ( applyData.toString() == 'invalid' ){
    $('#pollError').html('Global styles cannot be modified.');
    $('#pollNotice').html('');
  }
  else{
    $('#pollNotice').html('The changes to the style have been applied.');
  }
});

前面的示例是一个实时示例,说明您可以在成功"事件中的函数内部执行的操作.在那里,我处理无效"状态,否则成功,然后在无效的情况下刷新几个DIV,在成功的情况下更新单个DIV.

The previous example is a live example of what you can do inside the function in the "success" event. There I handle an "invalid" status and otherwise it's successful, after that I refresh a couple DIVs in case of invalid or update a single DIV in case of success.

这是执行的php:

if ( !$db->isGlobal($id_css)){
  $data['id_poll'] = $id_poll;
  $data['id_css'] = $id_css;
  $data['css'] = $css;
  $db->applyCssChanges($data);
}
else{
  echo 'invalid';
}

这篇关于jQuery Ajax发布-如何获取数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆