PHP set the value of a radio group dynamically? - php

To allow a user to edit information in a record, this is done:
$case=$_GET['case'];
$query="SELECT * FROM `cases` WHERE `case`= '$case'";
$result=mysql_query($query);
<input type="text" name="firstname" value="<?php echo $firstname; ?>" />
I need to set the value of a radio group based on what its value is in the "cases" table.
<input type="radio" name="flight1_departing" value="AM" />
<input type="radio" name="flight1_departing" value="PM" />
How is this possible?

<input <?php if ($somevalue == 'AM') echo 'checked="checked"'; ?> type="radio" name="flight1_departing" value="AM" />
<input <?php if ($somevalue == 'PM') echo 'checked="checked"'; ?> type="radio" name="flight1_departing" value="PM" />

Given a known value $val, you just need to check it against each radio button value and set the checked attribute, eg
<input type="radio" name="flight1_departing" value="AM"
<?php if ($val == 'AM') : ?>checked="checked"<?php endif ?>
/>
<input type="radio" name="flight1_departing" value="PM"
<?php if ($val == 'PM') : ?>checked="checked"<?php endif ?>
/>
That example is very manual. It would be easier if the radio elements are created in a loop.

Your questions is a little ambiguous but I'm going on the assumption that you mean you need to determine which value is default checked based on the value in the cases table?
Something like,
<input type="radio" name="flight1_departing" value="AM" <?php if ($some_cases_value) { print 'CHECKED'; } ?>/>
<input type="radio" name="flight1_departing" value="PM" <?php if ($some_cases_value) { print 'CHECKED'; } ?> />
Though there is likely a very more elegant way of doing it?

Related

Pre-populate a lot of radio button group from database

