Radio button on form field always submitting as ON/TRUE

radio button javascript
radio button properties
javascript get value of radio button group

I have a fairly basic form set up with an input field for a single radio button.

There's a php script that processes the form and inserts the values into a mysql database. For some reason, every time I fill the form out, regardless of whether I have the radio selected or not, the result is always ON/TRUE.

here is my input field:

<input type="radio" name="news" id="news" class="radio" value="yes" />

Here is part of the PHP code that is supposed to process that:

if(isset($_POST['news']))
{
$news = "Yes";
}
else
{
$news = "No";
}

Can someone tell me what hte problem is from that code or would I need to post more?

here is the full form code:

<form method="POST" id="ipad" name="ipad" action="list.php">
<input type="text" onfocus="if (this.value == 'First Name') {this.value = '';}" onblur="if (this.value == '') {this.value = 'First Name';}" value="First Name" id="fname" name="fname" class="inputtext">


<input type="text" onfocus="if (this.value == 'Last Name') {this.value = '';}" onblur="if (this.value == '') {this.value = 'Last Name';}" value="Last Name" id="lname" name="lname" class="inputtext">

<input type="text" onfocus="if (this.value == 'Email') {this.value = '';}" onblur="if (this.value == '') {this.value = 'Email';}" value="Email" id="email" name="email" class="inputtext">

<input type="text" onfocus="if (this.value == 'Code') {this.value = '';}" onblur="if (this.value == '') {this.value = 'Code';}" value="Code" id="code" name="code" class="inputtext">


<input type="radio" value="" class="radio" id="news" name="news">
<div class="radiotext">sign up?</div>

  </form>

Notice that the radio button input has no javascript being applied to it. I've tried filling in the value="" with YES or NO or leaving it blank.

You need to test for the radio button's value. The variable will always be present, so isset() is bound to always return true.

if(!empty($_POST['news']) and ($_POST['news'] == 'yes'))

How do we use radio buttons in HTML forms?, name Used to give a name to the control which is sent to the server to be recognized and get the value. Radio buttons. Radio buttons are similar to checkboxes, except that only one radio button in a group can be selected at any one time. (As with checkboxes, a radio button group is a collection of radio buttons with the same name attribute.) Radio buttons are useful for getting a user to pick from a choice of options.

if (isset($_POST['news']) && $_POST['news'] == 'yes'){
    $news = "Yes";
}
else
{
   $news = "No";
}

Show Hide DIV Based on Radio Buttons Selection Using jQuery, How do you show and hide input fields based on radio button selection? Data representation of a radio group. When the above form is submitted with a radio button selected, the form's data includes an entry in the form contact=value.For example, if the user clicks on the "Phone" radio button then submits the form, the form's data will include the line contact=phone.

about that jquery code, javascript doesn't need to be inside html to work. If I were you, I would search through my code for .attr("checked","checked") or .prop("checked");

Adding Radio Buttons, Checkboxes & Lists for User Input to HTML , How do I make sure only one radio button is selected? Radio buttons have exactly the same events as all the form fields. These events are used when you need the radio button selection to drive some other action on the form. Two of the most common uses are scripting the MouseUp event to set a value in another field(s) and to Enable/Disable or Hide/Show other fields.

You need to workaround it like so...

You need to add a hidden input to your form and when ever you click on one of the radio buttons, you call a function which will change the value of the hidden input by its id.. and here it is step by step :

  1. Add a hidden input to your form and give it a name and an id :

    <input type="hidden" id="IsFormer" name="IsFormerValue" value="">

  2. Add on click attribute for both radio buttons :

    <input type="radio" id="Former" name="IsFormer" onclick="ToggleIsFormer('Yes')">

    <input type="radio" id="NotFormer" name="IsFormer" onclick="ToggleIsFormer('No')">

  3. Add the function in Javascript :

    function ToggleIsFormer (Value) { document.getElementById('IsFormer').value = Value; }

  4. Finally, in PHP you check the value with the name of the hidden input and not the name of the radio buttons.

Here is the above steps on your code (try doing it your self first ;) ) :


