Prevent double options in dropdown list - php

in my dropdown list I insert a value from a mySQL query. This Value is my selected Option.
The problem is, that he duplicate my selected Value (it is logical because I create "6 Options" but the user only should have "5 Options").
How can I prevent this?
This is my Code:
echo "<select id='qty' name='qty' onchange='this.form.submit()'>";
echo "<option value='".$row['a_qty']."' selected>".$row['a_qty']."</option> ";
echo "<option value='1'>1</option>";
echo "<option value='2'>2</option>";
echo "<option value='3'>3</option>";
echo "<option value='4'>4</option>";
echo "<option value='5'>5</option>";
echo "</select>";
echo "<script>
And my Output:

Try as below :
echo "<select id='qty' name='qty' onchange='this.form.submit()'>";
for($i=1;$i<=5;$i++)
{
$selected = '';
if($i==$row['a_qty'])
$selected = 'selected="selected"';
echo "<option value='$i' $selected>$i</option>";
}
echo "</select>";

Related

Giving different names to a html object in a loop in PHP

How would I be able to give the different things different names (as I am creating them in a loop) so that I can use the data? Could I modify name=\"type\" to maybe include $x within the name so that it is different each time?
For($x=0; $x<=$noQuestions-1; $x++){
echo "<table>";
echo "<tr>"."Question ".($x+1).": ".$question[$x]."</tr>";
echo "<form method=\"post\">";
echo "<Select class=\"form-control\" type=\"text\" name=\"type\" required>";
echo "<option value=\"1\">".$optionData[$x][0]."</option>";
echo "<option value=\"2\">".$optionData[$x][1]."</option>";
echo "<option value=\"3\">".$optionData[$x][2]."</option>";
echo "<option value=\"4\">".$optionData[$x][3]."</option>";
echo "</select>";
echo "</form>";
echo "</table>";
}
You can simply concatenate variable $x in name like this:
echo "<Select class=\"form-control\" type=\"text\" name=\"type".$x."\" required>";
EDIT:
$type = "dropdown";
for($x=0; $x<=$noQuestions-1; $x++)
{
echo "<table>";
echo "<tr>"."Question ".($x+1).": ".$question[$x]."</tr>";
echo "<form method=\"post\">";
echo "<Select class=\"form-control\" type=\"text\" name=\"".$type.$x."\" required>";
echo "<option value=\"1\">".$optionData[$x][0]."</option>";
echo "<option value=\"2\">".$optionData[$x][1]."</option>";
echo "<option value=\"3\">".$optionData[$x][2]."</option>";
echo "<option value=\"4\">".$optionData[$x][3]."</option>";
echo "</select>"; echo "</form>";
echo "</table>";
}
One simple solution would be to add $x in the name like this for example:
echo "< select class=\"form-control\" type=\"text\" name=\"type-$x\" required>";

HTML generated through PHP making dropdown option stay selected after submission

Here I have html that has been generated through PHP and my issue is that I want the dropdown's option to stay selected after form submission. What am I missing?
$core_areas = array("Category 1", "Category 2", "Category 3");
if (isset($_POST["core"]) && $_POST["core"] != "")
{
echo "<select name=\"core\" id=\"core\">
<option value=\"\"> -Any- </option>";
foreach($core_areas as $value)
{
echo "<option value=\"$value\">$value</option>";
}
echo "</select>";
}
else
{
echo "<select name=\"core\" id=\"core\">
<option value=\"\"> -Any- </option>";
foreach($core_areas as $value)
{
echo "<option value=\"$value\">$value</option>";
}
echo "</select>";
}
Add the selected attribute to the selected option element.
echo "<option value=\"$value\" " .
($value == $_POST["core"] ? "selected" : "") .
">$value</option>";

Set select option using php when reloading the same page

