My box is populating from my database but I would like to show the drop down box blank until it is clicked to select. Not quite sure how to do that. Thanks in advance
<select type="text" name='id' id="inputItem" placeholder="Item #1" class="form-control">
<?php
require ('dbconnect.php');
$result = $con->query("select id, item from items");
while ($row = $result->fetch_assoc()) {
unset($id, $name);
$id = $row['id'];
$name = $row['item'];
echo '<option value="'.$id.'">'.$name.'</option>';
}
echo "</select>";
mysqli_close($con);
?>
</div>
Add <option>Choose One...</option> immediately after your first line.
you could add blank option before while loop, as:
...
echo '<option value="">Select</option>';
while ($row = $result->fetch_assoc()) {
unset($id, $name);
$id = $row['id'];
$name = $row['item'];
echo '<option value="'.$id.'">'.$name.'</option>';
}
...
<select type="text" name='id' id="inputItem" placeholder="Item #1" class="form-control">
<option value=''>Select First</option>
Before your while loop:
echo '<option value="">--- Select ---</option>';
Related
The Below code we are using. Please check and update:
`
dashboard
</div>
<div class="form-group">
<label for="to_user">Turbine</label>
<select name="to_user" class="form-control">
<option value="pick">--Select All--</option>
<option> <?php
$query = "SELECT DISTINCT Turbine FROM [JMDWF].[dbo].[TENAVG]";
$sql = sqlsrv_query($link,$query);
$row = sqlsrv_num_rows($sql);
$menu = "";
echo "<select name='to_user'>";
while ($row = sqlsrv_fetch_array($sql)){
/* echo "<option value='". $row['Turbine'] ."'>" .$row['Turbine'] ."</option>" ;*/
$menu.="<option>".$row['Turbine']."</option>";
}
echo "$menu";
?>
</option>
</select>
</div>`
Still we getting output out of the drop down list. I need to show the values in drop down.
Your are printing the output from PHP into your <option></option> node of the html - it won't be visible.
Try this instead:
<div class="form-group">
<label for="to_user">Turbine</label>
<?php
$query = "SELECT DISTINCT Turbine FROM [JMDWF].[dbo].[TENAVG]";
$sql = sqlsrv_query($link,$query);
$row = sqlsrv_num_rows($sql);
$menu = "";
echo "<select name='to_user'>";
echo "<option value='pick'>--Select All--</option>";
while ($row = sqlsrv_fetch_array($sql)){
/* echo "<option value='". $row['Turbine'] ."'>" .$row['Turbine'] ."</option>" ;*/
$menu.="<option>".$row['Turbine']."</option>";
}
echo "</select>";
echo "$menu";
?>
</div>`
I am trying to edit my form. I want to get selected value selected in selection list.
I have created function to store values in database, and it works. Below is html code I use and function below to insert values in database.
// insert values in database
<label>Dobavljač</label>
<select class="form-control" name="dobavljac" required>
<?php dobavljac() ?>
</select>
function dobavljac(){
$sql=mysqli_query($link, "SELECT * FROM `partneri` WHERE `Dobavljac`='1'
order by `PartnerId` asc ");
echo '<option value="">Izaberi dobavljača</option>';
while($record = mysqli_fetch_array($sql)) {
echo '<option value= "' .$record['PartnerId']. '">' . $record['PartnerNaziv'] . ' </option>';
}
}
// edit values
First I retrieve information from database
$id=$_GET['id'];
$sql = "SELECT * FROM materijali where Id=$id ";
$q = $conn->query($sql);
$r = $q ->fetch();
if ($r) {
$dobavljac=$r['Dobavljac'];
I want to get selected value in box
<label>Dobavljač</label>
<select class="form-control" name="dobavljac" value="<?php echo $dobavljac; ?>">
<?php dobavljac() ?>
</select>
Probably I am not doing it the right way, any advice would be appreciated
Try this
<label>Dobavljač</label>
<select class="form-control" name="dobavljac" value="<?php echo $dobavljac; selected?>">
<option value=<?php echo $dobavljac?> selected>
<?php dobavljac() ?>
</option>
</select>
Try this...
$id=$_GET['id'];
$sql = "SELECT * FROM materijali where Id=$id ";
$q = mysql_query($query);
echo "<select name="dobavljac" class="form-control">";
while (($row = mysql_fetch_row($q)) != null)
{
echo "<option value = '{$row['Dobavljac']}'>";
echo $row['Dobavljac'];
echo "</option>";
}
echo "</select>";
My SELECT looks like the following:
<?php
$query = "SELECT * FROM Rec_SW2_Rel AS a JOIN SW2 b ON a.Sbj_ID = b.IDsbj GROUP BY a.Sbj_ID ORDER BY b.Descriptor";
$result = mysql_query($query);
?>
<select name="country" onchange="getState(this.value)">
<?php
while ($line = mysql_fetch_array($result, MYSQL_ASSOC))
{
?>
<option value="<?php echo $line['Sbj_ID']; ?>">
<?php echo $line['Descriptor']; ?>
</option>
<?php
}
mysql_close();
?>
</select>
Querying the DB and setting up the drop-down works. The problem is that the value listed first isn't automatically selected. If a user wants to use it, for further navigation, they must first select a different one and then select the first once again.
I couldn't alter the values in the DB. If I insert selected='selected' it returns the last value of the result set, but always without being selected.
You maybe want this? First selected option when the form is loaded is blank.
<select name="country" onchange="getState(this.value)">
<option value=""></option>
<?php
while ($line = mysql_fetch_array($result, MYSQL_ASSOC))
{
or select the selected data from the database? selected column with selected value.
<select name="country" onchange="getState(this.value)">
<?php
$first = true;
while ($line = mysql_fetch_array($result, MYSQL_ASSOC))
{
?>
<option value="<?php echo $line['Sbj_ID']; ?>" <?php echo ($line['selected']=='selected') ? 'selected="selected"' : '' ; ?>>
you can test with respect to $line['Sbj_ID'] if this is = to the value you want by default
<?php
$query = "SELECT * FROM Rec_SW2_Rel AS a JOIN SW2 b ON a.Sbj_ID = b.IDsbj GROUP BY a.Sbj_ID ORDER BY b.Descriptor";
$result = mysql_query($query);
?>
<select name="country" onchange="getState(this.value)">
<?php
while ($line = mysql_fetch_array($result, MYSQL_ASSOC))
{
?>
<option value="<?php echo $line['Sbj_ID']; ?>" <?php if($line['Sbj_ID']==value_you_want_selected){?>selected<?php } ?>>
<?php echo $line['Descriptor']; ?>
</option>
<?php
$i++; }
mysql_close();
?>
Hi i have been working on a form wherein there's a dropdown menu and it's values are from the database. My problem is it doesnt show the value selected after submitting the form. what maybe the problem?
<select name="professional" />
<option value="">Choose one</option>
<?php
$result2 = mysql_query("SELECT * FROM professional");
while($row2 = mysql_fetch_array($result2))
{
$prc = $row2['name'];
$prof = $row2['prcno'] ."\t"."|\t". $row2['name'] ."\t"."|\t".$row2['profession'];
echo "<option value ='$prc'>$prof</option>";
}
?>
</select>
<select name="professional" disabled/>
<option value="">Choose one</option>
<?php
$result2 = mysql_query("SELECT * FROM professional");
$i=0;
while($row2 = mysql_fetch_array($result2))
{
$prc = $row2['name'];
$p1[$i] = $prc;
$prof = $row2['prcno'] ."\t"."|\t". $row2['name'] ."\t"."|\t".$row2['profession'];
$p2[$i] = $prof;
if($_POST['professional'] == $p1[$i])
{
echo "<option selected value ='$p1[$i]'>$p2[$i]</option>";
}
else
{
echo "<option value ='$p1[$i]'>$p2[$i]</option>";
}
}
?>
</select>
It seems to me, you're not incrementing $i, so you keep overwriting $p1[0] and $p2[0] in each iteration of the while-loop.
So add $i++ at the beginning or the end of your loop - or drop the whole use of these to arrays ($p1 and $p2) and use $prc and $prof just as you do in the first code-block - or do you need them for something?
Another thing, try removing the space between value and ='$p1[$i]' - but I'm not sure if that's a problem.
Try
selected="selected"
in stead of
selected
change these lines to
echo "<option selected value ='<?php echo $p1[$i]; ?>'><?php echo $p2[$i]; ?></option>";
and do not forget to increment your $i too
Hope it will help :)
How do I show the selected value of a dropdown list from my mysql database. The dropdown list is dependent to my Category dropdown list. These are the codes:
<?php $id = $_GET["id"];
if(!$pupcon){
die(mysql_error());
}
mysql_select_db($database_pupcon, $pupcon);
$getDropdown2 = mysql_query("select * from tblitemname where CategoryID = $id");
while($row = mysql_fetch_array($getDropdown2)){
echo "<option value=\"".$row["ItemID"]."\">".$row["Item_Name"]."</option>";
} ?>
Here are the codes for the first dropdown list (Category) which populates the Item Name dropdown.
<select name="Category" id="Category" class="field select large" onChange="loadXMLDoc(this.value);">
<?php do { ?>
<option value="<?php echo $row_Recordset1['CategoryID']?>"<?php if (!(strcmp($row_Recordset1['CategoryID'], $row_editRecordset['CategoryID']))) {echo "selected=\"selected\"";} ?>><?php echo $row_Recordset1['CategoryName']?></option>
<?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); $rows = mysql_num_rows($Recordset1); if($rows > 0) {
mysql_data_seek($Recordset1, 0);
$row_Recordset1 = mysql_fetch_assoc($Recordset1);}?>
</select>
While you're listing out the drop down options, you can check to see if the current ItemID matches the passed id value. If it matches, throw a selected="selected" in there. Try:
$selected = ($row['ItemID'] == $id);
echo "<option value=\"".$row["ItemID"]."\" ".($selected ? " selected=\"selected\"":"").">".$row["Item_Name"]."</option>";
EDIT
I tried to clean up the code some...not sure why you're using a do...while because $row_Recordset1 wouldn't be available on the first iteration.
<select name="Category" id="Category" class="field select large" onChange="loadXMLDoc(this.value);">
<?php
while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)) {
?>
<option value="<?php echo $row_Recordset1['CategoryID']; ?>"<?php if (!(strcmp($row_Recordset1['CategoryID'], $row_editRecordset['CategoryID']))) { echo " selected=\"selected\""; } ?>>
<?php echo $row_Recordset1['CategoryName']; ?>
</option>
<?php
}
?>
</select>
you can use this code inside while
$selected=$row["ItemID"]==$id ?'Selected':'';
echo "<option value=\"".$row["ItemID"]."\" {$selected} >".$row["Item_Name"]."</option>";;