Dropdown and MySQl - php

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>

Related

Year Dropdown do I have wrong datatype?

Why is it not picking up the items I want from the table in the DB instead its putting YEAR as the item in the column of the table.
enter image description hereBudget Year:enter image description here
<?php
echo "<select name= 'Budget_Year' class='form-control selectpicker' onChange='getState(this.value)' Required>";
$default = "year";
$time = array('year'=>"2018-2019");
foreach($time as $key=>$val)
{
echo ($key == $default) ? "<option selected=\"selected\" value=\"$key\">$val</option>":"<option value=\"$key\">$val</option>";
}
$sql = "SELECT budget_year FROM Budget_Year";
$query = sqlsrv_query($conn,$sql);
$query_display = sqlsrv_query($conn,$sql);
while($row=sqlsrv_fetch_array($query_display,SQLSRV_FETCH_ASSOC))
{
echo '<option value=" '. $row['budget_year'].' ">'.$row['budget_year']. '</option>';
continue;
}
?>
</td>
I'm not sure what you're trying to do.
Here in your code (as in your database) the only thing which is submitted through the form and store in your DB, will be the string "year".
When you make a $time = array('year'=>"2018-2019"); "year" is the key, and "2018-2019" is the value.
If you write:
$default = "year";
$time = array('year'=>"2018-2019");
foreach($time as $key=>$val)
{
echo ($key == $default) ? "<option selected=\"selected\" value=\"$key\">$val</option>":"<option value=\"$key\">$val</option>";
}
as there is only one pair of key/value in your array, the code above it's striclty the same as just writing:
echo "<option selected=\"selected\" value=\"year\">2018-2019</option>";
And then, after this listing your DB results. And as there is only records in the DB with "year" in the column Budget_Year, your select list will result in:
<select name= 'Budget_Year' class='form-control selectpicker' onChange='getState(this.value)' Required>
<option selected="selected" value="year">2018-2019</option>
<option value="year">year</option>
<option value="year">year</option>
<option value="year">year</option>
</select>
When storing the result with the form for adding a new entry, you should consider using $val in the value arg of your option, not $key:
<option selected=\"selected\" value=\"$val\">

How to show the selected item name in the drop down list?

I have created a drop down list which is working perfectly fine, its fetching data from database and showing it in the drop-down list. The problem is that I am unable to identify that where to use 'selected'attribute in the select tag. Right now whatever the field I select it opens it, but in the drop down list it shows the first given name. I also tried to use 'selected' attribute, but it was showing the last item name in the drop-down list.
Kindly check it and guide me how to use 'selected' attribute in the loop.
<?php
//Drop Down List
$sub_query = "select * from sub_categories where category_id=$category_id ";
if (!$sub_query_run = mysql_query($sub_query))
{
echo mysql_error();
}
else
{
echo "<select name='menu1' id='menu1' >
<option value='#'> All</option> ";
while ($sub_query_fetch = mysql_fetch_array($sub_query_run))
{
//$sub_query_fetch = mysql_fetch_array($sub_query_run);
$sub_category_id2 = $sub_query_fetch['sub_category_id'];
$sub_category_name = $sub_query_fetch['sub_category_name'];
echo "<option value='earings2.php?sub_category_id=$sub_category_id2' >"
.htmlspecialchars($sub_category_name= $sub_query_fetch['sub_category_name']).
"</option>";
}
}
follow this example..
<select name="cate" id="cate" class="reginput" >
<option value="">Select Category</option>
<?php $s2="select * from tbl_category order by cate_name";
$q2=mysql_query($s2);
while($rw2=mysql_fetch_array($q2)) {
?>
<option value="<?php echo $rw2['id']; ?>"<?php if($rw2['id']==$row['cate_id']) echo 'selected="selected"'; ?>><?php echo $rw2['cate_name']; ?></option><?php } ?>
</select>
You have to add a condition for the selected item.
echo "<option value='earings2.php?sub_category_id=$sub_category_id2'";
if ($sub_category_id2 == $MATCHING_CATEGORY_ID) echo " selected";
echo ">".htmlspecialchars($sub_category_name= $sub_query_fetch['sub_category_name'])."</option>";
Where $MATCHING_CATEGORY_ID is the category id that will be selected.
<option value="#" selected>ALL</option>
"selected" should be included in option tag
Try it like,
// get the category id from request parameter
$sc_id=isset($_REQUEST['sub_category_id']) ? $_REQUEST['sub_category_id'] : "";
while ($sub_query_fetch= mysql_fetch_array($sub_query_run))
{
//$sub_query_fetch= mysql_fetch_array($sub_query_run);
$sub_category_id2= $sub_query_fetch['sub_category_id'];
$sub_category_name= $sub_query_fetch['sub_category_name'];
$sel='';
if($sc_id==$sub_category_id2)// get the selected item
$sel='selected="selected"';
echo "<option value='earings2.php?sub_category_id=$sub_category_id2' ".$sel." >"
.htmlspecialchars($sub_category_name= $sub_query_fetch['sub_category_name']).
"</option>";
}
you need to use select in <option> tag
try this
while ($sub_query_fetch= mysql_fetch_array($sub_query_run))
{
//$sub_query_fetch= mysql_fetch_array($sub_query_run);
$sub_category_id2= $sub_query_fetch['sub_category_id'];
$sub_category_name= $sub_query_fetch['sub_category_name'];
$selected = ($isSelected == $sub_category_id2) ? 'selected' : ''; should be your selected condition fetch from db
echo "<option ".$selected."
value='earings2.php?sub_category_id=$sub_category_id2' >"
.htmlspecialchars($sub_category_name= $sub_query_fetch['sub_category_name']).
"</option>";
}

