Code for radio button

Good day!

I never used radio button. Now I need to add radio button in my webpage. Radio button 1 is mother and radio button 2 is child. Is it in my database it is only 1 field or separate field one for mother and one for child?

Kindly give me a code for radio button.

Thank you…

Here’s a quick example.
Fire up your HTML editor and try this:


<form action="">
<input type="radio" name="sex" value="male" /> Male<br />
<input type="radio" name="sex" value="female" /> Female
</form>

From here you can use PHP to make this more
dynamic.

Such as getting the categories for the radio
button from the database.

Did this help?

Well… If there are two conditions here, one being mother, the other being child, then it would be logical to use ONE field (e.g. value = 0 = mother, value = 1 = child).

Here is some simple code for a form with two radio buttons in it:


<form action="name_of_php_file_with_insert_code.php" method="post" id="formid" name="formname">
<input type="radio" name="mother" id="mother" value="Mother" /> Mother<br />
<input type="radio" name="child" id="child" value="Child" /> Child
</form> 

Is it only one field in the database?

Thank you

I used it but how can I make sure that if the user choose the mother it would save in the database?and if the mother is already choose the child would not be clickable

<form action=“name_of_php_file_with_insert_code.php” method=“post” id=“formid” name=“formname”>
<input type=“radio” name=“person” id=“mother” value=“Mother” /> Mother<br />
<input type=“radio” name=“person” id=“child” value=“Child” /> Child
</form>

then the sql is

insert into ‘tablename’(‘person’)values(“$_POST[‘person’]”)

All of the radio buttons in a group are clickable EXCEPT for the one already selected. Clicking on any of the others will change which one is selected.

With a radio button group there is only one field regardless of whether there are two buttons or two thousand buttons. The buttons just select what value to put in the field.

So with those two radio buttons and mother already selected the only clickable button is child. If it is clicked then it will be selected and not clickable and mother will be unselected and clickable.

Not necessarily so.

Let’s say you have a DB table containing records that will be
used as radio button category.

Simply loop through the search result of that and add the
selection.

Let’s say you’ve place the results in an array so the code
would look something like this:


$radioNum = 0;

foreach ($searchResult as $radioSelection) {
    
    $radioNum++;
    
    echo '<input type="radio" name="radio' . $radioNum . '" value="'. $radioSelection .'" />' . $radioSelection;
    
}

I know this is very rough but I’m just trying to
communicate the idea.

Plus I’m just stealing time here in the office :smiley:

Could someone give another example here?

Doing that would make each radio button a separate group of one and therefore invalid since you must have at least two radio buttons per group. The code you have there is more appropriate for checkboxes where each checkbox is a separate field.

With radio buttons you have one field per group of buttons and each button just supplies a different value.

I want to happen is if the user choose mother the mother was save in the database

i give the answer you’d better try it:D

Each user’s form should have its own record in the database.

Is it theres no code for the saving the choose radio button in the database?

Thank you

Best thing to do here is to test it right.

Check if you can get the right values upon
form submission.

Once the user submits the form get the value
of the radio button that was selected and
insert that to the database.

I can’t give an example but that’s the idea.

Anyone mind giving a concrete example?

Once the form is submitted the radio button group is no different from an ordinary input field and can be processed the same way. The only difference is that the only values you expect the field to have are those of the buttons in the group.

but in my situation, when the mother was click and click the child they are both click

This shouldn’t happen normally.

It would help if you could show us the code.

What should be the field type for this radio button in the database?

Thank you

That is dependent on what values you use with the buttons. If you use numbers as the values then you can use a integer field. If you use text then char or varchar.

I used this code:


include 'con.php';
$query = "INSERT INTO transact (pack_setting) VALUES ('" . $_POST["pack_setting"] . "')";

echo "<input type='radio' name='pack' id='mother' value='Mother'  /> Mother Lot<br />";
echo "<input type='radio' name='pack' id='child' value='Child' /> Child Lot";


But when i choose Mother radio button it did not save to the database