I have 3 radio buttons in my form. 2 of them have static values and the 3rd one has a radio button that should have the same value as the number entered in the textbox. I am able to fetch the first 2 radio buttons values by using $_POST method on next page but for the 3rd radio button, the value is null. Please help me get the textbox value for the 3rd radio button. The code is :
<input type="radio" name="radio" value="500000" checked="checked" />Purchase More than 5 Lakhs
<input type="radio" name="radio" value="2000000" />Purchase More than 20 Lakhs
<input type="radio" name="radio" value="" />Purchase More than : <input type="text" name="radio" />
If you want to solve this using PHP, try this approach:
<input type="radio" name="radio" value="500000" checked="checked" />Purchase More than 5 Lakhs
<input type="radio" name="radio" value="2000000" />Purchase More than 20 Lakhs
<input type="radio" name="radio" value="0" />Purchase More than : <input type="text" name="user_entered" />
In your PHP file:
$result = $_POST['radio']?$_POST['radio']:$_POST['user_entered'];
This means: if radio is set, use radio value; else use the user_entered value.
Of course, you should further check if user_entered contains a valid value, depending on your requirements.
Your inputs should have different names. Call your type="text" input something different from radio and then you should be able to do $_POST["newName"]
Related
This question already has an answer here:
I have Multiple radio button with same name in it of array and have same values as 1
(1 answer)
Closed 4 years ago.
i have multiple radio button in same name as array and same value for all.
<input type="radio" name="radio_name[]" id="radi_name" value="1" checked>
<label for="radio1">Set as Default</label>
<input type="radio" name="radio_name[]" id="radi_name" value="1" >
<label for="radio1">Set as Default</label>
<input type="radio" name="radio_name[]" id="radi_name" value="1" >
<label for="radio1">Set as Default</label>
<input type="radio" name="radio_name[]" id="radi_name" value="1" >
<label for="radio1">Set as Default</label>
in php i used like this
$a[]=$_post['radio_name'];
prinr_r($a);
im getting result like this :
Array ( [0] => 1 )
if i uncheck the button set as zero i want result to be like this
Array ( [0] => 1,[1] => 0,[2] => 0,[3] => 0 )
Please check this images i have form like this
You need to use only [] instead of array keys also use checkbox instead of radio . So use following code it will work for you.
<input type="checkbox" name="radio_name[]" id="radi_name1" value="1" checked>
<label for="radio1">Set as Default</label>
<input type="checkbox" name="radio_name[]" id="radi_name2" value="1" >
<label for="radio1">Set as Default</label>
<input type="checkbox" name="radio_name[]" id="radi_name3" value="1" >
<label for="radio1">Set as Default</label>
<input type="checkbox" name="radio_name[]" id="radi_name4" value="1" >
<label for="radio1">Set as Default</label>
I am pretty sure that radio buttons don't get sent to the server if none of the values is selected. In your html you really have 4 different radio button groups so only the ones that have a selected value, get sent.
If you want 4 groups where a value always gets sent for each group, you should do something like this:
<input type="radio" name="radio_name[1]" value="1" id="radio1" checked>
<input type="radio" name="radio_name[1]" value="0">
<label for="radio1">Set as Default</label>
<input type="radio" name="radio_name[2]" value="1" id="radio2">
<input type="radio" name="radio_name[2]" value="0" checked>
<label for="radio2">Set as Default</label>
// etc.
This way you will have independent radio button groups and you will get the result you want.
You have to use checkbox instead of radio. Radio only allowed 1 value being posted, and remove the array key like dipmala suggested.
I have a drop down that's numbered 1-30. So if I select '2', my radio buttons will appear like this:
<input type="radio" id="yes-sdf-1" name="field[1][sdfradio]" value="Yes" />
<input type="radio" id="no-sdf-1" name="field[1][sdfradio]" value="No" />
<input type="radio" id="yes-sdf-2" name="field[2][sdfradio]" value="Yes" />
<input type="radio" id="no-sdf-2" name="field[2][sdfradio]" value="No" />
How do I get the individual values of the radio buttons because I want to set conditions that will only affect those with the same name.
Here is an example to give you an idea.
Try this:
$('input:radio').click(function(){
alert($(this).attr('name'));
});
This will return the name of the radio button.
JSFiddle
I am developing a PHP website which will generate several approval requests.
My approval page has only one form and one save button, and I want to use 2 radio buttons for each request.
Here is a link to how I imagine it to look like: link
On the PHP side I have no problem with generating the form and assigning different names to the radio buttons. My problem is what names I should give to the radio buttons in order for me to be able to associate them to a specific member on the PHP side? Or do I need to set values for the radio buttons?
How would I accomplish this?
If you have an even better solution to handle such a case I am open to that as well.
Thanks
Every User will have a unique user_id,
As you are displaying radiobuttongroup for each other user in the page, and want to access then in php,
The base way to name them in a array with indices as their user_id
<input type="radio" value="1" name="acceptance['uid1']"/>Approve<br />
<input type="radio" value="0" name="acceptance[uid1]"/>Deny
<input type="radio" value="1" name="acceptance['uid2']"/>Approve<br />
<input type="radio" value="0" name="acceptance[uid2]"/>Deny
<input type="radio" value="1" name="acceptance['uid3']"/>Approve<br />
<input type="radio" value="0" name="acceptance[uid3]"/>Deny
in php
foreach($_POST['acceptence'] as $uid=>$acpt)
{
..Do whatever
}
I hope this would help..If something else is needed plz comment..
Here you are:
<input type="radio" value="approve" name="acceptance"/>Approve<br />
<input type="radio" value="deny" name="acceptance"/>Deny
With radio buttons, they should named the same and have different values for grouping them.
UPDATED
To me, I will add row id to each radio group buttons. For example
<input type="radio" value="19" name="acceptance1"/>Approve<br />
<input type="radio" value="0" name="acceptance1"/>Deny
...
<input type="radio" value="21" name="acceptance2"/>Approve<br />
<input type="radio" value="0" name="acceptance2"/>Deny
...
<input type="radio" value="23" name="acceptance5"/>Approve<br />
<input type="radio" value="0" name="acceptance5"/>Deny
Assuming we have 5 rows per page and each approve radio button holds id of a records in database.
<?php
$approve = array();
for($i=1;$i<=5;$i++)
$approve[] = $_POST['acceptance'.$i];
?>
Then you will get what row has approve selection (i.e value !=0). Hope this helps
I try to implement the follwing code:
<?php
$yesno1="";
$yesno2="";
$option1="";
$option2="";
$option3="";
if(isset($_POST['submit'])){
$yesno1=$_POST['yesno1'];
$yesno2=$_POST['yesno2'];
$option1=$_POST['option1'];
$option2=$_POST['option2'];
$option3=$_POST['option3'];
}
?>
<form method="POST" name="contact_form"
action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>">
<input type="radio" name="yesno1" value="yes" style="outline:0;"/>Yes
<input type="radio" name="yesno1" value="no" style="outline:0;"/>No
<input type="radio" name="yesno2" value="yes" style="outline:0;"/>Yes
<input type="radio" name="yesno2" value="no" style="outline:0;"/>No
<input type="checkbox" name="option1" value="Milk"> Milk<br>
<input type="checkbox" name="option2" value="Butter" checked> Butter<br>
<input type="checkbox" name="option3" value="Cheese"> Cheese<br>
<input type="submit" value="Submit" name='submit'>
</form>
Once I click submit button, normally the value I seleted in radio and marked in the checkbox are gone, so how can I keep the value even after the click the submit button or refrsh the page using php, javascript is fine as well, would be better if any one can help me with it on both php and javascript since I am not very good on both of them, thanks for kind help:)
An example below
<input type="checkbox" name="option1" value="Milk" <?php echo ($_POST['option1'] == 'Milk' ? 'checked' : '') ?>> Milk<br>
From MDN:
The input (<input>) element is used to create interactive controls for
web-based forms.
Attributes
type
[...]
radio: A radio button. You must use the value attribute to define the
value submitted by this item. Use the checked attribute to indicate
whether this item is selected by default. Radio buttons that have the
same value for the name attribute are in the same "radio button
group"; only one radio button in a group can be selected at one time.
This is a basic HTML question. Learn to walk before you run ;-)
I currently have a form with lets say 5 radio button entries (see below).
What im looking archive is the following:
- Being able to choose multiple radio buttons - lets say 3 and submit the form.
Currently I got it working fine with PHP, SQL, but im only able to choose one radiobutton and submit that.
I figure it would also come in handy being able to deselect a radio button in case you wrongly click one.
My guess is that this can be done through some javascript? Any suggestions? Online examples perhaps?
<form id="pollform" action="poll.php" method="post">
<input id="option-1" type="radio" value="1" name="poll">
<label for="option-1">Select option 1</label>
<input id="option-2" type="radio" value="2" name="poll">
<label for="option-2">Select option 2</label>
<input id="option-3" type="radio" value="3" name="poll">
<label for="option-3">Select option 3</label>
<input id="option-4" type="radio" value="4" name="poll">
<label for="option-4">Select option 4</label>
<input id="option-5" type="radio" value="5" name="poll">
<label for="option-5">Select option 5</label>
</form>
Radio buttons are designed so that only one option from each group (as designated by their shared name) can be selected at once (just like you can only tune a radio to one station).
The input control which allows any number of options to be selected is the checkbox. If you append [] to their names, then the selected options will arrive on the PHP side as an array.
<input type="checkbox" value="1" name="poll[]" />
<input type="checkbox" value="2" name="poll[]" />
As it has same name poll you will not be able to do that as input type radio is specialized in selecting a single value from multiple inputs.
You can use input type checkbox for that and make them as an array:
<form id="pollform" action="poll.php" method="post">
<input id="option-1" type="checkbox" value="1" name="poll[]">
<label for="option-1">Select option 1</label>
<input id="option-2" type="checkbox" value="2" name="poll[]">
<label for="option-2">Select option 2</label>
<input id="option-3" type="checkbox" value="3" name="poll[]">
<label for="option-3">Select option 3</label>
<input id="option-4" type="checkbox" value="4" name="poll[]">
<label for="option-4">Select option 4</label>
<input id="option-5" type="checkbox" value="5" name="poll[]">
<label for="option-5">Select option 5</label>
</form>
LIMIT (with jQuery) the number:
$("input[type=checkbox][name=poll[]]").click(function() {
var numberSel = $("input[type=checkbox][name=poll[]]:checked").length >= 3;
$("input[type=checkbox][name=poll[]]").not(":checked").attr("disabled",numberSel);
});
Radio buttons are designed for only 1 item to be selected, use checkboxes instead.