ToniD February 2016

How to send jquery post request using button

I am trying to send post request using jquery. This is the request i am trying to send

$.post("https://api.steampowered.com/IEconService/DeclineTradeOffer/v1/", { 
    key: "1234567891011121314151617181920", 
    tradeofferid: "$offerid" 
}).done(function(data) {
    alert("Data Loaded: " + data);
});

Inside my php i have $offerid , and key is always the same .

I want to send it on button click so i created button inside my php

$offerid = $value2['tradeofferid'];

echo '<button id="cancel-offer"> Cancel offer </button>';       

how i can connect button with jquery request and send $offerid to the request ?

Answers


Rory McCrossan February 2016

You just need to select the button by its id attribute, then add a click handler:

$('#cancel-offer').click(function() {
    $.post("https://api.steampowered.com/IEconService/DeclineTradeOffer/v1/", { 
        key: "1234567891011121314151617181920", 
        tradeofferid: "<?= $offerid ?>" 
    }).done(function(data) {   
        alert("Data Loaded: " + data);
    });
});

I would suggest you have a quick read through the jQuery documentation. It makes a great reference, and also gives you an idea of what jQuery is and is not capable of.


madalin ivascu February 2016

Modify your code like this:

php:

echo '<button id="cancel-offer" data-id="'.$offerid.'"> Cancel offer </button>';

js:

$('#cancel-offer').click(function(){
$.post( "https://api.steampowered.com/IEconService/DeclineTradeOffer/v1/", { key: "1234567891011121314151617181920", tradeofferid: $(this).attr('data-id') },function( data ) {
alert( "Data Loaded: " + data );
    });
});

Post Status

Asked in February 2016
Viewed 1,004 times
Voted 6
Answered 2 times

Search




Leave an answer