show div as per radio button value in database - php

Open a div on based on database radio button value. if value is male in database then show For male and if value is female in database then show Female male
$status = isset($_POST['status']) ? $_POST['status'] : '';
<input type="radio" name="status" value="male" checked="checked">male<br>
<input type="radio" name="status" value="female">female<br>
<div class="1">For male</div>
<div class="2">Female male</div>

if i understand you right this will do the trick
$status = isset($_POST['status']) ? $_POST['status'] : '';
if ($status == "male") {
<input type="radio" name="status" value="male" checked="checked">male<br>
<input type="radio" name="status" value="female">female<br>
}elseif ($status == "female") {
<input type="radio" name="status" value="male">male<br>
<input type="radio" name="status" value="female" checked="checked">female<br>
}else {
<input type="radio" name="status" value="male">male<br>
<input type="radio" name="status" value="female">female<br>
}

Just add if condition
$status = isset($_POST['status']) ? $_POST['status'] : ''; ?>
<input type="radio" name="status" value="male" <?php echo $status=='male'? 'checked="checked"':''?>>male<br>
<input type="radio" name="status" value="female" <?php echo $status=='female'? 'checked="checked"':''?>>female<br>
<?php if($status=='male') { ?>
<div class="1">For male</div>
<?php } ?>
<?php if($status=='female') { ?>
<div class="2">For Female</div>
<?php } ?>

Related

How to check or uncheck the radio button according to value in php?

I am stuck on the issue.
I have two radio buttons. One is a Buyer and the second is the Seller.
<input type="radio" name="account_type" value="1" id="acc_buyer" checked="checked">Buyer
<input type="radio" name="account_type" value="2" id="acc_seller">Seller
The Buyer radio button is already checked.
Now What I am doing is, I have the below code, and if the $account_type==2 then I have to select the seller radio button and unchecked the buyer.
I tried the below code
if(isset($_SESSION['error'])){
$error = $_SESSION['error'];
$account_type = $_SESSION['account_type'];
unset($_SESSION['account_type']);
unset($_SESSION['error']);
}
<input type="radio" name="account_type" value="1" id="acc_buyer" checked="checked"> Buyer
<input type="radio" name="account_type" value="2" id="acc_seller" <?php if(isset($account_type) && $account_type=='2') { ?> checked="checked" <?php }?>> Seller
<?php
if(isset($_SESSION['error'])){
$error = $_SESSION['error'];
$account_type = $_SESSION['account_type'];
unset($_SESSION['account_type']);
unset($_SESSION['error']);
}
?>
<input type="radio" name="account_type" value="1" id="acc_buyer" checked="checked">
<input type="radio" name="account_type" value="2" id="acc_seller" <?php if($account_type==2){echo 'checked="checked"';}?>>
Here's the correction with the added empty() check.
$checked = [];
$checked[0] = empty($account_type) || (isset($account_type) && $account_type == 1) ? 'checked' : '';
$checked[1] = isset($account_type) && $account_type == 2 ? 'checked' : '';
<input type="radio" name="account_type" value="1" id="acc_buyer" checked="<?php echo $checked[0] ?>">
<input type="radio" name="account_type" value="2" id="acc_seller" checked="<?php echo $checked[1] ?>">
I hope that I don't duplicate other's answers and my solution isn't unnecessary, but I used this code snippet:
<?php
if(isset($_SESSION['error']))
{
$error = $_SESSION['error'];
$account_type = $_SESSION['account_type'];
unset($_SESSION['account_type']);
unset($_SESSION['error']);
}
$account_type_set = isset($account_type);
$buyer = $account_type_set ? ($account_type == 2 ? 0 : 1) : null;
$seller = $account_type_set ? !$buyer : null;
?>
<input type="radio" name="account_type" value="1" id="acc_buyer" <?php if($buyer) { ?> checked="checked" <?php }?>> Buyer
<input type="radio" name="account_type" value="2" id="acc_seller" <?php if($seller) { ?> checked="checked" <?php }?>> Seller
I found the answer: What I did, I am checking the $account_type is empty then add checked. So I am getting a buyer checked.
I don't know this is the best answer or not but it's solved my issue.
I tried
if(isset($_SESSION['error'])){
$error = $_SESSION['error'];
$account_type = $_SESSION['account_type'];
unset($_SESSION['account_type']);
unset($_SESSION['error']);
}
<input type="radio" name="account_type" value="1" id="acc_buyer" <?php if(empty($account_type)){?> checked="checked" <?}?>>Buyer
<input type="radio" name="account_type" value="2" id="acc_seller" <?php if( isset($account_type) && $account_type=='2'){?> checked="checked" <?}?>> Seller

Check radio button based on php variable

