Search code examples
javascriptxmlhttprequestradio-button

XMLHttpRequest + submit the Value of the clicked Radiobutton


I have a question aboud a XMLHttpRequest with Radio Buttons. I need to submit the value for saving it in the DB. I have made a onclick Event to trigger the Value selected.

    function ajax_radiobtn(event){
    event = event || window.event;
    var target = event.target || event.srcElement;
    var rdbid = target.id;
    var rdbval = target.value;

    var xhr = new XMLHttpRequest();
    var url = 'URL';

    //Send
    xhr.open("POST", url, true);
    xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xhr.onreadystatechange = function(){
    if(xhr.readyState == 4 && xhr.status == 200){
    var return_data = xhr.responseText;
    document.getElementById("Register").innerHTML = return_data;
    }
    }
    xhr.send(ausgabe);
    document.getElementById("Register").innerHTML = "Reload..";
    }

This Request works for all Textfileds an Texareas but not for Radiobuttons. I can trigger the value, but the submitting do not work. Is there someone who can tell me how it works?

var url = "URL";
var params = "somevariable=somevalue&anothervariable=anothervalue";
var http = new XMLHttpRequest();
http.open("GET", url+"?"+params, true);

I trye this, maybe this works?


Solution

  • The discussion in the comments revealed that there was a syntax error leading to

    ReferenceError: ajax_radiobtn is not defined 
    

    fixing the missing brackets solved the problem for Mr.Robot

    Also the data in send() had to be

    var ausgabe =rdbid + '=' + rdbval;