Search code examples
javascriptphpjqueryx-editable

$_POST won't fetch attribute set by jquery


I have a HTML link element:

    <a href='#' class='editUsrProfile' data-type='text' data-pk='' data-url='file.php'></a>

I set the data-pk attribute with Jquery:

    $('.editUsrProfile').attr('data-pk', usr);

I check with console.log if data-pkhas been set, and it's Ok.

But in my php script, where i check for $_POST['pk'] i get nothing.
Other posts like name etc, that are static works just fine.

So, why can't $_POSTread the attribute set by Jquery, and how can i solve this?

UPDATE
I use "Bootstrap Editable" to send data to php file.
The problem was i enabled editable before i had set data-pk.
Simply i changed the events to first set the data-pkwith Jquery.
And then i run the editableplugin:

$('.editUsrProfile').attr('data-pk', usr);
$('.editUsrProfile').editable({});

Solution

  • As your data-pk is set on page load the you are initializing editable on page load too. But you are changing data-pk dynamically so you need to update your params of editable like,

    $('.editUsrProfile').editable({
        params: function(params) {
            params.pk = usr;// or you can try $(this).attr('data-pk') or $('.editUsrProfile').attr('data-pk') whichever works for you
            return params;
        }
    )