selecting value from drop down dropdown - php

hello please me out while editing the drop down box value how we show the value from the previous database Like v do over here
`<input name="starttime" size="8" value="<?php echo $res['starttime'];?>" /`>
so how can i do same for this code
<select name="employee_id" id="employee_id" >
<option value="">Select</option>
<?php
$task = new Task();
$task->connect();
echo $emp = $task->getEmployee();
$task->disconnect();
?>
</select>
function getEmployee()
{
$this->query=("select * from employee");
$rd=$this->executeQuery();
while($row = mysqli_fetch_assoc($rd))
{
$pno = $row['pno'];
$name = $row['name'];
echo "<option value='$pno'>$name</option>";
}
}
}
if i put over here in the value value then it will take one its value but it will no show in the drop down box . so in short it pick value from the array and show in dropdown box

This will select your wanted value on your dropdown list.
echo "<option value='$pno'" . ($pno == $selectedValue ? " selected='selected'" : "") . ">$name</option>";

Related

Dropdown and MySQl

I have a dropdown I dynamically populated from mysql
When I save the information it to mysql, it do not save the value of the dropdown list.
How do I set the selected value for the dynamic dropdown
Here is the code use to fill the dropdown.
How do I set the selected value on change in php
echo "<option value=\"" . utf8_encode($row['school_name']) . "\">" . utf8_encode($row['school_name']) . "</option>";
im have problems setting the correct format using this example
<option value="My School"<?php if ($row[school_name] == 'My School') echo ' selected="selected"'; ?>>My School</option>
Where My School should be the value populated from database
Ok
<select id="school_name" "name="school_name" style="width:200px;" name="school_name">
<option selected="">Select Province School</option>
</select>
i then fill the dropdownlist with following.
$sql="SELECT * FROM ppSD_schools where province_code ='".$_POST['c_id']."'";
$res = $conn->query($sql);
$num_rows = $res->num_rows;
if($num_rows > 0) {
echo "<option value=''>------- Select --------</option>";
while($row = $res->fetch_assoc())
{
?>
echo "<option value=\"" . utf8_encode($row['school_name']) . "\">" . utf8_encode($row['school_name']) . "</option>";
}
But when ever i select another school. it dont save the value selected to MySql
My guess is it dont mark the value as selected because it get filled dynamic
I now have to change the code so when i select it it must marked is as selected
I would use a repeated function to compare the value against the row value
<?php
function selected($a, $b) {
return ($a === $b) ? ' selected' : '';
}
?>
<option name="school" value="My School" <?= selected($row["school_name"], "My School"); ?>>My School</option>
<option name="school" value="Your School" <?= selected($row["school_name"], "Your School"); ?>>Your School</option>

How to fetch value from mysql to dropdown list?

i am newbie to PHP and MySql. I'm working on small project, some part of this project i have to deal with form and get value back to the field when i need to edit the text. Here's is what i meant
//dropdown list in Create
<select name="color">
while($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
echo '<option>'.$row['colors'].'</option>';
}
</select>
<input type="submit" name="submitcolor"/>
//dropdown list in Edit
<select name="color" value="???????">
</select>
i have no problem submit the value to MySql. The problem is, how to fetch the value back to dropdown list, so i don't have to click and search the value.
Thank you for your suggestion.
Try this
<//dropdown list in Create
<select name="color">
<?php
while($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
echo '<option value="'.$row['colors'].'">'.$row['colors'].'</option>';
}
?>
</select>
<input type="submit" name="submitcolor"/>
//dropdown list in Edit
<select name="color">
<?php
$color = 'some value you fetched from database';
while($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
$selected = '';
if($color == $row['colors']) {
$selected = 'selected';
}
echo '<option value="'.$row['colors'].'" '.$selected.'>'.$row['colors'].'</option>';
}
?>
</select>
You need to have a value in 'option' tag. And on editing, compare the exisiting 'color' value with the edit selectbox value
this is how you can fill a drop down with mysql results
<select name="name">
<?php
$count = count($name);
for ($i = 0; $i < $count; $i++){
"<option $type[$i]['name'] </option>';
}
?>
</select>

code for fetching value to select option

I have select option. this option has multiple value from database. I want to update something from database, this value i want to update is exist on the select option I have.
this is my option code
$id = $_GET['update'];
$query = mysql_query("SELECT * FROM transaction where id = '$id'") or die ("could not search");
$count = mysql_num_rows($query);
while ($rows = mysql_fetch_array($query)) {
$id = $rows['id'];
$tranid = $rows['tranid'];
$trandate = $rows['trandate'];
$patientid = $rows['patientid'];
$transactiontype = $rows['transactiontype'];
$trandescription = $rows['trandescription'];
$tranquantity = $rows['tranquantity'];
$tranunitprice = $rows['tranunitprice'];
$tranamount =$rows['tranamount'];
$gettrandescription = $rows['trandescription'];
}
}
if (isset($_POST['selectmedicine'])) {
$gettrandescription=$_POST['medicineid'];
}
if (isset($_POST['selectroomquantity'])) {
$tranquantity=$_POST['quantity'];
}
?>
<script type="text/javascript">
$('#collapseone').collapseone({
toggle: true
});
<option value="<?php echo $trandescription; ?>" <?php if($trandescription==$gettrandescription){ echo "selected";} ?> ><?php echo $gettrandescription; ?></option>
<option value="<?php echo $tranquantity; ?>" <?php if($tranquantity==$tranquantity){ echo "selected";} ?> ><?php echo $tranquantity; ?></option>
this has value results, but i cant fetch this value to my existing select option.
If you want to "make that variable an array" as aldrin27 said, append [] to the name attribute of the select tag. The selected value of the option with name selectroomquantity will be available in your script as $_POST["selectroomquantity"] (this is the varible).
<select multiple name="selectroomquantity[]">
<option value="...">...</option>
</select>
It should only be necessary if multiple options can be selected however.
Also, there seems to be a typo:
<?php if($tranquantity==$tranquantity)
That check will always return true. It should probably be:
<?php if($tranquantity==$gettranquantity)
hi all i just got the code on how to fecth the value to dropdown. actually i made a wrong word. pre-selected is the right one, sorry for that. here;s the working code.
<select name="selectmedicine" class="form-control col-sm-4" id="medicinename">
<option id="0" style="width:100px"></option>
<?php
$medicine = mysql_query("SELECT * FROM medicine");
while ($row = mysql_fetch_array($medicine)) {
echo '<option id="' . $row['medicinename'] . '"';
echo ' value="' . $row['medicineid'] . '"';
if($row['medicinename'] == $trandescription) {
echo ' selected="selected"';
}
echo '>';
echo $row['medicinename'];
echo '</option>';
}
?>
</select>
thanks everyone, whos trying to help me on this. actually this is my five revised question sorry for that. and finally i got the right one.

