javascript get input variable with a URL to executable Post to a URL

Viewed 149

I am trying to make diffent buttons post to diffent urls. I am not sure how to get the variable from onlcick to var url in the script and executable Post to that URL was set from the HTML input. This script works with one button with the url infromationn already set. I am not sure how do this. I don't know much about javascript and I copy 'n' paste it and try to change it. onlickonlick

I could make a nornal link. Only problem that is go that page. I only need to trigger a page and stay on the current page. I hope someone can help me to point the right way or give a exmple code. I am very new to this. Thank you so much,

    function sendWebhook(onlick) {

    var http = new XMLHttpRequest();
    var url = var.onlick;
    var content = {"value1" : "test data"};
    http.open('POST', url, true);

    //Send the proper header information along with the request
    http.setRequestHeader('Content-type', 'application/json');

    http.onreadystatechange = function() {//Call a function when the state changes.
        if(http.readyState == 4 && http.status == 200) {
            alert(http.responseText);
        }
    }
    http.send(JSON.stringify(content));

}


<input id="contact-submit" src="img/red.png" type="button"  value="Living Room Light On" onclick="sendWebhook(https://link1.com')" />

<input id="contact-submit" src="img/red.png" type="button"  value="Living Room Light Off" onclick="sendWebhook('https://link2.com')" />

<input id="contact-submit" src="img/red.png" type="button"  value="Living Room Light Off" onclick="sendWebhook('https://link3.com')" />

1 Answers

The line var url = var.onlick; is not valid javascript. I changed the function parameter's name to "url" and deleted the offending line.

function sendWebhook(url) {
  var http = new XMLHttpRequest();
  var content = {"value1" : "test data"};
  http.open('POST', url, true);

  //Send the proper header information along with the request
  http.setRequestHeader('Content-type', 'application/json');

  http.onreadystatechange = function() { //Call a function when the state changes.
      if(http.readyState == 4 && http.status == 200) {
          alert(http.responseText);
      }
  }
  http.send(JSON.stringify(content));
}
<input id="contact-submit" src="img/red.png" type="button"  value="Living Room Light On"
name="https://link3.com"
onclick="sendWebhook('https://link1.com')" />

<input id="contact-submit" src="img/red.png" type="button"  value="Living Room Light Off"
onclick="sendWebhook('https://link2.com')" />

<input id="contact-submit" src="img/red.png" type="button"  value="Living Room Light Off"
onclick="sendWebhook('https://link3.com')" />

If you want to assign a function parameter's value to a new variable, check out this sample code:

function aFunctionThatAssignsAParameterToAVariable(aParamter) {
  var myNewVariable = aParamter;
  console.log(myNewVariable);
}
aFunctionThatAssignsAParameterToAVariable("banana");

Related