我如何创建成功返回功能? [英] how can i create a success back function?

查看:99
本文介绍了我如何创建成功返回功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

$(function() {
      $(".follow").click(function(){
        var element = $(this);
        var I = element.attr("id");
        var info = 'id=' + I;

        $.ajax({
            type: "POST",
            url: "listen.php",
            data: info,
            success: function(){}
            });

        $("#follow"+I).hide();  ///showing the remove button after the data has been entered
        $("#remove"+I).show();
        return false;

      });
});

PHP文件listen.php

The PHP file listen.php

<?php session_start();
include_once ('includes/connect.php');

$id = $_POST['id'];
$follower = $_SESSION['user_id'];

 $registerlistener = mysql_query("INSERT INTO relationships (leader, listener) VALUES('".$id."', '".$follower."')");
?>

我要做的是,当我单击跟随"按钮时,我想检查数据是否已经输入到数据库中,然后显示删除"按钮,基本上是在后台进行检查.

what I want to do is when I click the follow button, I want to check if the data has been entered into the database, before showing the remove button, basically checking on the background.

推荐答案

mysql_query将返回TRUE或FALSE.您可以从PHP脚本中回显该内容,并让ajax调用读取它.

mysql_query will return TRUE or FALSE. You can echo that from the PHP script, and have the ajax call read it.

listen.php:

listen.php:

<?php session_start();
include_once ('includes/connect.php');

$id = $_POST['id'];
$follower = $_SESSION['user_id'];

 $registerlistener = mysql_query("INSERT INTO relationships (leader, listener) VALUES('".$id."', '".$follower."')");

echo json_encode(array('response'=>$registerlistener));
?>

在您的JavaScript中:

In your JavaScript:

$.ajax({
type: "POST",
url: "listen.php",
data: info,
dataType: 'json',
success: function(data){
    if(data.response){
        // mysql_query returned TRUE
        $("#follow"+I).hide();
        $("#remove"+I).show();
    }
    else{
        // FALSE
    }
}
});

如果需要,可以使用$.post速记:

If you want, you can use the $.post shorthand:

$.post('listen.php', info, function(data){
    if(data.response){
        // mysql_query returned TRUE
        $("#follow"+I).hide();
        $("#remove"+I).show();
    }
    else{
        // FALSE
    }
}, 'json');

这篇关于我如何创建成功返回功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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