Jump to content

Recommended Posts

All,

I have the following code:

<form action="" name="myform" method="post">
<input type="hidden" id="picimages" name="picimages" value="<?php echo $resultsetstory['bigger_id']; ?>" />
  <p>
    <label>
      <input type="radio" id="picimages" name="picimages" value="<?php echo $resultsetstory['picture_id']; ?>" />
      <?php
  if($resultsetstory['title']<>""){
echo $resultsetstory['title'];
}else{
echo "Image 1";
}
?>
  </label>
    <br />
    <label>
      <input type="radio" id="picimages" name="picimages" value="<?php echo $resultsetpic2['picture_id']; ?>" />
      <?php
  if($resultsetpic2['title']<>""){
echo $resultsetpic2['title'];
}else{
echo "Image 2";
}
?>
  </label>
    <br />
<input name="submit" type="button" value="Vote!" onclick="javascript:voteupdate(this.myform);"/>
  </p>
</form>

When I submit this, it goes to this function:

function voteupdate(obj) {
var poststr = "picimage=" + document.getElementById("picimage").value +
"&bigger_id=" + encodeURI( document.getElementById("bigger_id").value );
alert(poststr);
makePOSTRequest('voteresults.php', poststr);
   }

However I can't get the value of the radio group for the one that is selected. Can someone see what is wrong?

 

Thanks.

Link to comment
https://forums.phpfreaks.com/topic/181766-getting-a-radio-button-value/
Share on other sites

1. Why are you passing the form to the function when you don't use that information?

2. You use the same id - picimages - twice.  This is wrong.  An id is supposed to be unique, meaning only one id per element per page.

3. You have to check to see if a button has been clicked.

4. An unobtrusive style would serve you well:

 

<form action="" name="myform" method="post">
<input type="hidden" name="picimages" value="<?php echo $resultsetstory['bigger_id']; ?>" />
  <p>
    <label>
      <input type="radio" name="picimages" value="<?php echo $resultsetstory['picture_id']; ?>" />
      <?php
     if($resultsetstory['title']<>""){
echo $resultsetstory['title'];
}else{
echo "Image 1";
}
?>
     </label>
    <br />
    <label>
      <input type="radio" id="picimages" name="picimages" value="<?php echo $resultsetpic2['picture_id']; ?>" />
      <?php
     if($resultsetpic2['title']<>""){
echo $resultsetpic2['title'];
}else{
echo "Image 2";
}
?>
     </label>
    <br />
   <input name="submit" id="submit" type="button" value="Vote!" />
  </p>
</form>

<!-- after all your HTML is output -->

</body>

<script type="text/javascript">
   var button = document.getElementById('submit');

   button.onclick = function() {
      var radios = document.forms["myform"].elements["picimages"];
      var radiosLength = radios.length;
      var chosen = '';

      for(var i = 0; i < radiosLength; ++i) {
         if(radios[i].checked == true) {
            chosen = radios[i].value;
         }
      }

      if(chosen){
         var postStr = "picimage=" + chosen + "&bigger_id=" + encodeURI(document.getElementById('bigger_id').value);

         alert(postStr);

         makePOSTrequest('voteresults.php', postStr);
      }
      else {
         //error
      }
   }
</script>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.