0

I have the following code for a poll:

<div id="pollE2">
How's the weather today?<br />
<form>
<input type="radio" name="voteE2" value="a" onclick="VoteNow(this.value)" />  Good
<input type="radio" name="voteE2" value="b" onclick="VoteNow(this.value)" />  Bad
</form>
</div>

Where the "VoteNow" function looks like this (it send the results to the "resultsE2.php" file...):

function VoteNow(int) {
if (window.XMLHttpRequest) {
xmlhttp=new XMLHttpRequest();
} else  {
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function() {
  if (xmlhttp.readyState==4 && xmlhttp.status==200) {
    document.getElementById("pollE2").innerHTML=xmlhttp.responseText; }
  }
xmlhttp.open("GET","resultsE2.php?voteE2="+int,true);
xmlhttp.send(); }

When the user chooses one option from the pull, the results show directly, but I want the user to be able to click on a SUBMIT button before doing it.

I tried to use document.getElementById(formID).submit() but I'm not sure how to incorporate it in the code. Any suggest ions?

Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
ragnezza
  • 9
  • 1
  • 3

1 Answers1

0

Change your HTML

<form id="vote-form">

Change your code

function VoteNow(vote) {
  //Added line    
  var xmlhttp;

  var vote = getRadioButtonValue();

  if (window.XMLHttpRequest) {
     xmlhttp=new XMLHttpRequest();
   } else  {
     xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
   }

  xmlhttp.onreadystatechange=function() {
    if (xmlhttp.readyState==4 && xmlhttp.status==200) {
      document.getElementById("pollE2").innerHTML=xmlhttp.responseText; }
    }
  xmlhttp.open("GET","resultsE2.php?voteE2="+vote, true);
  xmlhttp.send();
}


var form = document.getElementById("vote-form");
form.onsubmit = function {
  VoteNow();
  // Prevent the form from submitting
  return false;
};


function getRadioButtonValue() {
  //http://stackoverflow.com/questions/9618504/get-radio-button-value-with-javascript
}
Jeremy J Starcher
  • 23,369
  • 6
  • 54
  • 74
  • Thanks....but I'm kinda confused in the last part (the getRadioButtonValue function), I looked at the link but it's not very clear to me, could you please be a bit more specific? – ragnezza Jun 04 '14 at 20:08
  • Nope -- it is tricky to get the value of a set of radio buttons in JavaScript -- that's why I included the link to the best discussion at StackOverflow I could find. Anything else I said would just be repeating what is there. – Jeremy J Starcher Jun 04 '14 at 20:47