change value of a radio button based on database value in php - php

i have a radio button in my form
<input type="radio" id="radio-2" name="radio" checked onclick="calculate()" >
how to set the radio button value based on database value like
if(db_val==2){
value=50
} else{
value=100
}

Radio button value is depend on checked attribute
If you set the checked attribute with condition then its value will be set
Example:
<input type="radio" id="radio-2" name="radio" value="50" <?php echo ($db_val==2)?'checked':'' ?> onclick="calculate()" >
<input type="radio" id="radio-2" name="radio" value="100" <?php echo ($db_val!=2)?'checked':'' ?> onclick="calculate()" >

Related

How to fetch textbox value in radio button value in php

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"]

How to get individual values of radio buttons that are added dynamically?

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

Using a default value with an html form radio button

I have a web form that updates a customer record. The entries are prefilled with the data stored in mysql when called. One field shows if equipment was returned as follows:
<font size=5>Returned:</font><input type="text" name="ud_Returned" value="<?php echo $rtrnd; ?>" /><br />
My question is I setup to convert this to radio button like:
<font size=5>Returned:</font><input type="radio" name="ud_Returned" value=Yes /> Yes<br />
<input type="radio" name="ud_Returned" value="No" /> No<br />
But I want it so the value stored in the $rtrnd variable fills in the existing radio button with the appropriate Yes/No for that customer when form is called. Any ideas?
Maybe something like this would do the trick:
<input type="radio" name="ud_Returned" value="Yes" <?php if($rtrnd) echo 'checked'; ?> />
<input type="radio" name="ud_Returned" value="No" <?php if(!$rtrnd) echo 'checked'; ?> />
Assuming that $rtrnd is a boolean value. If it isn't just use a comparison in the if statements like if($rtrnd == 'yes').
Assuming your store this information as 1 or 0 in the database you'll have to -
<?php
// Get stuff from database
$result = /* The resulting array from your query */;
if( $result["ud_Returned"] == 1 )
{
echo('<font size=5>Returned:</font>
<input type="radio" name="ud_Returned" value="Yes" checked="checked" /> Yes<br />
<input type="radio" name="ud_Returned" value="No" /> No<br />');
}
else
{
echo('<font size=5>Returned:</font>
<input type="radio" name="ud_Returned" value="Yes" /> Yes<br />
<input type="radio" name="ud_Returned" value="No" checked="checked" /> No<br />');
}
I'm not suggesting that this is the exact implementation you should pursue but this is the logic - get information from database, make a comparison, add the checked attribute to the appropriate radio button.

populate radio button value after submit

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 ;-)

receiving radio box value in php

I have 2 following radio boxes in a form,
<input type="radio" name="radio" value="yes" class="radio" /> Yes
<input type="radio" name="radio" value="no" class="radio" /> No
How can i recieve the value of the radio button once the form is posted (in PHP)
Once it is posted on the same page, how can I remember the selected radio button and keep that checked?
Thanks.
1) The value of the radio button is saved in $_POST only if any of the choices was selected.
if (isset($_POST['radio'])) // if ANY of the options was checked
echo $_POST['radio']; // echo the choice
else
echo "nothing was selected.";
2) Just check for the value and add checked='checked' if it matches.
<input type="radio" name="radio" value="yes" class="radio" <?php if (isset($_POST['radio']) && $_POST['radio'] == 'yes'): ?>checked='checked'<?php endif; ?> /> Yes
<input type="radio" name="radio" value="no" class="radio" <?php if (isset($_POST['radio']) && $_POST['radio'] == 'no'): ?>checked='checked'<?php endif; ?> /> No
<input type="radio" name="radio" value="yes" class="radio" /> Yes
<input type="radio" name="radio" value="no" class="radio" /> No
u get radio value using $_POST['radio'];
simple bro,
<input type="radio" name="radio" <?php if($_POST['radio']=="yes") echo "checked";?> value="yes" class="radio" /> Yes
u have to identify radio box by value man
How can i recieve the value of the radio button once the form is posted (in PHP)
$_POST['radio']
Once it is posted on the same page, how can I remember the selected radio button and keep that checked?
Add a checked attribute if the value is equal to $_POST['radio'].
1) u will receive only that radio box value via POST which is checked
$radio_value=$_POST['radio'];
2)
<input type="radio" name="radio" value="yes" class="radio"
<?php echo ($radio_value == 'yes') ? 'checked="checked"' : ''; ?>/> Yes
<input type="radio" name="radio" value="no" class="radio"
<?php echo ($radio_value == 'no') ? 'checked="checked"' : ''; ?>/> No

Categories