Eduardop911
Eduardop911

Reputation: 51

$.post data naming issue

Hello i did a jquery ajax post that calls for a php form depending on the key : the key is get by the data-apphere is the code:

$(document).ready(function(){
    $('.form1').keyup(function(){
        var value = $(this).val();
        var key = $(this).data('app');
        $.post('page2.php', { key : value }, function(data){
            $("div[ data-show='" + key + "' ]").text(data); 
        });
    });
});

the problem is that where i specify the data value key instead of giving me the value data i want it names it as key, is there a way i can make the variable work? so that the key is equal to the data value, and not equal to key, because in the php it works if i do this $_POST['key']; but lets say data-app="username" then key will be equal to username and the php will read like this: $_POST['username']; hope is understandable and thanks :)

Upvotes: 0

Views: 40

Answers (1)

pktangyue
pktangyue

Reputation: 8524

Try:

$(document).ready(function(){
    $('.form1').keyup(function(){
        var value = $(this).val();
        var key = $(this).data('app');
        var obj = {};
        obj[key] = value;
        $.post('page2.php', obj, function(data){
            $("div[ data-show='" + key + "' ]").text(data); 
        });
    });
});

Upvotes: 3

Related Questions