I'm trying to get selected option in select,I need to make a select from my database and use id to mark which category is set.
echo"<select>";
while ($row = mysql_fetch_array($rezultat1))
{ if ($row['biljka_id']==$GET['b']) {
echo "<option value=".$row['biljka_id']." selected='selected' >".$row['naziv']."</option>";
} else { echo "<option value=".$row['biljka_id'].">".$row['naziv']."</option>";
}
echo"</select>";
Following is the short code.
You need not use if else statement as you need to justify only one variable depending upon condition.
You can do it with ternary operator.
<?php
echo "<select>";
while ($row = mysql_fetch_array($rezultat1)) {
$selected = ($row['biljka_id']==$GET['b']) ? 'selected="selected"' : '';
echo "<option value=".$row['biljka_id']. $selected ">".$row['naziv']."</option>";
}
echo"</select>";
?>
Rererence
You didn't properly close your while loop or the else part of your if, depending on how you look at it. Plus, why not use concatenation instead. So, you might re-write your code as follows:
$selectStr = '';
$selectStr .= "<select>";
while ($row = mysql_fetch_array($rezultat1))
{
if ($row['biljka_id']==$GET['b'])
{
$selectStr .= "<option value='".$row['biljka_id']."' selected='selected' >".$row['naziv']."</option>";
}
else
{
$selectStr .= "<option value='".$row['biljka_id']."'>". $row['naziv']. "</option>";
}
}
$selectStr .= "</select>";
echo $selectStr;
Related
So, what I'm trying to achieve that after the user hits submit, the dropdown retains the value that the user have chosen. This is my code for the dropdown, I understand that I have to selected = 'selected', but I couldn't figure out how it fits in my code.
if (mysqli_num_rows($result) > 0) { // output data of each row
while($row_branch = mysqli_fetch_array($result)) {
$menu_branch .= "<option value='".$row_branch["0"]."'>" .
$row_branch["0"]. "</option>";
}
}
echo $menu_branch;
You cannot just add selected = selected inside the loop because every option will be selected.
You might need the if statement to select the option of your choice.
For example:
if($row_branch["0"] == 'bar'){
$menu_branch .= "<option value='".$row_branch["0"]."' selected>" .
$row_branch["0"]. "</option>";
}else{
$menu_branch .= "<option value='".$row_branch["0"]."'>" .
$row_branch["0"]. "</option>";
}
When the user select an option, Where you submit this information?
If this information return from a database
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row_branch = mysqli_fetch_array($result)) {
$select = ($row_branch["0"]==$value_db)? "selected='selected'" : "";
$menu_branch .= "<option value='".$row_branch["0"]."' $select>" .
$row_branch["0"]. "</option>";
}
}
if i understand right:
// $yourvalue is previous dropdown value
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row_branch = mysqli_fetch_array($result)) {
if ($row_branch["0"] == $yourvalue)
$selected = "selected";
else
$selected = "";
$menu_branch .= "<option value='".$row_branch["0"]."' $selected>" .
$row_branch["0"]. "</option>";
}
}
echo $menu_branch;
This script does not display the DB value in a drop down on the edit form.
<?php
echo "<select name='assign' value=''><option>Select name</option>";
while ($r = mysql_fetch_array($result)) {
$value = $r['name'];
echo "<option value=" . $r['emp_id'] . ">" . $r['name'] . " if ($name=='$value') echo 'selected = 'selected''></option>";
}
echo "</select>";
It does not show any error. How it can write in a correct way.
You can try this :
$echoSting = '<select name="assign"><option value="">Select name</option>'.PHP_EOL;
while($r = mysql_fetch_array($result)) {
$value=$r['name'];
$echoSting .= '<option value="'.$r['emp_id'].'" '.($name==$value ? 'selected' : '').'>'.$r['name'].'</option>'.PHP_EOL;
}
$echoSting .= '</select>'.PHP_EOL;
echo $echoSting;
a side note, try looking into PDO for your database stuff : http://php.net/manual/en/book.pdo.php
Try this:
echo "<select name='assign' value=''><option>Select name</option>";
while($r = mysql_fetch_array($result)) {
$value=$r['name'];
echo "<option value='.$r['emp_id'].'>'.$r['name'].' "; if ($name=='$value') echo "selected = 'selected'";echo">$value</option>";
}
echo "</select>";
The following worked on a local environment. Now that it's pushed
live everything but these seems to work. Displays completely empty select boxes now no checks or blank labels just empty space in the "select options" drop down.
select display
<?php
$selected = array();
$selected = explode(",",$fill['markets']);
$condb = mysql_query("SELECT * FROM `countries`");
$count = mysql_num_rows($condb);
$countries = array();
$str;
while ($countries = mysql_fetch_array($condb))
{
$str = "option{$countries['id']}";
echo "<option value='{$str}' ";
if(in_array($str,$selected)) {
echo "selected>";
echo $countries['country'];
echo "</option>";
} else {
echo ">";
echo $countries['country'];
echo "</option>";
}
}
?>
You should use while loop instead. You can declare $i outside the loop if you need it, Try the following code:
$condb = mysql_query("SELECT * FROM `countries`");
$i = 0;
while($countries = mysql_fetch_array($condb)) {
$str = 'option' . $i;
echo "<option value='{$str}' ";
if(in_array($str,$selected)) {
echo "selected>";
echo $countries['country'];
echo "</option>";
} else {
echo ">";
echo $countries['country'];
echo "</option>";
}
$i++;
}
?>
</select>
Note:
mysql_* is deprecated as of php-5.5. So instead use mysqli_* or PDO.
Why shouldn't I use mysql_* functions in PHP?
<div class="select_list">
<div class="labels">Category:</div>
<?php
if(!isset($_POST['postbtn)){
echo "<script>$(document).ready(function(){
$('.category').val(0);
})
</script>";
}
echo "<select name='category' id='catg_list' class='list_catg'>
<option value='0'";
if(isset($_POST['category']) && $_POST['category']=='0'){
echo "selected";
}
echo">none</option>";
$query = mysql_query("SELECT id, name from table1");
while($query_fetch = mysql_fetch_assoc($query)){
echo "<option value='".$query_fetch['id']."'";
if(isset($_POST['category']) && $_POST['category']==$query_fetch['id'])
{
echo "selected";
}
echo ">".$query_fetch['name']."</option>";
}
echo "</select>";
?>
</div>
The problem with the above code is that the selected element stays selected after the submission is done. I need the select option to return back to 'none' when the form is submitted successfully. How can that be done?
You have an syntax error in your line:
if(!isset($_POST['postbtn)){
change it to:
if(!isset($_POST['postbtn'])){
I'm guessing some things you didn't mention:
You validate your script above the code you posted, and you want - if the script was validated successfully - that the "none" option is selected.
$selected_value = $validation ? $_POST['category'] : false;
Only if the Validation is correct the value will be set, else it will be set to false;
<?php
echo "<select name='category' id='catg_list' class='list_catg'>
<option value='0'";
//CHANGE
if($selected_value == 0){ //Or if($selected_value === FALSE ||$selected_value === 0)
echo "selected";
}
echo">none</option>";
$query = mysql_query("SELECT id, name from table1");
while($query_fetch = mysql_fetch_assoc($query)){
echo "<option value='".$query_fetch['id']."'";
//CHANGE
if($selected_value !== FALSE && $selected_value == $query_fetch['id'])
{
echo "selected";
}
echo ">".$query_fetch['name']."</option>";
}
echo "</select>";
?>
Simplify your code as following to do required task.
$category = isset($_POST['category']) ? $_POST['category'] : 0;
$array["0"]="none";
$query = mysql_query("SELECT id, name from table1");
while($query_fetch = mysql_fetch_assoc($query)){
$id = $query_fetch['id'];
$array[$id] = $query_fetch['name'];
}
echo "<select name='category' id='catg_list' class='list_catg'>";
foreach($array as $id=>$name) {
$selected = $category==$id ? "selected" : "";
echo "<option value='".$id."' ".$selected." >".$name."</option>";
}
echo "</select>";
I'm trying to show the selected value in the list if it's found matching. It's successfully populated but the selected value code does not run.
Code:
$StaffName = 'Jimmy Chan';
<select name="Staff" id="Staff"><?php
$data = array();
$data[0] = '';
echo "<option value='" . $data[0] . "'>" . $data[0] . "</option>";
$result= $DB->query('select No, FirstName, LastName from Staff');
foreach ($result as $data)
{
$SNo = $data['No'];
$SFN = $data['FirstName'];
$SLN = $data['LastName'];
$SName = $SFN.' '.$SLN;
if($SName == $StaffName)
{
echo "<option value='".$Sno."' selected = \"selected\">".$Sname."</option>\n";
}
else
{
echo "<option value='" .$SNo. "'>" . $SName . " </option>";
}
}
?>
</select>
The second else statement do run but not the if statement. I have already put the "selected" inside. Kindly advise.
Try this:
foreach ($result as $data)
{
$SNo = $data['No'];
$SFN = $data['FirstName'];
$SLN = $data['LastName'];
$SName = trim($SFN).' '.trim($SLN);
if(strtolower($SName) == strtolower($StaffName))
{
echo "<option value='".$Sno."' selected = 'selected'>".$Sname."</option>\n";
}
else
{
echo "<option value='" .$SNo. "'>".$SName."</option>\n";
}
}
Also, you have $Sname instead of $SName in first "if" statement. I am not sure if PHP can make a difference on it, but just keep in mind.
The same for $Sno and $SNo variables.
It seems the problem is with the value in variable $SName. Before comparison trim and convert both variables to uppercase or lowercase.
Lower case: http://php.net/manual/en/function.strtolower.php
Uppercase: http://php.net/manual/en/function.strtoupper.php
Trim: http://php.net/manual/en/function.trim.php
Also try,
echo "<option value='".$Sno."' selected="selected">".$Sname."</option>"; //remove \n
You are using
echo "<option value='".$Sno."' selected = \"selected\">".$Sname."</option>\n";
But Variable names are $SNo and $SName and you are using $Sno and $Sname. So Please replace line with line given below.
echo "<option value='".$SNo."' selected = \"selected\">".$SName."</option>\n";
I hope i will be work for you,
thanks