Reputation: 125
I am working in a form, but Im wondering if I can actually after the form is submitted I can re-populate a radio button and a selection option in my form? This is how my code looks like right now but I dont have any implementation for this...
<form method="post" action="forms-part3.php">
<table>
<tr>
<th colspan="2">FSOSS Registration</th>
</tr>
<tr>
<td><br /></td>
</tr>
<tr>
<td class="right text">Title:</td>
<td><input type="radio" name="sex" value="male">Mr.</td></tr>
<tr><td></td><td><input type="radio" name="sex" value="female">Mrs.</td></tr>
<tr><td></td><td><input type="radio" name="sex" value="female">Ms.</td>
</tr>
<tr>
And this is the drop down selection part...
<tr>
<td class="right text">T-shirt Size:</td>
<td>
<select name="selection"><?php
$menu = array("-Enter T-Shirt Size-", "Small", "Medium", "Large", "X-Large", "Do not want a t-shirt");
$count = count($menu);
for($i = 0; $i < $count; $i++)
{
?><option><?php if (isset($menu[$i])){ echo $menu[$i]; }?></option><?php
}
?></select></td>
</tr>
Sorry, I forgot to say that Im doing my form to reappear with all the fields re-populated with the user entered.. So, I got how to it for text fields but Im not sure how to do it for radio and select options.. So after the user inputs whatever his choice is, the form will re-populate the fields and also re-select the radio and options selected..
Upvotes: 0
Views: 1585
Reputation: 125
This how to re-populate the radio buttons:
<td class="right text">Title:</td>
<td><input type="radio" name="sex" value="Mr." <?php if (isset($_POST['sex']) && $_POST['sex'] == "Mr.") echo "checked"; ?>>Mr.</td></tr>
<tr><td></td><td><input type="radio" name="sex" value="Mrs." <?php if (isset($_POST['sex']) && $_POST['sex'] == "Mrs.") echo "checked"; ?>>Mrs.</td></tr>
<tr><td></td><td><input type="radio" name="sex" value="Ms." <?php if (isset($_POST['sex']) && $_POST['sex'] == "Ms.") echo "checked"; ?>>Ms.</td>
</tr>
This is how to re-populate the selection menu:
<select name="selection">
<option value="" <?php echo(isset($_POST['selection'])&&($_POST['selection']=='')?' selected="selected"':'');?>>-Enter T-Shirt Size-</option>
<option value="Small" <?php echo(isset($_POST['selection'])&&($_POST['selection']=='Small')?' selected="selected"':'');?>>Small</option>
<option value="Medium" <?php echo(isset($_POST['selection'])&&($_POST['selection']=='Medium')?' selected="selected"':'');?>>Medium</option>
<option value="Large" <?php echo(isset($_POST['selection'])&&($_POST['selection']=='Large')?' selected="selected"':'');?>>Large</option>
<option value="X-Large" <?php echo(isset($_POST['selection'])&&($_POST['selection']=='X-Large')?' selected="selected"':'');?>>X-Large</option>
<option value="Do not want a t-shirt" <?php echo(isset($_POST['selection'])&&($_POST['selection']=='Do not want a t-shirt')?' selected="selected"':'');?>>Do not want a t-shirt</option>
</select>
Upvotes: 1
Reputation: 188
I think you want to retreive the selected options from yuor MySQL database.
Your databse saves the options the user selected.
You will need MySQL installed on your PC and mysqli must be enabled inside php.ini.
Alternatively you can save data into sessions or POSTs but you will lose the user's information the moment he closes the browser.
First create a table for your user(s) using phpMyADmin or the SQL command prompt.
Then create a shirt table for T-shirt sizes.
For the current selected user you can use sessions. Here is some sample PHP code for your dropbox: - but not complete though.
$CNN = "John";
$DBConnect = mysqli_connect("localhost", "root", "myUltraPasswrd#", "myDBname");
<select name="Tdropdown" onload="this.size=70;" onchange='this.form.submit()'>
$queryS = "select size from shirts where UserNo = $CNN";
if ($result = mysqli_query($DBConnect, $queryS)) {
while ($row = mysqli_fetch_assoc($result2)) {
$item1b = $row["Size"];
echo "<option value='";
echo $item1b."'>";
echo $item2b;
echo "____".$CNN; //selected customer of current session
print "_".$item1b;
print "_".$item3b;
print " </option>";
}
$result2->free();
}
}
but the code very much depends on what you want to accomplish.
Correct Database design is crtical. Enjoy!
PS for sessions you might use:
session_start();
$CNN = @$_SESSION['UserNo'];
Upvotes: 0
Reputation: 955
You are submitting the form through PHP right? so you have something like $_POST['input']
. In your form you can simply repopulate with;
<?php
if($_POST['input'] === TRUE){
$input_checked = 'checked';
}
?>
<input type="checkbox" name="input" value="value" <?php echo $input_checked; ?>/>
Personally though, it's much easier if you use ajax to submit your form, because then the form does not reload if the form validation fails, and all the input values remain the same.
Upvotes: 1
Reputation: 276
If you want to just select any radio option or select option, just add a selected
for <select>
and checked
for <input type='radio'>
option inside the tag. It would look like this:
<input type='radio' checked />
<select><option selected></option></select>
Upvotes: 0