I have a php select populated and given a matching value by using php to loop through the results of a sql query.
$result = mysqli_query($con, "select * from course")
echo "<form action='' method='post'>";
echo "<select name='CourseSelect'>";
echo "<option value='0'> - Select Course - </option>";
while($row = mysqli_fetch_array($result))
{
echo "<option value='" . $row['Title'] ." '>" . $row['Title'] . "</option>";
}
echo "</select>";
echo "<input name='SubmitCourse' type='submit'>";
echo "</form>";
That gives me a drop down list populated with all course titles,
upon submission I can access the selected value using $_POST['CourseSelect'];
However the drop down (select) resets itself to the default value when the page reloads.
How can I keep that option selected using php?
I know that I can use the selected keyword inside of an a select option to make that option the default selected option.
for example the second option would be selected when loading the page:
<select>
<option>One</option>
<option selected>Two</option>
<option>Three</option>
</select>
You can make it simple as
while($row = mysqli_fetch_array($result))
{
$select = '';
if( isset($_POST['CourseSelect']) && $_POST['CourseSelect'] == $row['Title'] ) $select = 'SELECTED';
echo "<option value='".$row['Title']."' ".$select.">" . $row['Title'] . "</option>";
}
you can use like below..
$result = mysqli_query($con, "select * from course");
$selected = "";
echo "<form action='' method='post'>";
echo "<select name='CourseSelect'>";
echo "<option value='0'> - Select Course - </option>";
while($row = mysqli_fetch_array($result))
{
$selected = $row['Title'] == $_REQUEST['CourseSelect'] ? "Selected" : "";
echo "<option value='" . $row['Title'] ." ' $selected>" . $row['Title'] . "</option>";
}
echo "</select>";
echo "<input name='SubmitCourse' type='submit'>";
echo "</form>";
try this:
echo "<option value='0'> - Select Course - </option>";
while($row = mysqli_fetch_array($result))
{
$selected = $_POST['CourseSelect'] == $row['Title'] ? 'selected' : '';
echo "<option value='{$row['Title']}' {$selected}>{$row['Title']}</option>";
}
you can do like this
echo "<form action='' method='post'>";
echo "<select name='CourseSelect'>";
if( isset($_POST['CourseSelect']) && $_POST['CourseSelect'] != "0")
{
echo "<option >".$_POST['CourseSelect']."</option>";
}
else
{
echo "<option value='0'> - Select Course - </option>";
}
while($row = mysqli_fetch_array($result))
{
if( isset($_POST['CourseSelect']) && $_POST['CourseSelect'] != $row['Title'])
echo "<option value='" . $row['Title'] ." '>" . $row['Title'] . "</option>";
}
echo "</select>";
echo "<input name='SubmitCourse' type='submit'>";
echo "</form>";
PHP Function
# select box
/*
Example:
Parameter 1:
$options[1] = 'Course 1';
$options[2] = 'Course 2';
$options[3] = 'Course 3';
Parameter 2:
$selectedOption = 2; The dropdown need to be selected
*/
function buildOptions($options, $selectedOption)
{
foreach ($options as $value => $text)
{
if ($value == $selectedOption)
{
$Return .='<option value="'.$value.'" selected="selected">'.stripslashes($text).'</option>';
}
else
{
$Return .='<option value="'.$value.'">'.stripslashes($text).'</option>';
}
}
return $Return;
}
Function Call
$result = mysqli_query($con, "select * from course");
while ($row = mysqli_fetch_array($result))
{
$UniqueId = $row['Title'];
$Value = $row['Title'];
$optionsArray[$UniqueId] = $Value; // Store the values into an array
}
$CourseSelect = isset($_POST['CourseSelect']) ? $_POST['CourseSelect'] : '0';
echo "<form action='' method='post'>";
echo "<select name='CourseSelect'>";
echo "<option value='0'> - Select Course - </option>";
echo buildOptions($optionsArray, $CourseSelect);
echo "</select>";
echo "<input name='SubmitCourse' type='submit'>";
echo "</form>";
Notes
You can use buildOptions() for all of your projects to display select box. I have been using this for years.

php dropdown population with first option blank

can someone tell me is there a possibility to add a "blank option" once the dropdown is populated?
echo '<select name="dropdown" style="width:150px">';
while($rec=mysql_fetch_array($run1))
{
$value = $rec['route'];
echo "<option value=\"$value\">$value</option>";
}
echo '</select>';
Why can't you do it beforehand?
echo '<select name="dropdown" style="width:150px">';
echo '<option value=""></option>';
while($rec=mysql_fetch_array($run1))
{
$value = $rec['route'];
echo "<option value=\"$value\">$value</option>";
}
echo '</select>';
Or if you really need it after (don't know why), just add a selected option to the <option>
echo '<select name="dropdown" style="width:150px">';
while($rec=mysql_fetch_array($run1))
{
$value = $rec['route'];
echo "<option value=\"$value\">$value</option>";
}
echo '<option value="" selected="selected"></option>';
echo '</select>';
After your while block, just add an additional echo:
echo "<option value=\"$value\"></option>";
As already mentioned, it's generally best to do this before your while loop, so
the default is blank. That way you can catch when/if a user forgets to select an
option.
simple trick, add extra line after the while loop.
while($rec=mysql_fetch_array($run1))
{
$value = $rec['route'];
echo "<option value=\"$value\">$value</option>";
}
echo "<option value=\"$value\"></option>";

Selected Value for MySQL generated Drop Down List

I have a mysql generate dropdown list..need to add in a pre selected option such as "Please Choose Availability". Not too sure how to do this as the drop down content is generated from a database
<?php
$sql = "SELECT DISTINCT availability FROM properties";
$result = mysql_query($sql);
echo "<select name='property'>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='". $row['availability']. "'>" . $row['availability'] ."</option>";
}
echo "</select>";
?>
if you want static first value for dropdown then you can do it like this.
echo "<select name='property'><option value=''>Please Choose Availability </option>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='". $row['availability']. "'>" . $row['availability'] ."</option>";
}
echo "</select>";
echo "<select name='property'><option value=''>Please Choose Availability</option>";

Categories