<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>";
Related
I am struggling to get this code to work, what I want from it is to show the item (that is already in the database) to be selected in the selection form.
<label>Server Ports:</label>
<select multiple class="form-control" name="select[]">
<?php
// Get Server Information
$query = "SELECT port_no FROM _servers WHERE (server_id = '$servid') ";
$result = mysql_query($query) or die ('Unable to run query:'.mysql_error());
while($row = mysql_fetch_assoc($result)){
$no = $row['port_no'];
}
$query = "SELECT id, name, port_no, unique_id FROM ports ORDER BY name ASC";
$result = mysql_query($query) or die ('Unable to run query:'.mysql_error());
while($row = mysql_fetch_assoc($result))
{
$port_no = $row['port_no'];
$port_name = $row['name'];
$p_unique = $row['unique_id'];
}
?>
<option value="<?php echo $p_unique;?>"
<?php
if ($p_unique == $no) {
$check = 'selected';
} else {
$check = '';
}
echo $check;
?>
>
<?php
echo $row['name'];
?>
(
<?php
echo $row['port_no'];
?>
)</option>
<?php
}
?>
</select>
Are you sure your test must be if ($p_unique == $no) {} and not if ($port_no == $no) {} ?
If yes, try to checks your variables values :
Do a var_dump() of $no :
var_dump($no);
In your while() loop, you can also check values of $p_unique and $no like that :
var_dump($p_unique.'/'.$no);
Also, here is a simplest way to test for selected :
<option value="<?php echo $p_unique;?>" <?php if ($p_unique == $no) echo 'selected="selected"'; ?>><?php echo $row['name'] ;?> (<?php echo $row['port_no'] ;?>)</option>
<select name="corequisite">
<option value="" selected="selected">No Corequisite</option>';
$res = mysql_query("SELECT * FROM course");
while($row = mysql_fetch_array($res)){
echo'<option value="'.$row['Code'].'"';if($row['Code']==$result['corequisite']) echo'
selected="selected"';;echo'>'.$row['Name'].'</option>';
}
echo'</select>';
I want the 'No Corequisite' value to be selected if no other options will be match with $result['corequisite']. But the first item in the table will be selected!
How can I fix it?
It may be fixed by inserting 'corequisite' as the first record. A good way?
Answering your last question, probably a bad way.
Try this code, currently on the laptop so haven't tested it.
<select name="corequisite">
<?php
$res = mysql_query("SELECT * FROM course");
$rows = mysql_num_rows($res);
if ($rows == 0)
{
$sel = 'selected';
} else
{
$sel = '';
}
echo '<option '.$sel.' value="">No Corequisite</option>';
while($row = mysql_fetch_array($res))
{
$sel = '';
if ($row['Code'] == $result['corequisite'])
{
$sel = 'selected';
}
echo '<option '.$sel.' value="'.$row['Code'].'">' . $row['Name'] . '</option>';
}
?>
</select>
I haven't tested it, but this should work
<select name="corequisite">
<?php
// this is your string that will return all your results
$to_echo = '';
// this will check if any value inside is selected ( this will be useful later )
$any_item_selected = false;
$res = mysql_query("SELECT * FROM course");
while($row = mysql_fetch_array($res))
{
if($row['Code']==$result['corequisite']) // OK! so one item is selected, remember it!
{
$to_echo .= '<option selected="selected" value="'.$row['Code'].'">'.$row['Name'].'</option>';
$any_item_selected = true;
}
else
$to_echo .= '<option value="'.$row['Code'].'">'.$row['Name'].'</option>';
}
// Now we can check if anything were selected, and prepend the "No corequisite" option, selected or not! :)
if( ! $any_item_selected)
$to_echo = '<option value="" selected="selected">No Corequisite</option>' . $to_echo;
else
$to_echo = '<option value="">No Corequisite</option>' . $to_echo;
echo $to_echo;
?>
</select>
I have a script that get all ids from a table and print them on option select form, and i want to reload page with the id i chose as selected on option. This is the script:
<?php
include('include/menu.php');
include('include/mysql.php');
if ($db_found) {
echo "<form action='' name='form' method ='get'>
<select name='funcionario'>";
$SQL = "SELECT * FROM funcionarios";
$result = mysql_query($SQL);
while ( $db_field = mysql_fetch_assoc($result) ) {
$idfunc = $_GET['funcionario'];
$selected = ($idfunc==$idfunc->$db_field['idfunc']) ? ' selected="selected"' : '';
echo "<option value'".$db_field['idfunc']."' ".$select." onclick='document.form.submit();' >".$db_field['nomefunc']."</option>";
}
echo "</selected></form>";
echo $idfunc;
} else {
print "Database NOT Found ";
mysql_close($db_handle);
}
?>
But the script are always returning only the first id as selected.
Remove $idfunc-> from $idfunc->$db_field['idfunc']
Replace value'".$db_field['idfunc']."' with value='".$db_field['idfunc']."'
You already defined $idfunc = $_GET['functionario'] which is a string, not an class object.
Also you defined $selected but you are using $select when echoing the result.
For further debugging, use error_reporting(E_ALL); at the top of your script. I guess that's why you didn't get error when you tried to execute this script.
Here's complete script:
<?php
include('include/menu.php');
include('include/mysql.php');
if ($db_found) {
echo "<form action='' name='form' method ='get'>
<select name='funcionario'>";
$SQL = "SELECT * FROM funcionarios";
$result = mysql_query($SQL);
while ( $db_field = mysql_fetch_assoc($result) ) {
$idfunc = $_GET['funcionario'];
$selected = ($idfunc==$db_field['idfunc']) ? ' selected="selected"' : '';
echo "<option value='".$db_field['idfunc']."' $selected onclick='document.form.submit();'>".$db_field['nomefunc']."</option>";
}
echo "</selected></form>";
echo $idfunc;
} else {
print "Database NOT Found ";
mysql_close($db_handle);
}
?>
$selected = ($idfunc)==$db_field['idfunc'] ? ' selected="selected"' : '';
echo "<option value'".$db_field['idfunc']."' ".$selected." onclick='document.form.submit();' >".$db_field['nomefunc']."</option>";
I hope this is you want! Try this!
I want to create a drop down list that allows the user to choose a value that is pulled from a database. When the page is updated based on the chosen value, the choice is reverted to the "original," default choice. How can I allow the selected value to save and remain when the page is updated?
// Drop Down Menu to choose Session
if (isset($_POST['action']))
{
$action = $_POST['action'];
$session = $_POST['session'];
}
else
{
$action = "";
if (!isset($_POST['session']))
{
$session = "";
}
else
{
$session = $_POST['session'];
}
}
?>
<form name='update' action='emailinquiry_webinarnew.php' method='POST'>
Session:
<select name='session'>
<?php
$query = "SELECT distinct session FROM web_attendees";
$result = mysql_query($query) or die(mysql_error());
while ($row = mysql_fetch_assoc($result))
{
echo "<option>".$row['session']."</option>";
}
?>
</select>
<input type='submit' name="ViewButton" value='View'/>
</form>
while ($row = mysql_fetch_assoc($result)) {
if($row['session']==$session){
echo "<option selected='selected' >".$row['session']."</option>";
} else {
echo "<option >".$row['session']."</option>";
}
}
hope it will work for you fine...
Just add such condition in the while loop that prints the options to add the selected="selected" that will select the option by default:
while ($row = mysql_fetch_assoc($result))
{
$selected = '';
if (isset($_POST['session']) && $row['session'] == $_POST['session'])
$selected = ' selected="selected"';
echo "<option{$selected}>".$row['session']."</option>";
}
If in the browser I have parameters like:
http://localhost/specials.php?year=2009&make=honda&model=civic
and the dropdown looks something like this:
<select name="year">
<?php
$query = mysql_query("select distinct year from tbl_content where year = '$year'");
while($row = mysql_fetch_assoc($query)) {
echo "<option value=\"{$row['year']}\">{$row['year']}</option>";
}
?>
</select>
Now what I'm trying to do is show select when the dropdown options value is equal to the parameter year in the browser URL.
I tried this:
<select name="year">
<?php
$query = mysql_query("select * from tbl_year
while($row = mysql_fetch_assoc($query)) {
#=============================
if(isset($_GET['year'])) {
$year = (int)$_GET['year'];
if($year == $row['year'] { $selected = "selected"; }
else { $selected = "";
}
echo "<option value=\"{$row['year']}\" {$selected}>{$row['year']}</option>";
}
?>
</select>
Maybe try "selected='selected'" to make it valid xml.
<select name="year">
<?php
$selectedYear = NULL;
if(isset($_GET['year']))
$selectedYear = (int)$_GET['year'];
$query = mysql_query('SELECT year FROM tbl_year GROUP BY year ORDER BY year ASC');
while($row = mysql_fetch_assoc($query)) {
echo '<option value="' . htmlspecialchars($row['year']) . '"';
if($selectedYear === (int)$row['year']) {
echo ' selected="selected"';
}
echo '>' . htmlspecialchars($row['year']) . '</option>";
}
?>
</select>
Fell free to separate functionality!
<?php
function selectList($name,$values,$labels=null,$selected=null){
if($labels==null) $labels=&$values;
$data="<select name='$name'>";
foreach($values as $k=>$v){
$selected=($v==$selected)?'selected="selected"':false;
$data.="<option value='$v' $selected>".htmlspecialchars($labels[$k])."</option>";
}
$data.="</select>";
return $data;
}
$select=isset($_REQUEST['year'])?(int) $_REQUEST['year']:null;
$query=mysql_query("SELECT DISTINCT `year` FROM `tbl_year` ORDER BY `year`");
while(($row=mysql_fetch_assoc($query))!==false){
$values[]=$row['year'];
}
echo selectList("year",$values,null,$select);
?>