1

从早上起我就一直在看这个,我无法弄清楚问题出在哪里。

这是javascript/ajax

$('#subscribe').live('click', function(){
        rel = $(this).attr("rel");
        datas = "topic_id="+rel;
        $.ajax({
            type: 'POST',
            url: 'subscribe.php',
            data: datas,
            success: function(result){
                alert("k");
                $(this).val(result);
            }
        });
    });

这是处理ajax的php被发布到“ subscribe.php

<?php
session_start();
require_once(functions/functions.php);

if(isset($_POST["topic_id"])){
        $uid = user_id($_SESSION['username']);
        $tid = $_POST["topic_id"];
        $qry = "SELECT user_id FROM subscribe WHERE topic_id = $tid";
        $rst = mysql_query($qry);
        if(!$rst){
            $query = "INSERT INTO subscribe (user_id, topic_id) VALUES ($uid, $tid)";
            $qry = mysql_query($query);
            echo "Subscribed";
        }else{
            echo "hmmm";
        }
}?>

这是按钮

<input type="button" value="Subscribe" name="buton" id="subscribe" rel="'.$output["id"].'" />

警报似乎有效,但更改未生效,并且数据库中没有更改

4

6 回答 6

2

您应该使用对象请求数据:

datas = {topic_id:rel};

您需要在 ajax 回调成功中保留对“this”的引用:

$('#subscribe').live('click', function(){
        var $self = $(this);
        rel = $self.attr("rel");
        datas = "topic_id="+rel;
        $.ajax({
            type: 'POST',
            url: 'subscribe.php',
            data: datas,
            success: function(result){
                alert("k");
                $self.val(result);
            }
        });
    });

你也可以使用闭包:

$('#subscribe').live('click', function () {
    rel = $(this).attr("rel");
    datas = "topic_id=" + rel;
    (function ($self) {
        $.ajax({
            type: 'POST',
            url: 'subscribe.php',
            data: datas,
            success: function (result) {
                alert("k");
                $self.val(result);
            }
        });
    })(this);
});

或查看$.proxy()方法。

顺便说一句,这里的 'rel' 是一个全局变量,不确定它是你真正期望的。要使其成为本地,请使用 'var' 作为其声明。并且不推荐使用“live”,您应该改用委托.on()

于 2013-06-04T08:43:05.693 回答
0

代替 live 函数你 on() 如下所示。它工作正常。

$('#subscribe').on('click', function(){
    rel = $(this).attr("rel");
    datas = "topic_id="+rel;
    $.ajax({
        type: 'POST',
        url: 'subscribe.php',
        data: datas,
        success: function(result){
            alert("k");
            $(this).val(result);
        }
    });
});
于 2013-06-04T09:20:24.620 回答
0

检查您的文件位置。subscribe.php 在哪里?以及 javascript 文件在哪里。尝试使用 firebug Firefox 插件并转到控制台并检查 ajax 请求详细信息。是否能找到subscribe.php

否则,在 ajax 请求中的 'url' 参数中给出相对路径

其他原因可能是 require_once("functions.php"); 失败 所以试着写 die("hii"); 在 subscribe.php 的那一行之上

于 2013-06-04T08:58:31.817 回答
0

$.ajax({ type: "POST", url: "JQueryServlets", data: { message : mge}, success : function(data){ alert('success'+data); } });

试试这个代码

并在 servlet

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    System.out.println(" inside do post "+request.getParameter("message"));
}
于 2014-11-12T17:44:20.637 回答
0

尝试这个

$('#subscribe').live('click', function(){
        var t = $(this);
        var rel = t.attr("rel");
        datas = {'topic_id' : rel};
        $.ajax({
            type: 'POST',
            url: 'subscribe.php',
            data: datas,
            dataType : 'json',
            success: function(result){
               t.val(result);
            }
        });
    });
于 2013-06-04T09:26:49.120 回答
-1

您发布 if(!$rst){ ...如果选择查询成功,那不会使插入查询不运行吗?问候

于 2013-06-04T08:41:20.343 回答