Clifton Miranda Clifton Miranda - 2 months ago 5
Ajax Question

I want to pass the id using ajax call.. i have tried the following code

I want to pass the id of the button using ajax call ... and display the result into id called demo2 ...

<script>
function loadDoc2() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (xhttp.readyState == 4 && xhttp.status == 200) {
document.getElementById("demo2").innerHTML = xhttp.responseText;
}
}
xhttp.open("GET", "get_coupon_code.php?offline_offer_id=" +this.id, true);
xhttp.send();
}
</script>

<div id="demo2"></div>

<button id="111" onclick="loadDoc2()">Button 1</button>
<button id="222" onclick="loadDoc2()">Button 2</button>
<button id="333" onclick="loadDoc2()">Button 3</button>

Answer

You can do this in two ways No 1.

<script>

 function loadDoc2(id) { //here is your id
  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (xhttp.readyState == 4 && xhttp.status == 200) {
      document.getElementById("demo2").innerHTML = xhttp.responseText;
    }
  }
  xhttp.open("GET", "get_coupon_code.php?offline_offer_id=" +this.id, true);
  xhttp.send();
}
</script>

<div id="demo2"></div>

<button id="111" onclick="loadDoc2("111")">Button 1</button>
<button id="222" onclick="loadDoc2("222")">Button 2</button>
<button id="333" onclick="loadDoc2("333")">Button 3</button>

No 2.

<script>
function loadDoc2() {
var Id = $(this).attr('id');  //here is your id
  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (xhttp.readyState == 4 && xhttp.status == 200) {
      document.getElementById("demo2").innerHTML = xhttp.responseText;
    }
  }
  xhttp.open("GET", "get_coupon_code.php?offline_offer_id=" +this.id, true);
  xhttp.send();
}
</script>

<div id="demo2"></div>

<button id="111" onclick="loadDoc2()">Button 1</button>
<button id="222" onclick="loadDoc2()">Button 2</button>
<button id="333" onclick="loadDoc2()">Button 3</button>
Comments