<form method="POST" id="ipad" name="ipad" action="list.php">
    <input type="text" onfocus="if (this.value == 'First Name') {this.value = '';}" onblur="if (this.value == '') {this.value = 'First Name';}" value="First Name" id="fname" name="fname" class="inputtext">
    <input type="text" onfocus="if (this.value == 'Last Name') {this.value = '';}" onblur="if (this.value == '') {this.value = 'Last Name';}" value="Last Name" id="lname" name="lname" class="inputtext">
    <input type="text" onfocus="if (this.value == 'Email') {this.value = '';}" onblur="if (this.value == '') {this.value = 'Email';}" value="Email" id="email" name="email" class="inputtext">
    <input type="text" onfocus="if (this.value == 'Code') {this.value = '';}" onblur="if (this.value == '') {this.value = 'Code';}" value="Code" id="code" name="code" class="inputtext">

    <input type="radio" class="radio" id="news" name="news" onclick="ToggleWithNews()">
    <div class="radiotext">Sign up?</div>

    <input type="hidden" id="WithNews" name="WithNewsValue" value="No">
</form>

<script>
    function ToggleWithNews () {
        document.getElementById('WithNews').value = 'Yes';
    }
</script>

Done!!!


My note : If you have only on option and the viewer can toggle on or off that option, you should use 2 radio buttons on for Yes and one for No, because one radio button will be unable to un-check once it has been checked.

Or

If you want to only add on input for that option, you should be using a checkbox input, since this can be checked and unchecked at any time.


Hope my answer helped you :)

Radio button, by setting the 'name' attribute to the same value for all the choices. Radio buttons let a user select only one of a limited number of choices: The <input type="radio"> defines a radio button. Radio buttons are normally presented in radio groups (a collection of radio buttons describing a set of related options). Only one radio button in a group can be selected at the same time.

Exposing Additional Form Fields via Checked Radio Buttons, Exposing Additional Form Fields via Checked Radio Buttons is a way to use this same concept in a totally non-hacky way, on an actual form! I used this technique on the CodePen job posting form to only reveal additional form fields as needed. Fair warning, it's not always super semantic or a good idea, but it's awful fun  The HTML <form> element defines a form that is used to collect user input: An HTML form contains form elements. Form elements are different types of input elements, like: text fields, checkboxes, radio buttons, submit buttons, and more. The <input> element is the most important form element. The <input> element is displayed in several ways

Value=TRUE for Checkbox - HTML & CSS, <th class='colSelect' /> <input name='selectAll' type='checkbox' value='TRUE' /> <​/th> is checked, and IF it is submitted on a Form, THEN it will have a value of “​TRUE”, which for example, The VALUE attribute, required for radio buttons and checkboxes, The value attribute is always optional in the current HTML spec. The third line of code sets Validate event script for the Title field, which is the name given to all of the “Radio Check Boxes”. Setting Field Visibility based on Valid Form Data. In Sample #4 a Submit Button is made visible only when all required field data is entered. This same technique could also be used to make a Signature field visible.

Labels for radio buttons - Radiobuttons, In its most simple form, a radio button is simply an input element with the type property the element won't be identifiable when posting the form back to a server for This is due to the nature of the radio button, which always offers you to decide be triggered by clicking both the actual radio button AND the attached label. Below is how the radio button component looks like. Putting it all together, Assuming we want to have an email input, name (TextInput), gender (Select Input), radio input all in a form control.

Comments
  • Sorry did not work. Answer always ends up being the same regardless.
  • The guy who wrote the script says I have javascript on my page that is always setting the radio button to ON but that is not the case. The only javascript I have on there is the jquery file. This is driving me insane
  • @Pekkka thanks. I just posted the form code at the top on my original posting.
  • @gteh you seem to have only one radio button. Isn't the one button selected in that case anyway? The normal use case is to have multiple radio buttons. Maybe what you're looking for is a checkbox?
  • I tried with a checkbox too and the same thing happens. I only have one radio button because that field is optional, so I figured if not selected, it would default to OFF. I did switch it out for a checkbox though and I had the same issue.
  • testing but doesn't seem to work.. have the same issue. Only this time the answer is always NO instead of YES.
  • 1. print value of $_POST['news'] after submit and before this (if (isset($_POST['news']) && $_POST['news'] == 'yes')) condition comes. so we come to know what is the value of $_POST['news']. 2 If you are trying this condition with news = '' then it will give you result "No"