I am able to set the value of a text field from a php variable so that when the page is refreshed, the text field remains filled in like so:
<td><input type="text" name="user" value="<?php echo ($_REQUEST['user']); ?>" /></td>
However, I would like to do the same for a radio button with a different variable, $gender. How could I do this?
<td><input type="radio" name="gender" value="male"> Male<br>
<input type="radio" name="gender" value="female"> Female<br>
<input type="radio" name="gender" value="other"> Other</td>
This is called a ternary operator
<?php
'<td><input type="radio" name="gender" value="male" '.($_REQUEST['gender'] == "male" ? 'checked' : '').'> Male<br>
<input type="radio" name="gender" value="female" '.($_REQUEST['gender'] == "female" ? 'checked' : '').'> Female<br>
<input type="radio" name="gender" value="other" '.($_REQUEST['gender'] == "other" ? 'checked' : '').'> Other</td>'
?>
If you want to automate this for multiple radio groups, you could write a function:
<?php
declare (strict_types=1);
function radio_buttons(string $name, array $values_labels, int $indent = 4)
{
$ind = str_repeat(' ', $indent);
foreach ($values_labels as $value => $label)
{
$checked = ($_REQUEST[$name] ?? '') === $value ? ' checked' : '';
echo <<<__EOF__
$ind<input type="radio" id="radio-$name-$value" name="$name" value="$value"$checked><label for="radio-$name-$value">$label</label>
__EOF__;
}
}
?>
<body>
<form>
<?php radio_buttons('gender', ['male' => 'Male', 'female' => 'Female', 'other' => 'Other'], 4); ?>
<button type="submit">submit</button>
</form>
</body>

How to fill different form fields with values using php

