keep the selected value for dropdown after submit - php

How do I keep the selected item after I submit the page? I have the country dropdown list with the following code.
<?php
$SQL = "SELECT countr_id, country_name FROM countries";
$Result = mysql_query($SQL) or die(mysql_error());
?>
<select name="country" style="width:400px"> <option value='-1'></option>
<?php
while($row = mysql_fetch_array($Result))
{
echo "<option value=\"".$row["country_id"]."\">".$row["country_name"]."</option>";
}
?>

<?php
$SQL = "SELECT countr_id, country_name FROM countries";
$Result = mysql_query($SQL) or die(mysql_error());
?>
<select name="country" style="width:400px"> <option value='-1'></option>
<?php
while($row = mysql_fetch_array($Result))
{
echo "<option value=\"".$row["country_id"]."\"";
if($_POST['country'] == $row['country_id'])
echo 'selected';
echo ">".$row["country_name"]."</option>";
}
?>

<?php
while($row = mysql_fetch_array($Result))
{
if ($_POST['country'] == $row["country_id"]) {
echo "<option value=\"".$row["country_id"]."\" selected="selected">".$row["country_name"]."</option>";
} else {
echo "<option value=\"".$row["country_id"]."\">".$row["country_name"]."</option>";
}
}
?>

<?php
$SQL = "SELECT countr_id, country_name FROM countries";
$Result = mysql_query($SQL) or die(mysql_error());
?>
<select name="country" style="width:400px"> <option value='-1'></option>
<?php
while($row = mysql_fetch_array($Result))
{
echo "<option ";
if($_REQUEST['country'] == $row["country_id"]) echo 'selected="selected" ';
echo "value=\"".$row["country_id"]."\">".$row["country_name"]."</option>";
}
?>

This code depends if you are trying to keep the value after you submit back to the original page.
<?php
$SQL = "SELECT countr_id, country_name FROM countries";
$Result = mysql_query($SQL) or die(mysql_error());
?>
<select name="country" style="width:400px">
<option value='-1'></option>
<?php
while($row = mysql_fetch_array($Result))
{
echo "<option ";
if($_REQUEST["yourSelectName"] ==$row["country_id"])
echo ' selected = "selected" ';
echo " value=\"".$row["country_id"]."\">".$row["country_name"]."</option>";
}
?>
</select>

Or you can do it all inline... less code, doesn't require all the if/then/else stuff
Change
echo "<option value=\"".$row["country_id"]."\">".$row["country_name"]."</option>";
To
echo "<option ". (($_POST['country'] == $row["country_id"]) ? 'selected ' : '') ."value=\"".$row["country_id"]."\">".$row["country_name"]."</option>";
If it is get (passed in URL), the use GET

<?php
$selectedid = 5; //example of selected if before submitting
$SQL = "SELECT countr_id, country_name FROM countries";
$Result = mysql_query($SQL) or die(mysql_error());
?>
<select name="country" style="width:400px"> <option value='-1'></option>
<?php
while($row = mysql_fetch_array($Result))
{
echo "<option value=\"".$row["country_id"]." ".(($selected==$row["country_id"])?"SELECTED":"")."\">".$row["country_name"]."</option>";
}
?>

//assume you have $result = array(your result list);
<select name='question'>
<?php
foreach ($result as $question) {
if ($_POST['question'] == $question) {
$selected = "selected";
} else {
$selected = '';
}
echo "<option value='" . $question . "' $selected>$question</option>";
}
?>
</select>

Related

PHP Query set combobox for another combobox

how to fix this syntax for this logic ?
i want to select my select option to select the another select option
<?php
$query_string = "SELECT * FROM products";
$query_string1 = "SELECT * FROM suppliers where ProductID = // firstSelectoption(value)";
$query_string2 = "SELECT * FROM categories";
$query = mysql_query($query_string);
$query1 = mysql_query($query_string1);
$query2 = mysql_query($query_string2);
?>
and in the body i make
<select name="first" id="first" onchange="childrenOnChange(this.value)">
<?php
while ($row = mysql_fetch_array($query)) {
echo '<option value=' . $row["ProductID"] . '>';
echo $row['ProductID'];
echo '</option>';
}
?>
</select>
<select name="second" id="second">
<?php
while ($row = mysql_fetch_array($query1)) {
echo '<script>';
echo 'var arr = array(';
$row['SupplierID'] . ',';
echo ')';
echo '</script>';
}
?>
</select>
i want to set the second select option value with $query1;
If you get the value from the query with $val = mysql_fetch_array($query1), then you can include the following in your loop:
$selected = '';
if ($row['ProductID'] == $val) {
$selected = "selected";
}
echo '<option value="'.$row['ProductID'].'" '.$selected.'>'.$row['ProductID'].'</option>';

