How can I pass parameters in data attributes with ajax?

208 Views Asked by At
function update(id, flag) {
    var flag_count = jQuery(flag + id).html();
    flag_count++;
    jQuery(flag + id).html(flag_count);
    jQuery.ajax({
        url: 'update_count.php',
        type: 'post',
        data: {
            type: flag,
            id: id
        },
        success: function(result) {

        }
    })

}

How to pass parameters in data but flag and id are arguments of the function.

How to write this?

data:'type='+flag+'&id='+id

Pls help...

1

There are 1 best solutions below

0
Talha Safdar On

Below there is a JavaScript approach. You normally add this function inside your function, the same way as you do with jQuery.

xmlhttp.onreadystatechange = function ()
    {
        if (this.readyState === 4 && this.status === 200)
        {
            if (confirm == 9 || confirm == 10) 
            {
                // do something prior to send data to ajax file
            }
        }
    };
    xmlhttp.open('POST', 'update_count.php?type=' + flag + '&id=' + id, true);
    xmlhttp.send();

The above code will pass the values stored inside each variable (flag, id) to the ajax file (update_count.php).