I'm having issues with filling 3 types of form input. Radio buttons, select (drop-down list) and textarea.
<textarea name="kommentar" cols="25" rows="7" value="<?php echo "$comment";?>" required></textarea>
<select name="interesse" required>
<option disabled selected>Bitte auswählen</option>
<option>Java</option>
<option>PHP</option>
<option>C++</option>
<option>Ruby</option>
<option>SQL</option>
<option>PLSQL</option>
</select>
<fieldset>
<label for="bewertung">
<input type="radio" name="bewertung" value="1" required />1
<input type="radio" name="bewertung" value="2" required />2
<input type="radio" name="bewertung" value="3" required />3
<input type="radio" name="bewertung" value="4" required />4
<input type="radio" name="bewertung" value="5" required />5
<input type="radio" name="bewertung" value="6" required />6
</label>
</fieldset>
I need a preselected radio button, selected drop-down list entry and also the comment field should be filled (that doesn't work yet).
How is it possible, to fill these with values from php variables?
<textarea> doesn't support the value attribute, echo your $comment between the <textarea></textarea> tags.
Use conditional logic to check off a radio button and select box options:
<option value="bar" name="foobar" <?php echo ($foobar == "bar" ? "selected=\"selected\"" : ""); ?>>bar</option>
<input type="radio" value="foo" name="foobar" <?php echo ($foobar == "foo" ? "checked=\"checked\"" : ""); ?> /> foo
UPDATE
Applied to your original code:
<?php
$interesse = "PHP";
$bewertung = 4;
?>
<textarea name="kommentar" cols="25" rows="7" required><?php echo "$comment";?></textarea>
<select name="interesse" required>
<option disabled>Bitte auswählen</option>
<option <?php echo ($interesse == "Java" ? "selected=\"selected\"" : ""); ?>>Java</option>
<option <?php echo ($interesse == "PHP" ? "selected=\"selected\"" : ""); ?>>PHP</option>
<option <?php echo ($interesse == "C++" ? "selected=\"selected\"" : ""); ?>>C++</option>
<option <?php echo ($interesse == "Ruby" ? "selected=\"selected\"" : ""); ?>>Ruby</option>
<option <?php echo ($interesse == "SQL" ? "selected=\"selected\"" : ""); ?>>SQL</option>
<option <?php echo ($interesse == "PLSQL" ? "selected=\"selected\"" : ""); ?>>PLSQL</option>
</select>
<fieldset>
<label for="bewertung">
<input type="radio" name="bewertung" value="1" required <?php echo ($bewertung == 1 ? "checked=\"checked\"" : ""); ?> />1
<input type="radio" name="bewertung" value="2" required <?php echo ($bewertung == 2 ? "checked=\"checked\"" : ""); ?> />2
<input type="radio" name="bewertung" value="3" required <?php echo ($bewertung == 3 ? "checked=\"checked\"" : ""); ?> />3
<input type="radio" name="bewertung" value="4" required <?php echo ($bewertung == 4 ? "checked=\"checked\"" : ""); ?> />4
<input type="radio" name="bewertung" value="5" required <?php echo ($bewertung == 5 ? "checked=\"checked\"" : ""); ?> />5
<input type="radio" name="bewertung" value="6" required <?php echo ($bewertung == 6 ? "checked=\"checked\"" : ""); ?> />6
</label>
</fieldset>
This would have the "PHP" option selected and the 4th radio button checked.
Basically, all you have to do is echo in certain content.
For textarea, we want to echo in the comment between the textarea open/close tags.
<textarea name="kommentar" cols="25" rows="7" required> <?php echo "$comment";?> </textarea>
For the radio button, you use the word "checked" (or checked="checked") to declare a checked option. You could check specific things as needed, and echo in the word checked where it should be.
<input type="radio" name="bewertung" value="1" required <?php echo "checked"; ?> />
For the select element, you use the word "selected" (or selected="selected") to declare a selected option. You could check specific things as needed, and echo in the word selected where it should be.
<option <?php echo "selected"; ?> >Java</option>

PHP: How can create an array from radio button and save to MySql

I have this code HTML :-
<label>Show Full Name</label>
<input type="radio" value="1" name="fullname" >
<input type="radio" value="0" name="fullname" >
<label>Show Job</label>
<input type="radio" value="1" name="job" >
<input type="radio" value="0" name="job" >
<label>Show Fav</label>
<input type="radio" value="1" name="fav" >
<input type="radio" value="0" name="fav" >
Now I need to save this value to database an array like this format :-
$a = array(
'fullname' => '1',
'job' => '0',
'fav' => '1'
);
And when save it, How can i read it and Check it here :-
<label>Show Full Name</label>
<input type="radio" value="1" name="fullname" <? if ($fullname == '1') {?> checked="checked" <? } ?> >
<input type="radio" value="0" name="fullname" <? if ($fullname == '0') {?> checked="checked" <? } ?> >
<label>Show Job</label>
<input type="radio" value="1" name="job" <? if ($job== '1') {?> checked="checked" <? } ?> >
<input type="radio" value="0" name="job" <? if ($job== '0') {?> checked="checked" <? } ?> >
<label>Show Fav</label>
<input type="radio" value="1" name="fav" <? if ($fav== '1') {?> checked="checked" <? } ?> >
<input type="radio" value="0" name="fav" <? if ($fav== '0') {?> checked="checked" <? } ?> >
==================
Update:-
Now we cansave array , but when retrive data i canot get it
<?php
$sql="SELECT req from formsreq where id = 1";
$result=mysql_query($sql);
while($row=mysql_fetch_array($result)){
$data= json_decode($result['req']);
$fullname = $data['fullname'];
$bridthdate = $data['bridthdate'];
$wherebridth = $data['wherebridth'];
$nationalty = $data['nationalty'];
$whereplace = $data['whereplace'];
$job = $data['job'];
$degree = $data['degree'];
$special = $data['special'];
$enjaz = $data['enjaz'];
$morashah = $data['morashah'];
$jeha = $data['jeha'];
$maswagha = $data['maswagha'];
$cv = $data['cv'];
$work = $data['work'];
}
?>
<label>Show Full Name</label>
<input type="radio" value="1" name="fullname" <? if ($fullname == '1') {?> checked="checked" <? } ?> >
<input type="radio" value="0" name="fullname" <? if ($fullname == '0') {?> checked="checked" <? } ?> >
Where is error !
First of all, your html code will be in a form. After the submit, the action of the form (let's call it test.php), will handle the SQL operations.
test.php :
<?php
$fullname = $_POST['fullname'];
$job = $_POST['job'];
$fav = $_POST['fav'];
$a = array(
'fullname' => $fullname,
'job' => $job,
'fav' => $fav
);
$json = json_encode($a);
mysqli_query($conn,"INSERT INTO tbl (data) VALUES ('".$json."') ");
?>
As you can see, the data has been json encoded and inserted into the table.
When you want to retrieve and analyze the data, you can do this:
<?php
$sql="SELECT data from tbl where id = ".$id;
$result=mysqli_query($con,$sql)
while($row=mysqli_fetch_array($result,MYSQLI_ASSOC)){
$data= json_decode($result['data']);
$fullname = $data['fullname'];
$job = $data['job'];
$fav = $data['fav'];
}
?>
<label>Show Full Name</label>
<input type="radio" value="1" name="fullname" <? if ($fullname == '1') {?> checked="checked" <? } ?> >
<input type="radio" value="0" name="fullname" <? if ($fullname == '0') {?> checked="checked" <? } ?> >
<label>Show Job</label>
<input type="radio" value="1" name="job" <? if ($job== '1') {?> checked="checked" <? } ?> >
<input type="radio" value="0" name="job" <? if ($job== '0') {?> checked="checked" <? } ?> >
<label>Show Fav</label>
<input type="radio" value="1" name="fav" <? if ($fav== '1') {?> checked="checked" <? } ?> >
<input type="radio" value="0" name="fav" <? if ($fav== '0') {?> checked="checked" <? } ?> >

How to retrive and update radio button's data from mysql database?

CODE am trying:
<input type="radio" name="type" value="Male" <?php if ($type == 'Male') echo 'checked="checked"'; ?>"> Male
OR
<input type="radio" name="gender" value="<?php echo $gender ?>"/>Female
This one is right ??
i refer 2 links in SO but m not getting properly with radio button ....
Suggestions always Welcome ...
The first one is correct:
<input type="radio" name="type" value="Male" <?php if ($type == 'Male') echo 'checked="checked"'; ?>"> Male

Categories