I'm working on a project. I want to pre-populated data from database.
HTML
<div class="question">
<input type="radio" name="q1" value="A" <?php echo $checkedA ?>>
<input type="radio" name="q1" value="B" <?php echo $checkedB ?>>
<input type="radio" name="q1" value="C" <?php echo $checkedC ?>>
</div>
<div class="question">
<input type="radio" name="q2" value="A" <?php echo $checkedA ?>>
<input type="radio" name="q2" value="B" <?php echo $checkedB ?>>
<input type="radio" name="q2" value="C" <?php echo $checkedC ?>>
</div>
<div class="question">
<input type="radio" name="q3" value="A" <?php echo $checkedA ?>>
<input type="radio" name="q3" value="B" <?php echo $checkedB ?>>
<input type="radio" name="q3" value="C" <?php echo $checkedC ?>>
</div>
<!-- etc till let's say 30 question -->
PHP
$query="SELECT * FROM `quiz` WHERE email='$email'";
$result=mysqli_query($link,$query);
$data=mysqli_fetch_assoc($result);
for($i=1; $i<=30; $i++){
switch($data[${"answer".$i}]{
case "A" : $checkedA="checked"; break;
case "B" : $checkedB="checked"; break;
case "C" : $checkedC="checked"; break;
}
}
Then, how to make the q1 which is corresponded with $data['answer1'] checked if it is filled with data from database, etc?
Using the code you've provided, It's viable to get the data from the database and then show the result in your html and not over complicate things. To pre-populate your radio buttons, you need to have the following below:
PHP
$query="SELECT * FROM `quiz` WHERE email='$email'";
$result=mysqli_query($link,$query);
$data=mysqli_fetch_assoc($result);
HTML
<div class="question">
<input type="radio" name="q1" value="A" <?php echo ($data['q1'] == 'A')? 'checked' : ''; ?>>
<input type="radio" name="q1" value="B" <?php echo ($data['q1'] == 'B')? 'checked' : ''; ?>>>
<input type="radio" name="q1" value="C" <?php echo ($data['q1'] == 'C')? 'checked' : ''; ?>>
</div>
<div class="question">
<input type="radio" name="q2" value="A" <?php echo ($data['q2'] == 'A')? 'checked' : ''; ?>>
<input type="radio" name="q2" value="B" <?php echo ($data['q2'] == 'B')? 'checked' : ''; ?>>
<input type="radio" name="q2" value="C" <?php echo ($data['q2'] == 'C')? 'checked' : ''; ?>>
</div>
<div class="question">
<input type="radio" name="q3" value="A" <?php echo ($data['q3'] == 'A')? 'checked' : ''; ?>>
<input type="radio" name="q3" value="B" <?php echo ($data['q3'] == 'B')? 'checked' : ''; ?>>
<input type="radio" name="q3" value="C" <?php echo ($data['q3'] == 'C')? 'checked' : ''; ?>>
</div>
With that piece of code, it sets checked depending on what the value coming from the database is and displays it on the screen.
Example has exactly 3 variables for checked ($checkedA, $checkedB, $checkedC). In order to have 3 varsiables per question, it would need a collection of "checks". If you have n questions, your collection needs n members. And each of those members needs 3 members representing whether the answer is checked, one for each option.
As an example:
If the answer to q1 is A, then
$checked['q1'] = ['A'=> "checked",'B'=>"",'C'=>""].
In the php, initialize a $checked member for each question as described above. Then change the switch to update the corresponding member. [The reason to initialize this way is because 1) everything needs a value in the html and 2) checked is a boolean attribute]
The html would echo $checked[q#]['A'] $checked[q#]['B'] and $checked[q#]['C'] respectively.

How to Make Check box is checked

How can I make my check box is checked using PHP, when i visit the page later i want previously selected check boxes are checked
<input name="product[]" type="checkbox" value="1" />
Start Session as
<?php
session_start();
$session_products = array();
if(array_key_exists("products", $_SESSION))
{
if($_SESSION["products"] != null)
{
$session_products = $_SESSION["products"];
}
}
?>
Change your code as follows
<input name="product[]" type="checkbox" value="1" <?php if(in_array("1", $session_products)) echo "checked='checked'"; ?>/>
You can add a checked.
Here an example:
<input name="product[]" type="checkbox" value="1" checked />
By the way, your value=1 seems to be wrong. Normally you use `value to distinguish items.
Example:
<input name="product[]" type="checkbox" value="dvd" checked />
<input name="product[]" type="checkbox" value="cd" />
to do something like this
with php
<input name="product[]" type="checkbox" value="1" <?php echo ($value =="1") ? "checked" : ""; ?> />
where $value is the value from database

if checkbox is checked change the value using PHP

I'm trying to accomplish one simple task (but is not simple for me).
I have a form and I'm having a trouble with this check box.
<input type="checkbox" name="b"
<?php if (isset($_POST[b])) echo "value='y'"; else echo "value='n'"; ?>/>
I'm not sure if I use the right one, but it doesn't work for me.
So basically I want the value of the input of b will be y if the checkbox is checked else it will always be n if the checkbox is unchecked.
That's not how a checkbox works.
It's checked when the checked attribute is there.
<input type="checkbox" name="a" value="a" checked /> Checked
<input type="checkbox" name="a" value="a" /> NOT Checked
So you want to use
<input type="checkbox" name="a" value="a" <?php echo isset($_POST['b']) ? "checked" : ""; ?>/>
Now if $_POST['b'] is set, the checkbox will be checked.
Also, you have $_POST[b]. The b should be in quotes. It should be $_POST['b']
You have to use two conditions one is for showing checked/unchecked and second for showing y/n
<input type="checkbox" name="b" <?php echo (isset($_POST['b'])?"value='y'":"value='n'")?>
<?php echo (isset($_POST['b'])?"checked":"") ?> />
tested code!
<form method="post">
<input type="checkbox" name="b" <?php if (isset($_POST['b'])) echo "value='y'"; else echo "value='n'"; ?>/>
<input type="submit" name="asd" value="asd">
</form>
So go with the following
<?php if (isset($_POST['b'])) echo "value='y'"; else echo "value='n'"; ?>
This worked for me:
first: Giving the checkbox a default value
then: assign the desired value only if the box is checked.
<input type="hidden" name="b" value="n">
<input type="checkbox" name="b" value="y" >
*Approved by Deployment:
<input type="radio" name="dep_approval_status" value="Approved"
<?php
if ($deploy['dep_approval_status'] === "Approved")
{
echo ' checked';
}
?>
/> Yes, approved
<input type="radio" name="dep_approval_status" value="Not Approved"
<?php
if ($deploy['dep_approval_status'] === "Not Approved")
{
echo ' checked';
}
?>
/> Not Approved

Save radio button status php

Form table is introduced inside an echo and, for the texts fields, I use value=" ' .$_POST['name'] to set default value if form was already sent at least one time. It works properly.
However, how could I save radio buttons status when form was already sent? Thanks.
<tr>
<td colspan="2" align="left" valign="top"> <input type="radio" name="ambiente" value="si" />
Si
<input type="radio" name="ambiente" value="no" />
No</td>
</tr>
Simply:
<input type="radio" name="ambiente" value="si" <?php if ($_POST['ambiente'] == 'si') echo 'checked'; ?> /> Si
<input type="radio" name="ambiente" value="no" <?php if ($_POST['ambiente'] == 'no') echo 'checked'; ?> /> No
<?php
$checked = NULL;
if(isset($_POST['ambiente']) && $_POST['ambiente'] == 'no') {
$checked = 'checked="checked"';
}
?>
<input type="radio" name="ambiente" value="no" <?php echo $checked ?> />
Or this can be shown on one line:
<input type="radio" name="ambiente" value="no" <?php if (isset($_POST['ambiente']) && $_POST['ambiente'] == 'no') echo 'checked="checked"'; ?> />

How to set the value for Radio Buttons When edit?

I have a Gender Row with radio buttons male & female. when i register as first time the values of radio button will store in database. Now my question is if i edit that row again it want to come(that means checked) with that value as male/female. how to make it?
Note : Doing with php.
HTML Script :
<tr id="inside">
<td align="right" width="40%" id="side" >Gender</td>
<td width="3%"> </td>
<td align="left" width="50%">
<input type="radio" name="sex" value="Male" size="17">Male
<input type="radio" name="sex" value="Female" size="17">Female
</td>
</tr>
When you populate your fields, you can check for the value:
<input type="radio" name="sex" value="Male" <?php echo ($sex=='Male')?'checked':'' ?>size="17">Male
<input type="radio" name="sex" value="Female" <?php echo ($sex=='Female')?'checked':'' ?> size="17">Female
Assuming that the value you return from your database is in the variable $sex
The checked property will preselect the value that match
just add 'checked="checked"' in the correct radio button that you would like it to be default on. As example you could use php quick if notation to add that in:
<input type="radio" name="sex" value="Male" size="17" <?php echo($isMale?'checked="checked"':''); ?>>Male
<input type="radio" name="sex" value="Female" size="17" <?php echo($isFemale?'checked="checked"':''); ?>>Female
in this example $isMale & $isFemale is boolean values that you assign based on the value from your database.
This is easier to read for me:
<input type="radio" name="rWF" id="rWF" value=1 <?php if ($WF == '1') {echo ' checked ';} ?> />Water Fall</label>
<input type="radio" name="rWF" id="rWF" value=0 <?php if ($WF == '0') {echo ' checked ';} ?> />nope</label>
Gender :<br>
<input type="radio" name="g" value="male" <?php echo ($g=='Male')?'checked':'' ?>>male <br>
<input type="radio" name="g" value="female"<?php echo ($g=='female')?'checked':'' ?>>female
<?php echo $errors['g'];?>
For those who might be in need for a solution in pug template engine and NodeJs back-end, you can use this:
If values are not boolean(IE: true or false), code below works fine:
input(type='radio' name='sex' value='male' checked=(dbResult.sex ==='male') || (dbResult.sex === 'newvalue') )
input(type='radio' name='sex' value='female' checked=(dbResult.sex ==='female) || (dbResult.sex === 'newvalue'))
If values are boolean(ie: true or false), use this instead:
input(type='radio' name='isInsurable' value='true' checked=singleModel.isInsurable || (singleModel.isInsurable === 'true') )
input(type='radio' name='isInsurable' value='false' checked=!singleModel.isInsurable || (singleModel.isInsurable === 'false'))
the reason for this || operator is to re-display new values if editing fails due to validation error and you have a logic to send back the new values to your front-end
If you are getting your values from a database table and creating radio buttons dynamically, here is the solution. The database records are fetched into an array in this example and used for creating radio buttons.
<?php foreach ($dbrecords as $item) : ?>
<div class="form-check form-check-inline">
<input class="form-check-input" type="radio" name="paymentMethod" id="paymentMethod" value=<?php echo $item["Id"]; ?> <?php echo ($paymentMethod == $item["Id"]) ? 'checked' : '' ?>><?php echo $item["Name"]; ?>
</div>
<td><input type="radio" name="gender" value="Male" id="male" <? if($gender=='Male')
{?> checked="" <? }?>/>Male
<input type="radio" name="gender" value="Female" id="female" <? if($gender=='Female') {?> checked="" <?}?>/>Female<br/> </td>

Categories