php dropdown population with first option blank - php

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>";

Related

PHP onchange event of select box

I am new to PHP and this community
I was trying to use onchange function in php select
but it seems to be not working
I searched for solutions but basically are all with JS and the value goes to the URL, but my value is going to use in php for loop
don't want to get the parameter in URL
please help
echo "<select onchange='myFunction(this.value)'>";
echo "<option value=''>select your number</option>";
echo "<option value='5'>5</option>";
echo "<option value='10'>10</option>";
echo "<option value='15'>15</option>";
echo "</select>";
//////////////////////////////////////////////////////
function myFunction($numbers){
for($count=0;$count<$numbers;$count++)
echo $count;
}
by the way, inside for loop contains some MySQL so that's why i'm using PHP
function change_function(element){
document.location.href = element.value
}
<select class="form-contro-drop" name="val" onChange="change_function(this);" >
<option value="ur.php?val=1">1</option> <option value="ur.php?val=2">2</option></select>
Hope this helps!
I think you can coding like this
<?php
header('contact-type:text/javascript;charset=utf-8');
echo "<select onchange='myFunction(this.value)'>";
echo "<option value=''>select your number</option>";
echo "<option value='5'>5</option>";
echo "<option value='10'>10</option>";
echo "<option value='15'>15</option>";
echo "</select>";
?>
<script>
function myFunction($numbers){
for($count=0;$count<$numbers;$count++)
echo $count;
}
</script>

How to display input field if select value is null in php

How can I display that when select value is null I want to display input field to enter customer own choice. I am new to php so anyone can please suggest me how to do it? Here is my reference code.
<select class="form-control" name="outlet">
<option>Select Outlet</option>
<?php
$values = explode(",",$camp_outlet);
foreach(explode(",",$camp_outlet) as $value){
?>
<option value="<?php echo "$value";?>">
<?php echo "$value";?>
</option>
<?php
}
?>
<?php
$theData = explode(",",$camp_outlet);
if (count($theData) > 0 ){
echo "<select class=\"form-control\" name=\"outlet\">";
echo "<option>Select Outlet</option>";
foreach($theData as $value){ // loop through the data and add the options to the select input
echo "<option value=\"$value\">$value</option>";
}
echo "</select>";
} else {
echo "<input type=\"text\" class=\"form-control\" name=\"outlet\">";
}
?>

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>";

How to add php in html option tag?

I want to add values in option tag from sql database with the help of php. I have tried it but it is not showing anythingHere is my code which I have tried
<form action="addsubcategory.php" method="post">
<input type="text" name="sub_category" placeholder="Add sub category">
<?php
$res = mysqli_query($conn, "SELECT * FROM product_category");
echo '<select name="testSelect" id="testId">';
//Here is the problem
while ($record = mysqli_fetch_array($res)) {
echo '<option value="'.$record['category_name'].'">' . "</option";
}
echo '</select>';
?>
</form>
Where you have this:
echo '<option value="'.$record['category_name'].'">' . "</option";
You need this:
echo '<option value="'.$record['category_name'].'">' . "</option>";
Try with
echo '<option value="'.$record['category_name'].'">'.$record['category_name'].' "</option">';
You have only added value to option tag.
while ($record = mysqli_fetch_array($res)) {
echo '<option value="'.$record['category_name'].'">' . "</option";
}
instead of above code you should use:
while ($record = mysqli_fetch_array($res)) {
echo '<option value='.$record["category_name"].'>'. $record["category_name"] .'</option>';
}
Now above code will add value and text in option tag. Text to view and value to set or get value from db.

Prevent double options in dropdown list

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>";

Categories