PHP : Get the value of Dropdown and pass it to another dropdown

I have a problem right now [PHP]. I have a dropdown and its loading my database for the first page, when I proceed to the next page it also have a dropdown where its also loading the my database and also I can get the value of my dropdown in the first page using an echo only.
This is the scenario:
I choose in the dropdown first page the "Letter A" and when I click the button it will proceed to the next page. The dropdown in the second page loaded the items in the database but instead of "-select-" is the first index in the dropdown I want is "Letter A" will be the first index.
This is my code in first page for drowdown:
<select name="id">
<option value="" >- select -</option>
<?php
include 'connect.php';
$q = mysql_query("select fldNetname from tblnetwork");
while ($row1 = mysql_fetch_array($q))
{
echo "<option value='".$row1[fldNetname]."'>".$row1[fldNetname]."</option>";
}
?>
</select>
and this is my code in second page for dropdown:
if ($get_ID != "")
{
echo "<br/>";
echo $get_ID;
//echo "show()";
}
else
{
echo "No Network Selected";
echo "<br/>";
//echo "hide()";
}
?>
<option value="">- select -</option>
<?php
include 'connect.php';
$q = mysql_query("select fldNetname from tblnetwork");
while ($row1 = mysql_fetch_array($q))
{
echo "<option value='".$row1[fldNetname]."'>".$row1[fldNetname]."</option>";
}
?>
</select>
Thanks in advance!
On your second page, you need to check whether the value is the same as the one you received from the first page:
echo "<option value='".$row1[fldNetname]."' " . (($row1[fldNetname] == $get_id)?"selected":"") . ">".$row1[fldNetname]."</option>";
if $get_ID, is actually your $_GET['id'] value, then just do...
while ($row1 = mysql_fetch_array($q))
{
echo "<option value='".$row1['fldNetname']."'";
if($row1['fldNetname']==$get_ID){echo "selected='selected'";}
echo ">".$row1['fldNetname']."</option>";
}
Or as one line...
while ($row1 = mysql_fetch_array($q))
{
echo "<option value='".$row1['fldNetname']."' " . (($row1['fldNetname'] == $get_ID)?"selected='selected'":"") . ">".$row1'[fldNetname']."</option>"
}
Have you tried?
changing (on the second page)
<option value="">- select -</option>
to
<option value="<?php echo $yourVar; ?>"><?php echo $yourVar; ?></option>
Then in the while loop, skip the value selected to prevent a duplicate choice.
Something like:
while ($row1 = mysql_fetch_array($q))
{
if($yourVar != $row1[fldNetname]){
echo "<option value='".$row1[fldNetname]."'>".$row1[fldNetname]."</option>";
}
}

Show selected value in dropdown after submit

hi guys i added dropdown field to form however after i submit the form if there is any error dropdown resets itself how can keep to value after validation thanks a lot for your any helps and idea
here is my code
<td><select id="country" name="country" style="width:150px;">
<option value="-1">Select</option>
<?php
$query = "SELECT country_id, name FROM countries ";
$result = mysql_query($query);
while($row = mysql_fetch_array($result))
{
echo "<option value=\"".$row['country_id']."\" >".$row['name']."</option>\n ";
}
?>
</select></td>
<td><?php echo $form->error("country"); ?></td>
Typically you would set the default option with attribute selected that is tied to the current selected value. So in this case, the option that equals the value of $_POST['country']:
while ($row = mysql_fetch_array($result))
{
if ($row['country_id'] == $_POST['country'])
$selected = "selected=\"selected\"";
else
$selected = "";
echo "<option value=\"".$row['country_id']."\" $selected>".$row['name']."</option>\n ";
}
Which would render as the following on the appropriate option:
<option value="123" selected="selected">456</option>

Categories