I am trying to create a selected value of the previous output.
(the user fills in a form , submits, returns to the form) And I want the selected value to be the value the user previously used.
The vraagNummer and answer are given in the url parameter and the dropdown menu of items available in the list are created in a for loop.
Now I got stuck on that part.. How do I create an option selected value if it is created in a for loop?
Usually I would just put in option value = $vraagNummer selected> <?php echo $vraagNummer ?></option but in this case that wouldn't work. I suppose?
Anyone knows how to fix this?
Same with the A/B/C/D. How do I put in the selected value of answer into the value previously selected while still keeping the others as an option.
With kind regards,
if(!empty($_GET['vraagnummer'])){
$vraagNummer = $_GET['vraagnummer'];
if(!empty($_GET['answer'])){
$answer = $_GET['answer'];
}
}
echo $vraagNummer;
echo $answer;
?>
<form id="start" method="post" action="index.php?test=true">
<select name="question">
<?php
for($i= 1; $i < $nRows+1 ; $i++){
?><option value="<?php echo $i ?>">Question <?php echo $i?></option>
<?php
}
?>
</select>
<select name="result">
<option value="A">Answer A</option>
<option value="B">Answer B</option>
<option value="C">Answer C</option>
<option value="D">Answer D</option>
<input type="submit" id="submit" value="Submit" />
</form>
You simply test the $_GET['question'] against the current $i value in the loop using a ternary if
<form id="start" method="post" action="index.php?test=true">
<select name="question">
<?php
for($i= 1; $i < $nRows+1 ; $i++){
$sel = (isset($_GET['question']) && $_GET['question'] == $i) ? 'selected="selected"' : '';
echo '<option ' . $sel . ' value="' . $i .">Question ' . $i . '</option>';
}
?>
</select>
Warning:
Also be careful with the use of empty() when the variables can contain a zero as zero is considered as empty by the empty() function
Actually I would do your other dropdown like this
<select name="result">
foreach (range('A', 'D') as $char) {
$sel = (isset($_GET['result']) && $char == $_GET['result']) ? 'selected="selected"' : '';
echo "<option $sel value='$char'>Answer $char</option>";
}
</select>
Related
I am trying to loop inside a select tag and output the quantity inorder to allow the user to select a quantity from the selection box , but the numbers are printed next to each other ,,how to solve that ? ,,and how to echo the selected item !?
<select name="quantity">
<div style="font-
size:13px;position:relative;left:10px;width:80px;height:20px;border-style:
hidden;background-color:rgba(0, 0, 0, 0.50);color:white;" > </div>
<option value="" disabled selected>Quantity </option>
<option value="quantity"><?php for($i=1;$i<=$row["item_quantity"];$i++)
{echo
$i."<br>";}?></option>
</select>
Instead of this:
<option value="quantity"><?php for($i=1;$i<=$row["item_quantity"];$i++)
{echo
$i."<br>";}?></option>
try this:
for($i=1;$i<=$row["item_quantity"];$i++)
{
// To make a option selected
$selected = '';
if( $i == 2 ) // 2 or any value
{ $selected = 'selected="selected"'; }
echo '<option value="'. $i .'" '.$selected.' >'. $i .'</option>'
}
Explanation: The issue is, your loop is inside the option tag, which is wrong. Put the option tag inside the loop.
Check your script you have add loop at wrong place
for($i=1;$i<=$row["item_quantity"];$i++)
{
echo '<option value="'. $i .'">'. $i .'</option>'
}
TRY THIS
<select name="quantity">
<option value="" selected>Quantity </option>
<?php for($i=1;$i<=$row["item_quantity"];$i++) { ?>
<option value="<?php echo $i ?>"><?php echo $i ?></option>
<?php } ?>
</select>
I have the following form:
<form action="#" method="POST" enctype="multipart/form-data">
from:
<select name="age_from" id="age_a" onchange="checkages_a()">
<option value=""></option>
<?php
for($i = 17; $i <= 50; ++$i) {
echo "\t", '<option value="', $i. '">', $i, '</option>', "\n";
}
?>
</select>
to:
<select name="age_to" id="age_b" onchange="checkages_b()">
<option value=""></option>
<?php
for($i = 18; $i <= 50; ++$i) {
echo "\t", '<option value="', $i, '">', $i, '</option>', "\n";
}
?>
</select>
<input type="radio" name="gender" value="male">Male</input> <br />
<input type="radio" name="gender" value="female">Female</input><br />
<input type="submit" class="btn btn-info"
name="submit_form" value="Click to start chat! " />
The way it works is that a user can select one of the two options from gender and age and search for a user. It is not required for both options to be completed to search.
However, if none of the options are selected, I want it to stay on the current page (`random_chat.php), and echo a message.
Here is my approach:
$refined_gender = isset($_POST['gender']) ? escape($_POST['gender']) : '';
$age_from = isset($_POST['age_from']) ? escape($_POST['age_from']) : '';
$age_to = isset($_POST['age_to']) ? escape($_POST['age_to']) : '';
if (isset($_POST['submit_form'])){
if (empty ($refined_gender) || empty($age_from) || empty ($age_to)) {
echo "<div class='no_user'><p> Please apply at least one of the filters above. </p> </div>";
} else {
// search user
}
Currently, when I have both search options empty, the message echos, which is good. But the message also echos when I am searching for a male user, or have age not empty, which shouldn't happen.
you need to change || to &&, (Some other changes are also stated as comment):-
<form method="POST" enctype="multipart/form-data"> <!-- remove action ='#' -->
from:
<select name="age_from" id="age_a" onchange="checkages_a()">
<option value=""></option>
<?php
for($i = 17; $i <= 50; ++$i) {
if(isset($_POST['age_from']) && $_POST['age_from'] == $i){ // to make selected value to be selected even after form submit
echo "\t", '<option value="', $i. '" selected ="selected">', $i, '</option>', "\n";
}else{
echo "\t", '<option value="', $i. '">', $i, '</option>', "\n";
}
}
?>
</select>
to:
<select name="age_to" id="age_b" onchange="checkages_b()">
<option value=""></option>
<?php
for($i = 18; $i <= 50; ++$i) {
if(isset($_POST['age_to']) && $_POST['age_to'] == $i){// to make selected value to be selected even after form submit
echo "\t", '<option value="', $i. '" selected ="selected">', $i, '</option>', "\n";
}else{
echo "\t", '<option value="', $i. '">', $i, '</option>', "\n";
}
}
?>
</select>
<input type="radio" name="gender" value="male">Male</input> <br />
<input type="radio" name="gender" value="female">Female</input><br />
<input type="submit" class="btn btn-info" name="submit_form" value="Click to start chat! " />
<form>
<?php
$refined_gender = isset($_POST['gender']) ? $_POST['gender'] : ''; // escape gives me undefined function error at my end so i removed it, but if it is working at your end thhen keep it up as it is.
$age_from = isset($_POST['age_from']) ? $_POST['age_from'] : '';
$age_to = isset($_POST['age_to']) ? $_POST['age_to'] : '';
if (empty ($refined_gender) && empty($age_from) && empty ($age_to)) { // check always with form posted value not submit button value and also cange || to &&
echo "<div class='no_user'><p> Please apply at least one of the filters above. </p> </div>";
} else {
// search user
}
?>
The if condition does not reflect what you want fix it like this
if (empty ($refined_gender) && empty($age_from) && empty ($age_to)) {
echo "<div class='no_user'><p> Please apply at least one of the filters above. </p> </div>";
} else { // search user }
i am newbie to PHP and MySql. I'm working on small project, some part of this project i have to deal with form and get value back to the field when i need to edit the text. Here's is what i meant
//dropdown list in Create
<select name="color">
while($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
echo '<option>'.$row['colors'].'</option>';
}
</select>
<input type="submit" name="submitcolor"/>
//dropdown list in Edit
<select name="color" value="???????">
</select>
i have no problem submit the value to MySql. The problem is, how to fetch the value back to dropdown list, so i don't have to click and search the value.
Thank you for your suggestion.
Try this
<//dropdown list in Create
<select name="color">
<?php
while($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
echo '<option value="'.$row['colors'].'">'.$row['colors'].'</option>';
}
?>
</select>
<input type="submit" name="submitcolor"/>
//dropdown list in Edit
<select name="color">
<?php
$color = 'some value you fetched from database';
while($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
$selected = '';
if($color == $row['colors']) {
$selected = 'selected';
}
echo '<option value="'.$row['colors'].'" '.$selected.'>'.$row['colors'].'</option>';
}
?>
</select>
You need to have a value in 'option' tag. And on editing, compare the exisiting 'color' value with the edit selectbox value
this is how you can fill a drop down with mysql results
<select name="name">
<?php
$count = count($name);
for ($i = 0; $i < $count; $i++){
"<option $type[$i]['name'] </option>';
}
?>
</select>
I have this form:
<form method="post">
<fieldset>
<select name="taskOption" required>
<option value="" disabled selected>Choose group</option>
<?php
while($hej < count($hejsan)) {
echo '<option value=' . $hej . '>' . $hejsan[$hej] . '</option>';
$hej++;
}
?>
</select>
<input type="submit" value="Find bookings">
</fieldset>
</form>
When I press the submit button the select/option will reset to the default, how can I change this and make it stay as the one I selected?
Try this
if (isset($_POST['taskOption'])) {
$Option = $_POST['taskOption'];
}
while($hej < count($hejsan)) {
if($hej==$_POST['taskOption']){
$selected = "selected=selected";
}else{
$selected = "";
}
echo '<option value="' . $hej . '" '.$selected .'>' . $hejsan[$hej] . '</option>';
$hej++;
}
You will need to look at the option you have selected and make the corresponding option selected. Since your form leads to the same page, you can just look at the contents of $_POST.
P.S.: also, you will find it useful to echo a newline after each option to make the output more readable.
Try this:
<?php
$taskOption = '';
if (isset($_POST['taskOption']) {
$taskOption = $_POST['taskOption'];
}
?>
<form method="post">
<fieldset>
<select name="taskOption" required>
<option value="" disabled selected>Choose group</option>
<?php
while($hej < count($hejsan)) {
if ($hey == $taskOption) {
echo "<option value={$hey} selected>{$hejsan[$hej]}</option>";
} else {
echo "<option value={$hey}>{$hejsan[$hej]}</option>";
}
$hej++;
}
?>
</select>
<input type="submit" value="Find bookings">
</fieldset>
</form>
At a if else statement to it. If the option value is equal to the select, then add the selected="selected"
This is an example code. could be that the operator is wrong, im always confused with the greater then and smaller then operators.
<?php
$option_value = 5;
for($i=0; $i > 10; $i++){
if($option_value == 5){
echo '<option value="'.$i.'" selected="selected">Item number'.$i.' is selected</option>';
} else {
echo '<option value="'.$i.'">Item number'.$i.'</option>';
}
}
?>
I'm having trouble with my php form that i query the info from the db which works fine now i just want when the user clicks submit to return the the selected value when the form reloads. I been trying to get this to work all night.
Example to be more clear.
--- Select ---
--- Apple ---
--- Banana ---
If they selected banana and clicked sumbit when the page reloads to have the field banana already selected. Here the code below. After 45 mins of fighting the "Parse error: syntax error, unexpected '.' I'm ready to pull my hair out so i hope you guys can lend a hand.
echo '<option value="'. $row['free'] .'" "'. $free==$row['free'] ? .'" selected=':'>$"'.$row['free'].'"</option>';
Thanks
echo '<option value="'. $row['free'] .'" "'. $free==$row['free'] ? /*.*/ '" selected=':'>$"'.$row['free'].'"</option>';
Just commented it out so you can see it.
let's assume you have a select element within a form
<form action="" method="post">
<select name ="fruits">
<option value="apple">Apple</option>
<option value="banana">Banana</option>
<option value="orange">Oranges</option>
<option value="mango">Mangoes</option>
</select>
<input type="submit" name="submit"/>
</form>
if i understand you correct you want to show the selected value back to the user when a user click on the submit button to do that place this in top of your php code.
<?php
//This will check if form is submitted then fetch the value from select element else assign null
$value = isset($_POST['submit']) ? $_POST['fruits'] : NULL;
?>
and change all the <option> to
<option value="apple" <?php echo ($value == 'apple') ? 'selected' : ''; ?>>Apple</option>
<option value="banana" <?php echo ($value == 'banana') ? 'selected' : ''; ?>>Banana</option>
<option value="orange" <?php echo ($value == 'orange') ? 'selected' : ''; ?>>Oranges</option>
<option value="mango" <?php echo ($value == 'mango') ? 'selected' : ''; ?>>Mangoes</option>
echo '<option value="' . $row['free'] . '"' . ($free == $row['free'] ? ' selected="selected"' : '') . '>' . $row['free'] . '</option>';
That should work :)
I'd advice you simplify your sintax and make it readable first of all.
$selected = '';
if($free==$row['free']){ $selected=' selected'; }
echo "<option value='{$row['free']}'{$selected}>{$row['free']}</option>";
Not sure how that string got into such a mess.... it's clearer (in my opionion) if you drop out of PHP mode if you're rendering HTML ... as follows:
?>
<option value="<?php echo $row['free']; ?>" <?php if($free == $row['free']) echo 'selected="selected"'; ?>><?php echo $row['free']; ?></option>
<?php