keeping selected value in the dropdown after reloading the page in php

I have a dropdown in my form, In which data is being fetch from database, Problem is i want to keep the selected value in the dropdown if page reloads. Any help will be really appreciated.
Here is my code
<select name="ans_type" class="select-form " onChange="checkAnswer(this.value)" style="background-color: #fff !important;width:159px!important;">
<option value="" style="color:#000">Select</option>
<?php
$sql = "select * from (table name)";
$res = mysqli_query($dbhandle,$sql);
$numrows = mysqli_num_rows($res);
if($numrows){
while($obj = mysqli_fetch_object($res)){
if($ansTypeId == $obj->id){
echo '<option value="'.$obj->id.'" style="color:#000" selected>'.($obj->ans_type).'</option>';
}
else{
echo '<option value="'.$obj->id.'" style="color:#000">'.($obj->ans_type).'</option>';
}
}
}
?>
</select>
Try below code.
<select name="ans_type" class="select-form " onChange="checkAnswer(this.value)" style="background-color: #fff !important;width:159px!important;">
<option value="" style="color:#000">Select</option>
<?php
$selectStr = '';
$sql = "select * from (table name)";
$res = mysqli_query($dbhandle,$sql);
$numrows = mysqli_num_rows($res);
if($numrows){
while($obj = mysqli_fetch_object($res)){
$selectStr = ($ansTypeId == $obj->id) ? 'selected' : '';
echo '<option value="'.$obj->id.'" style="color:#000" '.$selectStr.'>'.($obj->ans_type).'</option>';
}
}
?>
</select>
Try this
selected = '';
if($ansTypeId == $obj->id){
$selected = "selected='selected'";
}
echo '<option value="'.$obj->id.'" style="color:#000" $selected>'.
($obj->ans_type).'</option>';

How to insert data in a dropdown list

We managed to get the drop down list menu however, we are having difficulties getting the data from sql. So far, this is what we got.
<select>
<option id="">--Select jobscope--</option>
<?php
$con=mysqli_connect("host","user","password","database");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$getIT = mysqli_query("SELECT job_title FROM `job_details`");
while($viewIT = mysqli_fetch_array($getIT)) {
}
?>
<option id="<?php echo $viewIT['job_title']?>"<?php echo $viewIT['job_title']?></option>
</select>
Shouldn't be like this ? with tag inside WHILE LOOP
while($viewIT = mysql_fetch_array($getIT)) {
<option id="<?php echo $viewIT['job_title']?>"<?php echo $viewIT['job_title']?></option>
}
$query = "SELECT * FROM test_groups_tb WHERE user_id='$userid'";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_assoc($result))
{
$dd .= "<option value='{$row['group_id']}'>{$row['group_name']}</option>";
}
Try this,
<option value="">--select--</option>
<?php
while($rec = mysql_fetch_assoc($result)) {
?>
<option value="<?=$rec['job_title']?>"><?=$rec['job_title']?></option>
<?php }
}?>
</select>
I am not from php background. Try this.
<?php
$query = "SELECT job_title FROM job_details";
$result = $mysqli->query( $query );
echo '<select id="domain_account" name="domain_account" class="txtBox">';
echo '<option value="">-select-</option>';
while ($row = $result->fetch_assoc()){
?>
<option value="<?php echo $row['job_title']; ?>"><?php echo $row['job_title']; ?></option>
<?php
}
echo "</select>";
?>
Better use PDO or MYSQLi . MYSQL* is depriciated
U need to use that <option id="<?php echo $viewIT['job_title']?>"<?php echo $viewIT['job_title']?></option> line b/w while loop
$getIT = mysql_query("SELECT job_title FROM `job_details`");
while($viewIT = mysql_fetch_array($getIT)) {?>
<option id="<?php echo $viewIT['job_title']?>"<?php echo $viewIT['job_title']?></option>
<?hp }?>

Get value of mysql database in select menu