display from database to dropdown list

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>

selecting value from drop down dropdown

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>";

How to make form elements "remember" selection?

I am using php to build a "change classifieds" page right now.
I use Mysql as a db.
Currently I use PHP to fetch all mysql information about the classified, and then I output it like this:
$table.="
<select name='year' id='year'>
<option id='2000' value='2000'>2000</option>
<option id='2001' value='2001'>2001</option>
<option id='2002' value='2002'>2002</option>
</select>";
echo $table;
I have a picture upload tool which submits the page to itself, and at the same time uploads the picture, and shows it at the bottom of the page. The problem is, whenever this is done, the user must fill in all information again, because they are "forgotten".
I know about input type="text" where you simply can do something like this:
<input type="text" name="text" value="<?php echo $_POST['text'];?>">
but what about selects? Radios? etc?
What should I do here?
Thanks
You can set the selected attribute to "selected" to make it the default/current selection:
<option id="2000" value="2000" selected="selected">2000</option>
php example:
$options = array( '2000' => '2000', '2001' => '2001', '2002' => '2002' );
$selected = '2001';
$select = '<select name="year" id="year">';
foreach ( $options as $key => $value )
{
$select .= ' <option value="' . $key . ( $key == $selected ? '" selected="selected">' : '">' ) . $value . '</option>';
}
$select .= '</select>';
<option
id='2000'
value='2000'
<?php if(isset($year) && $year === '2000') echo 'selected="selected"'?>
>2000</option>
Where $year contains the year from wherever. This assumes $year is a string. If $year is an integer change the condition to:
if(isset($year) && $year === 2000)
For radio buttons and checkboxes just replace selected="selected" with checked="checked".
For selects, it is a bit more intense you need to loop through the data and check if the value equals the selected value (at least that is the easiest to do). But you could apply a similar technique as seen with the checkbox / radio.
<input type="radio" name="radio1" value="1" <?php echo (!empty($_POST['radio1']))?'checked':''?>>
<input type="checkbox" name="chkbox1" value="1" <?php echo (!empty($_POST['chkbox1']))?'checked':''?>>
Since you are creating the table PHP side here is the code:
$years = array('2000', '2001', '2002');
$table .= "<select name='year' id='year'>";
foreach ($years as $year) {
$table .= "<option id='" . $year . "' value='" . $year . "' " .
(($_POST['year'] == $year)?'selected':'') . ">" . $year . "</option>\n";
}
$table.="</select>";
Should get you on the right track for select statements. The ? and : make up the ternary operator which is a shortened if / else statement.
You can conditionnally echo selected in PHP, depending on the value to select.
<?php
$selected[$_GET['year']] = "selected='selected'";
$table.="
<select name='year' id='year'>
<option id='2000' value='2000' " . $selected['2000'] . ">2000</option>
<option id='2001' value='2001' " . $selected['2001'] . ">2001</option>
<option id='2002' value='2002' " . $selected['2002'] . ">2002</option>
</select>";
echo $table;
?>

Categories