You must set the default value for a group of stations if it is not verified

In my html form I have a group of radio buttons

<form action="receive.php" method="post"> <input type="radio" name="rad" value="one" /> One <br /> <input type="radio" name="rad" value="two" /> Two <br /> <input type="radio" name="rad" value="three" /> Three <br /> <input type="submit" value="submit" /> </form> 

I have a script in which there is no switch. In this case, my server php gets the input value as false . I want to change false to null or undefined . Is it possible? Any help is appreciated.

+4
source share
4 answers

You can do something like this:

 <form action="receive.php" method="post"> <input type="radio" name="rad" value="one" /> One <br /> <input type="radio" name="rad" value="two" /> Two <br /> <input type="radio" name="rad" value="three" /> Three <br /> <input type="hidden" name="rad" value="null" /> <input type="submit" value="submit" /> </form> 

Now, if you have not checked any switch, you will get the value "null", but remember that "null" not the same as NULL in PHP.

+5
source

Radio devices, by definition, do not have a null value.

However, you can add a switch, for example:

 <form action="receive.php" method="post"> <input type="radio" name="rad" value="one" /> One <br /> <input type="radio" name="rad" value="two" /> Two <br /> <input type="radio" name="rad" value="three" /> Three <br /> <input type="radio" name="rad" value="null" /> null <br /> <input type="submit" value="submit" /> </form> 

And use it as the zero you need.

+1
source

Use javascript to return null if the radio button is not selected , click on "send" :

 var radioChecked=0; var radios=document.getElementsByName("rad"); for(var i=0;i<radios.length;i++) { if(radios[i].checked){ radioChecked=radioChecked+1; } } if(radioChecked==0) return null; 

Hope it solves your problem.

0
source

This can be done using the if / else statement inside .php:

 $radioInput = false; if (!isset($_POST['rad'])){ $radioInput = NULL; } else { $radioInput = $_POST['rad']; } 

http://php.net/manual/en/function.isset.php

0
source

Source: https://habr.com/ru/post/1442714/


All Articles