I use the following code to select all value from a DB table and put it in dropdown. Here is the code :
<select name="dis_name">
<?php
$qu="Select DISTINCT name from root";
$res=mysqli_query($con,$qu);
while($r=mysqli_fetch_row($res))
{
echo "<option value='$r[0]'> $r[0] </option>";
}
?>
Based on previous code a value is selected and submitted. I want to show previously selected value in dropdown on another page so i use following code:
<select name="dis_name">
<option value="RAM"<?php echo $r20 == "RAM" ? " selected" : ""; ?>>RAM</option>
<option value="ROHAN"<?php echo $r20 == "ROHAN" ? " selected" : ""; ?>>ROHAN</option>
</select>
This code work fine till no new value added in DB table. But whenever i add new value i have to update edit code manually. Is there any way, so that dropdown previous selection updated automatically in edit page.
Add the condition in your loop, eg:
<?php
while($r=mysqli_fetch_row($res))
{
echo "<option value='$r[0]'";
if ($r[0] == $r20) echo " selected";
echo "> $r[0] </option>";
}
?>
If I get what you want (assuming that $r20 is the previously selected value:
<?php
$qu="Select DISTINCT name from root";
$res=mysqli_query($con,$qu);
while($r=mysqli_fetch_row($res))
{
echo "<option value='$r[0]'";
if($r[0]==$r20){echo 'selected="selected"';}
echo "> $r[0] </option>";
}
?>
this way the select will display as selected the option that match the $r20 value dinamically.
BTW, you should use id in your code. This way you could also have more than one user with the same name (but different ID). With DISTINCT, as you are doing, if you have more than one user with the same name you will get only one.
Related
I have below code which runs query correctly but does not keep selected option. I could get similar answers but not worked with this code. Here select options are in combination of user given and rest are from mysql column.
I am new to php, so any help will be appreciated.
<select name="tester">
<option value=""> -----------ALL----------- </option>
<?php
$dd_res=mysqli_query($conn, "Select DISTINCT tester from workflow1");
while($r=mysqli_fetch_row($dd_res))
{
echo "<option value='$r[0]'> $r[0] </option>";
}
?>
</select>
<input type="submit" name="find" value="find"/>
You can use the $_POST superglobal array to check the value of submitted form fields. In this case $_POST['tester'] contains the value of the select field after submitting the form.
echo "<option value='$r[0]'".($_POST['tester']==$r[0]?' selected':'')."> $r[0] </option>";
I am thinking you are asking to select the value passed into the form? If I am correct in that, you would do something like:
$selVal = $_REQUEST['tester'];
$val = $r[0];
echo '<option value="'.$val.'"';
if ($val == $selVal) {
echo ' selected="selected"';
}
echo '>'.$val.'</option>';
The $_REQUEST gets any POST, GET or COOKIE value and should be validated to make sure no hack attempts or anything. Once you have the value, you just add the selected attribute if it matches the value in the list.
I can't seem to get this right. I am trying to make a select box that gets it values from the database sticky please see my code below :
<select name="cob_bank">
<?php while($row = mysqli_fetch_array($query,MYSQLI_ASSOC)){
if($row['Id']==$myId) {
echo '<option value="'.$row['Id'].'"selected="selected">'.$row['Name'].'</option>';
} else {
echo '<option value="'.$row['Id'].'">'.$row['Name'].'</option>';
}
}?>
</select>
after the select box is filled in and the submit button is pressed where I save my Post variable
$myId = $_POST['cob_name'];
The above code does not work I please assist
the no space could be causing the problem as a space before selected
if($row['Id']==$myId) {
echo '<option value="'.$row['Id'].'" selected="selected">'.$row['Name'].'</option>';
}
The select name should be set as:
<select name='cob_name'>
So this should be my last question for this entire project. I've made the employee timecard pages. Works great. I'm trying to make them a review/change page per timecard. Basically the initial review page pulls up the timecards they've submitted. They select Review. It carries the ID number to a review page, and I call upon the ID number to display all the info from that timecard into the new timecard html/php form. (Basically its the exact same form they used to submit, however I've echo'd the values as the option already)
When I do update though it's not only carrying over the echo'd value. It's only Updating any changes they make, and in fact deleting everything else. Below is a snippit of just one drop-down list (all over non drop-down's update fine. This is just for drop down lists where the data is contained in the DB.
<select name="starttime" id="input_6" style="width:150px">
<option value="" selected><?php echo $stime ?></option>
<?php
$result = mysql_query("SELECT time FROM selTime ORDER BY id");
while($row = mysql_fetch_array($result)) {
echo "<option value=\"" . $row['time'] . "\">" . $row['time'] . " </option>";
}
?>
</select>
So what happens with this code. Is when they open the review page They would see their start time as they submitted it. If they leave it alone, and not change anything and push submit after changing something else. The starttime actually UPDATES as blank. If they change the starttime to a value it DOES submit the change. Is there something in this code that I can change that will submit the echo'd value, instead of just displaying it?
I believe the issue is with this line of code:
<option value="" selected><?php echo $stime ?></option>
Although you set it as "selected" the selected element has the value of "" (empty). Try changing it to:
<option value="<?php echo $stime; ?>" selected="selected"><?php echo $stime; ?></option>
If they dont change this select list item, and submit the page directly, the selected item is the first one with the value="", so this will update in the database with blank ( null).
You need to give your fist item a value of date time now. This code here:
<option value="DATETINMENOW" selected><?php echo $stime ?></option>
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
PHP - PRE-select drop down option
I have a situation in my php project where a user can edit his account.
In editing, there is a city field which is a drop down list of cities.
I want to already select one city which is in profile before editing(the one which user enter at the time of registration).
Also he is able to change his city by choosing fron drop down list.
Html code:
<div class="search_bar1_txt">State:</div>
<div class="search_bar1">
<select class="styled" name="state_trainer">
<option>-select-</option>
<option>washington</option>
<option>perth</option>
<option>delhi</option>
<option>london</option>
</select></div>
</div>
on editing I am using this code to fetch current data(city) of user:
<?php
if(isset($_GET['userid']))
{
$sql = "select city from `wp_pelleresuser` where userId =".$_GET['userid'];
$result = mysql_query($sql);
$value = mysql_fetch_assoc($result);
}?>
please tell me how can I get already selected one option which is fetch from database.
And also it is changeable.
<div class="search_bar1_txt">State:</div>
<div class="search_bar1">
<select class="styled" name="state_trainer">
<option <?if($value['city']=='-select-') echo "selected";?>>-select-</option>
<option <?if($value['city']=='washington') echo "selected";?>>washington</option>
<option <?if($value['city']=='perth') echo "selected";?>>perth</option>
<option <?if($value['city']=='delhi') echo "selected";?>>delhi</option>
<option <?if($value['city']=='london') echo "selected";?>>london</option>
</select>
</div>
</div>
You can do it in a number of ways, the simplest (and probably least elegant) is to do something like this:
<select class="styled" name="state_trainer">
<?php
$myCity='london'; // assumed to be data from database...
echo '<option'.($myCity=='-select-') ? ' selected ' : ' ' .'-select-</option>';
echo '<option'.($myCity=='washington') ? ' selected ' : ' ' .'washington</option>';
?>
This is of course horrid.
I would rather suggest that you check the data as you are pulling your information out of the database and putting your initial dropdown list together.
If you are making an array of data to create the drop down list (for example) check it right there and then. If the city matches what you want, do it inside your loop right off the bat.
$usersCity="london";
$myCityList=array();
while( ... ) // Database loop that is pulling the data from the database.
{
$selected='';
if($userCity==$row['city'])
{
$selected=' selected ';
}
$myCityList[]='<option'.$selected.'>'.$row['city'].'</option>';
}
Then to display the drop down list, you can simply do this:
$cityCount=count($myCityList);
for($i=0;$i<$cityCount;$i++)
{
echo $myCityList[$i].'\n';
}
The users city will already be selected.
I'm not sure if it is clear way but you can put code inside every option cell like this:
<option <?php if($value['city'] == "washington") echo "selected=selected"; ?> >washington</option>
This way you can get what u want.
And better use mysqli function for database interactions.
<option
<?php if ($value['city'] == delhi)
{
echo "selected = true";
} ?>
>delhi
</option>
Just het the selected value form db like
$choosen = $some value form db
then
$options = array(1 => 'data1', 2 => 'data2', 3 => 'data3');
foreach ($options as $key => $value)
{
echo '' . $value . '';
}
to get your selection box in php.I thnk you understand,it works for me
I generated a drop down menu with the code below. How can I set the field to the GET variable after submit is clicked?
<select>
<?php
$sql="SELECT c FROM problems WHERE b='$id'";
$result=mysql_query($sql);
$options="";
while ($row=mysql_fetch_array($result)) {
$id=$row["c"];
$c=$row["c"];
$options.="<option value=\"$id\">".$c;
}
?>
<option value=''>C <?=$options?> </option>
</select>
<select>
<?php
$sql="SELECT c FROM problems WHERE b='$id'";
$result=mysql_query($sql);
$options="";
while ($row=mysql_fetch_array($result)) {
$id=$row["c"];
$c=$row["c"];
if($_GET['var'] == $id)
echo '<option selected="selected" value=\"$id\">' . $c . '</option>';
else
echo '<option value=\"$id\">' . $c . '</option>';
}
?>
</select>
Basic idea is compare value GET data with database data and using if else condition add selected="selected" if condition matched. I am directly printing string as they will not be getting use later on.
I'm a bit confused about what you want to know. To have an option selected by default, use the selected="selected" attribute on that option.
If you want to know how to get the submitted value in PHP, you need to assign the name attribute to the <select> tag. In general, however, you've got the idea right.