0

I've edited this question from the original OP to better represent my issue.

How can I pass the variable data-uid with AJAX ?

Right now the variable doesnt get passed.

var uid = $(this).data("uid"); doesn't work = undefined

var uid = '199'; gets passed. works.

is it possible to have something like : var uid = $uid; ?

HTML

 <form>
  <fieldset>
  <textarea id="post_form" type="text" data-uid="<?php echo $uid ?>"/></textarea>
  <button type="submit" id="add" value="Update" name="submit" />OK</button>
  </fieldset>
 </form>

JS

$(function() {
$("#add").click(function() {
    var boxval = $("#post_form").val();
    var uid = $(this).data("uid");  // this needs to be changed
    var dataString = 'post_form=' + boxval + '&uid=' + uid;
    if (boxval == '') {
        return false;
    } else {
        $.ajax({
            type: "POST",

        $.ajax({
            type: "POST",
            url: "add.php",
            data: dataString,
            cache: false,

            success: function(html) {
            parent.html(html);
            }
        });
        return false;
    });
});
12
  • 3
    can you show the related html ? Commented Oct 24, 2013 at 21:18
  • what does the .like element look like? Commented Oct 24, 2013 at 21:19
  • 3
    you're perform POST instead of GET, use get and url: "delete.php?"+dataString Commented Oct 24, 2013 at 21:20
  • Post some of your HTML code and make sure there aren't any other elements with the same class name that could be interfering. Commented Oct 24, 2013 at 21:20
  • 1
    @ChHr use var uid = $('post_form').attr('data-uid'); Commented Oct 24, 2013 at 23:53

2 Answers 2

1

problem in your code in:

var uid = $(this).data("uid");

you're trying to wrap this into jquery object, but it is button object in this context, then you're trying to obtain something unassigned from it

you shoud use:

var uid = $('#post_form').attr('data-uid');

or, you can add <input type="hidden" name=... value=... and get id from it, this is more general way

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

Comments

1

Looks like your issue is with the data attribute that should be data-uid="somevalue" <a href="#" class="like" id="1234" data-uid="7687687">Like</a>.

Check this fiddle to see if this solves your main problem

2 Comments

+1 Thanks for your reply. I've edited my question. When the page is refreshed data-id="<?php echo $uid ?> echoes the right uid. The issue is during the AJAX callback: when a new item is added, data-id is undefined.
What does html parameter return? A new element?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.