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>
Related
I have a sticky select form, which gets the data for the options from MySQL. The form should display "Select Type" if no form was submitted (this works). Then I tried to make it sticky. My idea was to compare the $_POST['type'] with the data from MySQL and if its the same it should echo selected.
The $_POST['type'] was working perfectly when I tried to echo it, also the options from the MySQL DB are working.
I feel like I'm close to the solution, but I'm missing something. Any ideas?
<select type="text" name="type" id="type" class="form-control input-lg">
<option value="" disabled <?php if(!isset($_POST['submit'])){ echo "selected";} ?> >Select type</option>
<?php
$result = mysql_query("select * from type");
while ($row = mysql_fetch_assoc($result))
{
$type[] = $row;
}
$count = count($type);
for ($i = 0; $i < $count; $i++)
{
$selected = $_POST['type'];
echo "<option";
if($selected === $type[$i] ){
echo "selected";
}
echo ">";
echo $type[$i]['type'];
echo '</option>';
}
?>
</select>
Change the line echo "selected"; to echo " selected ";
It will currently echo <optionselected> on selected option which is malformed.
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 :)
I am trying to pre select values on my edit page in multiple drop down so once user click edit, can see already inserted values. Values are saved as comma separated in MySQL like "1,2,3,4,5"
trying this solution but doesn't work :( , is there is any way that those vales will be pre selected? Please help
<select name="w_owning_branches[]" size="10" id="w_owning_branches" multiple="multiple" required>
<option value="" class="dropdown-option"> Select Owning Branch </option>
<?php do {
$value = $row_branches['branch_id'];
$name = $row_branches['name'];
$selected = '1,2,3,4,5,6';
echo "<option value='$value'".(($selected == '$value') ? " selected='selected'":"").">$name</option>";
} while ($row_branches = mysql_fetch_assoc($branches)); ?>
</select>
If I understand you correctly, then the selected values are stored in the comma separated string and the numbers are the values the where previously selected.
In that case the answer is simple:
<select name="w_owning_branches[]" size="10" id="w_owning_branches" multiple="multiple" required>
<option value="" class="dropdown-option"> Select Owning Branch </option>
<?php do {
$value = $row_branches['branch_id'];
$name = $row_branches['name'];
$selected = '1,2,3,4,5,6';
echo "<option value='$value'".(in_array($value, explode(",",$selected)) ? " selected='selected'":"").">$name</option>";
} while ($row_branches = mysql_fetch_assoc($branches)); ?>
</select>
try:
<select name="w_owning_branches[]" size="10" id="w_owning_branches" multiple="multiple" required>
<option value="" class="dropdown-option"> Select Owning Branch </option>
<?php do {
$value = $row_branches['branch_id'];
$name = $row_branches['name'];
$selected = '1,2,3,4,5,6';
$selected_values = explode(",",$selected);
echo "<option value='$value'".((in_array($value,$selected_values)) ? " selected='selected'":"").">$name</option>";
} while ($row_branches = mysql_fetch_assoc($branches)); ?>
</select>
You could use something like this:
$selected = '1,2,3,4,5,6';
$selectedArr = explode(",", $selected);
echo "<option value='$value'".((in_array($value, $selectedArr))?" SELECTED ":"").">$name</option>";
I have stored value from drop down list to database. I want to echo the same value to be displayed in that drop down list in my edit form. how can I achieve that in php?
Region
<select name="stf_region">
<option>Select</option>
<option value="1">MDU</option>
<option value="2">TMM</option>
</select>
i have stored in database using value of selection
but i dont know to display that value in same drop down
Use something like this:
<?
$sql = "SELECT id, description FROM dropDownTable";
$rs = mysql_query($sql);
?>
<select name="dropDown">
<option value="-1">Please select...</option>
<? while ($obj = mysql_fetch_object($rs)) { ?>
<option value="<?= $obj->id; ?>" <? if ($data['downDown'] == $obj->id) echo "SELECTED"; ?>>
<?= $obj->description; ?>
</option>
<? } ?>
</select>
Please note $data needs to be set as an associative array containing attributes of the entity that is being edited. This code is flexible because in the case of a form where a user may have submitted an incomplete form $data could be set to the $_POST variable and so all entered fields can be included without the user needing to re-specify fields they previously filled in. This basically means your form template, inserting an entry and editing an entry can be the same!
Well you could do like this
$query = "select id, label from lookup_table";
$result = mysql_query($query);
$html = "<select name='yourname'><option value="">Please select...</option>";
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$html .= "<option value='$row[id]'>$row[label]</option>";
}
$html = "</select>";
echo ($html);//Display the select in the page
If you're able to get db data into array, you can work with them like this:
<?php
$options = array('label 1' => 'value 1', 'label 2' => 'value 2');
echo "<select name=somename>";
foreach($options as $key => $value){
echo "<option value=" . $value . ">" . $key . "</option>";
}
echo "</select>";
?>
i used the COOKIE to match the value that should be selected when it comes to edit the form.
<?php
while($result_row=mysql_fetch_array($result)){
if ( $_COOKIE['MY_COOKIE'] == $result_row[importance_level_id )
{
echo "<option SELECTED=\"SELECTED\" value=$result_row[importance_level_id]>$result_row[importance_level]</option>";
}
else
{
echo "<option value=$result_row[importance_level_id]>$result_row[importance_level]</option>";
}
?>
Assuming you are ok with selecting the value out of the database into a PHP variable (let's say $region) I think you are just after
Region
<select name="stf_region">
<option>Select</option>
<option value="1"<?php echo ($region == '1') ? ' selected="selected"' : ''; ?>>MDU</option>
<option value="2"<?php echo ($region == '2') ? ' selected="selected"' : ''; ?>>TMM</option>
</select>
This is the most concise answer to the question that I think you are asking. However this is a very specific answer and assumes that your dropdown values are hard-coded and won't really be changing.
If you want a more flexible setup that retrieves the values of the dropdown from the database you are looking for something more along the lines of what has been suggested by Gordon Murray Dent above
<div class="form-group has-success col-md-6">
<label class="control-label" for="state-success">Select Buyer</label>
<select id="state-success" class="form-control ">
<?php
$sql="SELECT full_name FROM `new_customer`";
$data=mysqli_query($dbcon,$sql);
?>
<option>Select byer...</option>
<?php while($row1=mysqli_fetch_array($data)){?>
<option value="<?php echo $row1['full_name'];?>"><?php echo $row1['full_name'];?></option>
<?php } ?>
</select>
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>";