0

I have a Ajax function that connects to database to fetch "ROWS" of info. This is achieved using while..Fine..

I want the value of the while loop to be assigned to a radio button.

When I click each radio button, I want it to display the value that was assigned to it. i.e.

while($row = mysqli_fetch_array($res, MYSQLI_ASSOC))
{
   $mn = $row['mn'];
   echo $mn . "<td onclick = \"vote()\">
   <input type = \"radio\" id = \"voteCan\" value = \"$mn\">
   </td> 
   </tr>";
}
echo "</table>";

The above query displays multiple results.

I have a JavaScript function to be fired when user clicks any of the radio button, but the JavaScript function is getting only the value of the first row. It's not getting the other rows.

Please any help will be much appreciated.

2
  • Try putting the onClick on the input instead of the td. Also, show your vote() function. Commented Aug 14, 2017 at 16:19
  • Ok, I did that not working still. Below is my vote(). <script> function vote(){ voteFor = document.getElementById('voteCan').value; alert(voteFor); } </script> Commented Aug 14, 2017 at 16:26

1 Answer 1

1

Try this

while($row = mysqli_fetch_array($res, MYSQLI_ASSOC))
{
   $mn = $row['mn'];
   echo $mn . "<td onclick = \"vote(this)\">
   <input type = \"radio\" id = \"voteCan\" value = \"$mn\">
   </td> 
   </tr>";
  }
 echo "</table>";


<script>
function vote(e){
 var val = $(this).val();
 alert(val);
}
</script>
Sign up to request clarification or add additional context in comments.

2 Comments

I got undefined as response.
var val = $(this).val(); alert (val);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.