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!
Related
I already try many ways but the value didn't show in dropdown list
Here, this is my code. can you suggest me anything that i was wrong
<?php
$result = mysqli_query($con,"SELECT * FROM project");
if( mysqli_num_rows( $result )==0){
echo "<tr><td>No Rows Returned</td></tr>";
}else{
$row = mysqli_fetch_assoc( $result );
$pos = 0;
echo "<select name=Pname >";
while($pos <= count ($row)){
echo "<option value="$row["project_no"]">"$row["project_name"]"</option>";
$pos++;
}
echo "</select>";?>
And i write as .php file. Thanks for your help.
Try this out:
$output = '';
if(mysqli_num_rows($result) == 0){
// echo error;
} else {
while($row = mysqli_fetch_assoc($result)){
$project_no = $row['project_no'];
$project_name = $row['project_name'];
$output .= '<option value="' . $project_no . '">' . $project_name . '</option>";
}
}
Then inside of your HTML, print your $output variable inside of your <select> element:
<select>
<?php
print("$output");
?>
</select>
It should print all options for every row that you have requested from the database.
Hope this helps :)
Try this:
$result = mysqli_query($con,"SELECT * FROM project");
if( mysqli_num_rows( $result )==0){
echo "<tr><td>No Rows Returned</td></tr>";
}else{
echo "<select name=Pname >";
while ($row = mysqli_fetch_assoc($result)) {
echo "<option value="$row["project_no"]">"$row["project_name"]"</option>";
}
echo "</select>";
}
This is the result code that i can run it. I put this code in a form code of html
$result = mysqli_query($con,"SELECT * FROM project"); ?>
<?php
$output = '';
if(mysqli_num_rows($result) == 0){
// echo error;
} else {
echo " <select name = Pname>";
while($row = mysqli_fetch_assoc($result)){
$project_no = $row['project_no'];
$project_name = $row['project_name'];
$output = "<option value=" . $project_no . "> ". $project_name ." </option>";
print("$output");
}
echo " </select>";
}
?>
Thank you every one for helping me ^^
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>
I've tried a lot to retain the selected option value on all pages which we browse but I didn't succeed :(
Can any one figure out my code what went wrong here ??
<form name="area_form" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" >
<?php
$sql = "select *from city";
$loop = mysql_query($sql)
or die ('cannot run the query because: ' . mysql_error());
echo "<select name='areaa' onchange='window.location.href=this.value'>";
while($row = mysql_fetch_array($loop))
{
if($_POST['areaa']==$row['area'])
{
echo "<option selected VALUE=\"http://example.com\">".$row['area']."</option>";
}
else
{
echo "<option VALUE=\"http://example.com\">".$row['area']."</option>";
}
}
echo "</select>";
?>
Use mysql_fetch_assoc instead of mysql_fetch_array
$areaa = isset($_POST['areaa'])?$_POST['areaa']:"";
while($row = mysql_fetch_array($loop))
{
echo "<option VALUE=\"http://example.com\" $areaa==\"http://example.com\"?'selected=selected' : '' >".$row['area']."</option>";
echo "<option VALUE=\"http://example2.com\" $areaa==\"http://example2.com\"?'selected=selected' : '' >".$row['area']."</option>";
}
<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>";
I am new to php, i created drop down which calling data from mysql data base, user selects option and its save to data base.
Problem Arises in edit form in which its do not showing selected value.
Drop Down code is below:
$query = 'SELECT name FROM owner';
$result = mysql_query($query) or die ('Error in query: $query. ' . mysql_error());
//create selection list
echo "<select name='owner'>\name";
while($row = mysql_fetch_row($result))
{
$heading = $row[0];
echo "<option value='$heading'>$heading\n";
}
echo "</select>"
Please advise solution for the edit form.
Thanks in Advance
you must close <option> tag:
echo "<option value='$heading'>$heading</option>";
$query = 'SELECT name FROM owner';
$result = mysql_query($query) or die ('Error in query: $query. ' . mysql_error());
//create selection list
echo "<select name='owner'>\name";
while($row = mysql_fetch_row($result))
{
$heading = $row[0];
?>
<option <?php if($heading=="SOMETHING") { echo "selected='selected'"; } ?> value="SOMETHING">SOMETHING</option>
<option <?php if($heading=="SOMETHING2") { echo "selected='selected'"; } ?> value="SOMETHING2">SOMETHING2</option>
<option <?php if($heading=="SOMETHING3") { echo "selected='selected'"; } ?> value="SOMETHING3">SOMETHING3</option>
<?php
}
echo "</select>"
I'd do it this way.
$numrows = mysql_num_rows($result);
if ($numrows != 0){
echo "<select name='owner'>\name";
while ($x = mysql_fetch_assoc($result)){
echo "<option value='".$x['heading']."'>".$x['heading']."</option>";
}
echo "</select>";
}
$x['heading'] is using the value of the row 'heading' in the database
It's much more efficient and simply looks more sophisticated.