Skip to content Skip to sidebar Skip to footer

Ajax POST With Function

I was using this code for get function and It works perfectly. Im getting data with this function. function getdata(getdatafrom, resultclass){ $.get(getdatafrom, function(d

Solution 1:

If you would do this using inline-event you should add single quotes :

onclick="postdata('post.php', '#input1-#input2-#input3')"

Then your js should be :

function postdata(postdatafrom, inputnamesvaluelist){
    var inputs_ids = inputnamesvaluelist.split('-');//split string passed into array of ids
    var parameters={};

    //construct obejct of {name: value,..} that you could pass it in post request
    $.each(inputs_ids, function(index, input_id){
        var input_name  = $(input_id).attr("name");
        var input_value = $(input_id).val();

        parameters[input_name]=input_value;
    })

    //post parameters to given "postdatafrom"
    $.post(postdatafrom, parameters, function(data) {
        //Your code here
    });
}

Hope this helps.


Post a Comment for "Ajax POST With Function"