1

I have been looking at this since morning and I can't figure out where the problem is from..

this is the 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);
            }
        });
    });

and this is the php handling the ajax is been posted to "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";
        }
}?>

and this is the button

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

alert seems to work but the changes don't take effect and no changes in the database

9
  • 1
    Please fix your sql injection error :) Commented Jun 4, 2013 at 8:38
  • 2
    live() is deprecated in jquery1.7 and removed then after .. Commented Jun 4, 2013 at 8:41
  • tell me where it is @AlanFoster Commented Jun 4, 2013 at 8:41
  • @RajeevRanjan i have another code that is using same structure and it's working..maybe when i get this one to work i will switch to the accepted one..but in the mean time can u tell me where i made the mistake Commented Jun 4, 2013 at 8:43
  • can you see any response using firefox console or Chrome Net. this seems relatively simple. Commented Jun 4, 2013 at 8:44

6 Answers 6

2

You should use object for request data:

datas = {topic_id:rel};

You need to keep reference on 'this' inside ajax callback success:

$('#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);
            }
        });
    });

You could use a closure too:

$('#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);
});

Or see for $.proxy() method.

BTW, here 'rel' is a global variable, not sure it is what you really expect. To make it local, use 'var' for its declaration. And 'live' is depreacated, you should use delegation with .on() instead.

Sign up to request clarification or add additional context in comments.

4 Comments

i just changed and the text of the button changed but no to the specified result...its like the ajax is not posting to the php page...
Check in console network tab result returned by you php script (errors, etc...)
i just checked the console but there is no error related to the script
Fanx man i got it to work...you re really good..fanx for the help
0

Check your file locations. where is subscribe.php ? and where is javascript file. try to use firebug Firefox addon and go to console and check ajax request details. whether it is able to find subscribe.php

other wise give relative path in 'url' parameter in ajax request

Other reason may be failure of require_once("functions.php"); so try writing die("hii"); above that line in subscribe.php

2 Comments

i wrote die and the button text changed to it...so wat could be the problem....i just checked the require_once and its correct
just fixed it..it was a problem from the sql per say....fanx you actually put me on the right path
0

In place of live function you on() like below.Its works fine.

$('#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);
        }
    });
});

Comments

0

Try this

$('#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);
            }
        });
    });

Comments

0

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

try this code

and at servlet

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    System.out.println(" inside do post "+request.getParameter("message"));
}

Comments

-1

you post if(!$rst){ ... wouldn't that make the insert query NOT run if the select query is successful? regards

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.