Home > OS >  Radio button with defined value always returns "on"
Radio button with defined value always returns "on"

Time:02-17

I have a very odd problem... I'm submitting a form with radio buttons (see below) and as you can see, all the buttons have values. And yet, when I try to get the value of the selected button, whichever I have selected, it always returns "on".

I know "on" is the default value for radio buttons, but I did specify a value... so why??

Any insight would be very much appreciated. Thanks!

index.php file:

<form   action="newuser.php?espace=1" method="POST" name="activity" autocomplete="off">
<input type="radio" id="radio1" name="usertype" value="1"  required><label  for="radio1">Étudiant</label>
<input type="radio" id="radio2" name="usertype" value="2"  required><label  for="radio2">Personnel de l'université</label>
<input type="radio" id="radio5" name="usertype" value="5"  required><label  for="radio5">Professionnel</label>
<input type="radio" id="radio9" name="usertype" value="9"  required><label  for="radio9">Particulier</label>

newuser.php file:

if (isset($_REQUEST['prenom'], $_REQUEST['nom'], $_REQUEST['mail'], $_REQUEST['usertype'])) {
  echo $_REQUEST['usertype'];
}

CodePudding user response:

Your issue might be with $_REQUEST. Try switching to $_POST (in your specific case)

CodePudding user response:

Below you can see how the search string is generated from the form before it is sent to the php script for processing. Admittedly, the search string only makes sense for a GET request. Nonetheless it illustrates, that clearly no "ON" value is sent but instead something like usertype=5:

// submit handler de-activated again:
if (false) document.querySelector("form").onsubmit=function(ev){
 ev.preventDefault();
 console.log((new URLSearchParams(new FormData(this))).toString())
}
<form   action="https://jsonplaceholder.typicode.com/users" method="POST" name="activity" autocomplete="off">
<input type="radio" id="radio1" name="usertype" value="1"  required><label  for="radio1">Étudiant</label>
<input type="radio" id="radio2" name="usertype" value="2"  required><label  for="radio2">Personnel de l'université</label>
<input type="radio" id="radio5" name="usertype" value="5"  required><label  for="radio5">Professionnel</label>
<input type="radio" id="radio9" name="usertype" value="9"  required><label  for="radio9">Particulier</label>
<button>go</button>
</form>

  • Related