2

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 ?

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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 );
    });
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.