I have issue with select menu on PHP. I tried to get mysql database to select menu. However, it displays none.
Here is my code:
default:
mysql_select_db($database_conn, $conn);
$query_Rsenroll = "SELECT * FROM `tbl_enroll` WHERE `tbl_enroll`.`courseid` ='".$_GET['courseid']."'";
$Rsenroll = mysql_query($query_Rsenroll, $conn) or die(mysql_error());
$row_Rsenroll = mysql_fetch_assoc($Rsenroll);
$totalRows_Rsenroll = mysql_num_rows($Rsenroll);
$courseid = $row_Rsenroll['courseid'];
$er_staffid = "";
break;
}
?>
<select name="courseid">
<option value="" SELECTED>Selected Course ID</option>
<?php
foreach( $Course as $course_id) {
if ( $course_id == $courseid) {
$selected = " SELECTED";
} else {
$selected = "";
}
?>
<option value="<?php echo $course_id; ?>"<?php echo $selected; ?>><?php echo $row_Rsenroll['courseid']; ?></option>
<?php
}
?>
</select>
Thank you for any help and advice.
Assuming courseid is being passed as a variable in the sending URL (file.php?courseid=COURSEID), I think this should do what you want:
This might clean up your script a little (though I switched it to mysql_fetch_array as I'm more familiar with that than mysql_fetch_assoc. Feel free to use assoc):
<?php
$cid = '6116';
?>
<select name="courseidMenu">
<option value="" SELECTED>Selected Course ID</option>
<?php
$query = mysql_query("SELECT * FROM tbl_enroll WHERE courseid = '$cid'", $conn)or die(mysql_error());
$total_rows = mysql_num_rows($query);
while($row = mysql_fetch_array($query)){
$courseId = $row['courseid'];
?>
<option value="<?=$courseId?>" ><?=$courseId?></option>
<?
}
?>
</select>
updated use this it is working on my portal <select>
<option value=''>Select Provider</option>
<?php
$server="server name";
$user="user name";
$password="password";
$database="database";
$conn=mysql_connect($server,$user,$password) or die("connection failed");
mysql_select_db($database,$conn);
$query_Rsenroll = "SELECT * FROM `tbl_enroll` WHERE `tbl_enroll`.`courseid` ='".$_GET['courseid']."'";
$result= mysql_query($query_Rsenroll, $conn) or die(mysql_error());
$n=mysql_num_rows($result);
if($n>0)
while($row=mysql_fetch_array($rs))
echo"<option value='$row['courseid']'>$row['courseid']</option>";
mysql_close($conn);
?>

How to make a dropdown with data from a mysql table?

I wanted to make a dropdown area by getting the values from a mysql table. This code does not seem to work; all it prints out is the box for the dropdown without the content, how can I get this to work? or is there an alternative procedure in doing this?
<?
$connection = mysql_connect("localhost", "root", "");
mysql_select_db("test", $connection);
$query = "SELECT full_name FROM test";
$names = mysql_query($query);
function dropDown($content, $result)
{
while($row = mysql_fetch_row($result))
{
$name = $row[0];
$content .= "<option value='$name'>$name</option>";
}
}
$content=<<<EOT
<html>
<body>
<select name="name">
EOT;
dropDown($content, $names)
$content.=<<<EOT
</select>
</body>
</html>
EOT;
echo $content;
?>
return the string. PHP is not C where you use out parameters just because they are sometimes handy.
function dropDown($result, $fieldName)
{
$content = '';
while($row = mysql_fetch_row($result)) {
$name = $row[0];
$content .= "<option value='$name'>$name</option>";
}
return '<select name="'.$fieldName.'">'.$content.'</select>';
}
$content = '<html><body>';
$content .= dropDown($names, 'name');
here database details
mysql_connect('hostname', 'username', 'password');
mysql_select_db('database-name');
$sql = "SELECT username FROM userregistraton";
$result = mysql_query($sql);
echo "<select name='username'>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['username'] ."'>" . $row['username'] ."</option>";}
echo "</select>";
here username is the column of my table(userregistration)
it works perfectly
or simply the code is
<?
$sql = "SELECT * FROM userregistraton ";
$result = mysql_query($sql); ?>
<select name="username" id="username">
<option value="">Select user</option>
<?php
while ($row = mysql_fetch_array($result))
{ ?>
<option value="<?php echo $row['uid']?>" <?php if($row['uid']==$_POST["username"]) echo "selected"; ?> >
<?php echo $row['username'];?></option>
<?php
} ?>
<?php echo $form->dropDownList($model,'courseprefer', array(
'(*value in database table*'=>'displaying value',
)); ?>
you can manually add them just make sure that the first value in array is in the database table.. :)
to avoid error!

Categories