I have set a value in a session variable in 1 page, now I have to display that session variable value on another page's dropdown as selected.
I've tried everything but couldn't get it to work, maybe because of bad coding.
Here is my code:
<select name="subject_id"
class="select-form subjectSelect sub" onchange="ajaxDrp(this.value)"
>
<option value="" style="color:#000">Select</option>
<?php
$sql = "select * from mock_subject ";
$res = mysqli_query($dbhandle,$sql);
$numrows =mysqli_num_rows($res);
echo mysql_error();
if($numrows){
while($obj = mysqli_fetch_object($res)){
if($obj->status == 1){
if($subjectId == $obj->id){
echo '<option value="'.obj->id.'"
style="color:#000"
selected >'.$obj- >subject_name.'
</option>';
} else {
echo '<option value="'.$obj->id.'"
style="color:#000">'.($obj->subject_name).'
</option>';
}
}
}
}
?>
</select>
Session is already started, my session variable value is subject'Id, but in the dropdown I have to display subjectId's subject name, and code is like this code session:
$_SESSION["subject"] = "contains subject ID"
Related
I have a MySQL table with headers "courseid", "world" and "name".
I am trying to populate an HTML dropdown box via a PHP loop and I can not get it to work.
<?php
$sql = "SELECT courseid, world, name FROM courses ORDER BY world DESC, name ASC";
$result = $link->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$world = '';
if ($world != $row['world']) {
echo '<option value="'.$row['courseid'].'">'.$row['name'].'</option>';
}else{
echo '<optgroup label="'.$row['world'].'">';
}
}
echo '</select>';
} else {
echo "0 results";
}
$link->close();
?>
I would like it to loop through the whole table and display as follows:
<optgroup label"world1">
<option value="courseid1">name1</option>
<option value="courseid2">name2</option>
<option value="courseid3">name3</option>
</optgroup>
<optgroup label"world2">
<option value="courseid4">name1</option>
<option value="courseid5">name2</option>
<option value="courseid6">name3</option>
</optgroup>
(courseid, world and name are values from the table)
A couple of issues. Firstly, you need to move $world = ''; before your while loop, otherwise it will cause the if test to always be true. Secondly, the if test should only be used to test whether to finish an old and start a new optgroup, not to decide whether to output an option. This should work:
if ($result->num_rows > 0) {
echo '<select>';
$world = '';
while($row = $result->fetch_assoc()) {
if ($world != $row['world']) {
if ($world != '') echo '</optgroup>';
echo '<optgroup label="'.$row['world'].'">';
$world = $row['world'];
}
echo '<option value="'.$row['courseid'].'">'.$row['name'].'</option>';
}
echo '</optgroup></select>';
}
else {
echo "0 results";
}
I have the below code where I can get dynamic value into drop down list from mysql db but i can't print selected value when i click on submit button.
can anyone help me urgntly ?
<?php
include("includes/config.inc.php");
$query = "SELECT * FROM category";
$result = mysql_query ($query);
echo "<select class='turnintodropdown' name='CategoryID' ><option value=''>All</option>";
while($r = mysql_fetch_array($result)) {
echo "<option value=".$r['CategoryID'].">".$r['CategoryName']."</option>";
}
echo "</select>";
if (isset($_POST['submit'])) {
$selected_val = $_POST['CategoryID']; // Storing Selected Value In Variable
echo "You have selected :" .$selected_val; // Displaying Selected Value
}
?>
<input type="submit" name="submit" value="Get Selected Values" />
</form>
If you want to preselect the selected value of the then you can use the following code. Also check your form method attrivute to see if it set to post (note that mysql_* functions are deprecated, it's better to use PDO using prepared statements).
while($r = mysql_fetch_array($result)) {
if (!empty($_POST['CategoryID']) && $_POST['CategoryID'] == $r['CategoryID']) {
$selected = 'selected="selected"';
} else {
$selected = '';
}
echo "<option ".$selected." value=".$r['CategoryID'].">".$r['CategoryName']."</option>";
}
from above:
echo "you have selected :" . $selected_val;
from what you are echoing, based on what I'm exp[laining], it echos the ID, not the name of the category.
I try to display me database username with a blank default option. My problem is that my default option is not showing up.
<?php
mysql_connect("localhost","demo","xxxxx");
mysql_select_db("demo");
$first = 0;
$sql=mysql_query("SELECT vorname FROM users WHERE dispo=1");
if(mysql_num_rows($sql)){
$select= '<select name="select">';
while($rs=mysql_fetch_array($sql)){
if (first == 0){
$select.='<option value='' selected="selected"></option>';
$first++;
}else{
$select.='<option value='.$rs['vorname'].'>'.$rs['vorname'].'</option>';
}
}
}
$select.='</select>';
echo $select;
?>
I'm pretty new to php and database, so I would appreciate the help.
I just changed it to always add a default blank option if there are records from the result, then it adds all records as options.
What you had was replacing the first record with the default option.
$sql=mysql_query("SELECT vorname FROM users WHERE dispo=1");
if(mysql_num_rows($sql)) {
$select= '<select name="select">';
$select.='<option value='' selected="selected"></option>';
while($rs=mysql_fetch_array($sql)){
$select.='<option value='.$rs['vorname'].'>'.$rs['vorname'].'</option>';
}
$select.='</select>';
echo $select;
?>
Change below two lines
if ($first == 0){ // from if (first == 0){
// and
$select.='<option value="" selected="selected"></option>';
// from $select.='<option value='' selected="selected"></option>';
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>";
}
I populated the options for a dropdown box using the results of a query. How do I retain the selected value after the user submits?
Here's the code:
$query="SELECT trainingName,trainingID FROM training ORDER BY trainingName";
$result = mysql_query ($query);
echo "<select name='training' value=selected>Training Name</option>";
$training = strip_tags(#$_POST['training']);
echo "<option>---------------------Select---------------------</option>";
while($nt=mysql_fetch_array($result)){
echo "<option value=$nt[trainingID]>$nt[trainingName]</option>";
}
Thanks!
Try this:
$query="SELECT trainingName,trainingID FROM training ORDER BY trainingName";
$result = mysql_query ($query);
echo "<select name='training'>";
echo "<option>---------------------Select---------------------</option>";
while($nt=mysql_fetch_array($result)){
$selected = false;
// check if the current value equals the value submited
if($_POST['training'] == $nt['trainingID']){
$selected = true;
}
// show selected attribute only if $selected is true
echo "<option value='{$nt['trainingID']}' ". ($selected ? "selected" : "") .">{$nt['trainingName']}</option>";
}
echo '</select>';