I am new to php 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, Tried everything but couldn't get it, May be because of bad coding :) , Any help from anyone would be really appreciated. Thanks.
here is my code
Select
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>
You need to get the session variable first.
<?php
session_start();
$subjectId = ! empty($_SESSION['YOUR_SESSION_VARIABLE']) ? $_SESSION['YOUR_SESSION_VARIABLE'] : '';
?>
<select name="subject_id" class="select-form subjectSelect sub" onchange="ajaxDrp(this.value)" style="background-color: #fff !important; width:159px!important;">
<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){
$selected = ($subjectId == $obj->id) ? 'selected="selected"' : '';
echo '<option value="'.$selected.'" style="color:#000" $selected>'.$selected->subject_name.'</option>';
}
}
}
?>
Related
I am trying to create a sticky drop down option box. but every time I click the submit button, the value doesn't carry over. It changes.
What am I doing wrong?
<select name=person >
<option value="NULL">Choose a person</option>';
<?php $dbc = getDBC ();
$result = $dbc->query('SELECT * from person');
while ($row = $result->fetch_assoc()) {
echo "<option value=\"$row[ID]\"";
if (isset($_POST['person']) && ($_POST['person'] == $row[ID])) {
echo 'selected="selected"';
}
echo ">($row[ID])   $row[FIRST_NAME]   $row[LAST_NAME]</option>\n";
}
?>
</select>
Try below code i made some syntax changes changes. and also you can optimize it using where condition SELECT * from person where id = $_POST['person']
<select name=person >
<option value="NULL">Choose a person</option>';
<?php
$dbc = getDBC ();
$result = $dbc->query('SELECT * from person');
while ($row = $result->fetch_assoc()) {
echo "<option value=\"$row['ID']\"";
if (isset($_POST['person']) && $_POST['person'] == $row['ID']) {
echo 'selected="selected"';
}
echo ">($row['ID'])   $row['FIRST_NAME']   $row['LAST_NAME']</option>\n";
}
?>
</select>
I know only this method. this method is assume that you know the values in all <option>
<select name="agama" id="agama">
<option value="Islam"<?php if ($rows['agama'] === 'Islam') echo ' selected="selected"'>Islam</option>
<option value="Khatolik"<?php if ($rows['agama'] === 'Khatolik') echo ' selected="selected"'>Khatolik</option>
<option value="Protestan"<?php if ($rows['agama'] === 'Protestan') echo ' selected="selected"'>Protestan</option>
<option value="Hindu"<?php if ($rows['agama'] === 'Hindu') echo ' selected="selected"'>Hindu</option>
<option value="Buddha"<?php if ($rows['agama'] === 'Buddha') echo ' selected="selected"'>Buddha</option>
<option value="Lain-Lain"<?php if ($rows['agama'] === 'Lain-Lain') echo ' selected="selected"'>Lain-Lain</option>
</select>
.... the above code is example from other people not mine.
but My case is the <option> is select from database too.
I have 2 table, oav_event and oav_album
the oav_album has foreign key (event_id) from oav_event table
I want to check if row['event_id'] from oav_album table is equal to option value (from oav_event table) if true, then set selected="selected"
while($row = mysqli_fetch_assoc($result)) { ?>
<option value="<?php echo $row['event_id']; ?>" >Event: <?php echo $row['event_date']; ?> </option>
<?php } ?>
the option will change depend on change in database table, so I don't know the value in option. How should I do?
<select name="event_id">
<?php
$sql = "SELECT * FROM oav_event";
$result = mysqli_query($conn, $sql);
while($row = mysqli_fetch_assoc($result)) {
$selected = "";
if($row['event_id'] == $Yourmatchvalue)
{
$selected = "selected";
}
?>
<option value="<?php echo $row['event_id']; ?>" selected="<?php echo $selected; ?>" >Event: <?php echo $row['event_date']; ?> </option>
<?php } ?>
</select>
may this helps your. you need to replace $Yourmatchvalue variable with your variable.
You can use $_GET as the method on your form and pass the id of the record using it:
while($row = mysqli_fetch_assoc($result)) {
if (!empty($_GET['event_id']) && $row['event_id'] == $_GET['event_id']) {
$selected = 'selected = "selected"';
} else {
$selected = '';
}
echo '<option '.$selected.' value="'.$row["event_id"].'">'.$row["event_date"].'</option>';
}
Here is a solution,
$selected_value = 'Hindu'; // This will come from database
Change option tag with this
<option value="<?php echo $row['event_id']; ?>" <?php echo ($row['event_id'] == $selected_value) ? 'selected="selected"' : ''; ?> >Event: <?php echo $row['event_date']; ?> </option>
Create one function which will create options list like this:
function setDropdownValue($selectQueue_list,$selectedVal)
{
$queueVal = '';
$selectQueue_list_res=$db->query($selectQueue_list);
while($selectQueue_list_res_row=$db->fetchByAssoc($selectQueue_list_res))
{
$val = $selectQueue_list_res_row['id'];
$name = $selectQueue_list_res_row['name'];
if($val == $selectedVal)
{
$queueVal .= "<option value='$val' selected='selected' label='$name'>$name</option>";
}
else
{
$queueVal .= "<option value='$val' label='$name'>$name</option>";
}
}
return $queueVal;
}
Then create a query:
$get_value_query="SELECT id, name FROM table";
$dropdown_selected_value = !empty($dropdown_value) ? $dropdown_value: ''; // Pass value which you want to be selected in dropdown
Then call this function:
$dropdown_options = setDropdownValue($get_value_query, $dropdown_selected_value);
Later when you get dropdown options in $dropdown_options, use jquery to populate the dropdown, like this:
$('#dropdown_select_id').html("$dropdown_options");
Give it a try, and let me know.
I have select option. this option has multiple value from database. I want to update something from database, this value i want to update is exist on the select option I have.
this is my option code
$id = $_GET['update'];
$query = mysql_query("SELECT * FROM transaction where id = '$id'") or die ("could not search");
$count = mysql_num_rows($query);
while ($rows = mysql_fetch_array($query)) {
$id = $rows['id'];
$tranid = $rows['tranid'];
$trandate = $rows['trandate'];
$patientid = $rows['patientid'];
$transactiontype = $rows['transactiontype'];
$trandescription = $rows['trandescription'];
$tranquantity = $rows['tranquantity'];
$tranunitprice = $rows['tranunitprice'];
$tranamount =$rows['tranamount'];
$gettrandescription = $rows['trandescription'];
}
}
if (isset($_POST['selectmedicine'])) {
$gettrandescription=$_POST['medicineid'];
}
if (isset($_POST['selectroomquantity'])) {
$tranquantity=$_POST['quantity'];
}
?>
<script type="text/javascript">
$('#collapseone').collapseone({
toggle: true
});
<option value="<?php echo $trandescription; ?>" <?php if($trandescription==$gettrandescription){ echo "selected";} ?> ><?php echo $gettrandescription; ?></option>
<option value="<?php echo $tranquantity; ?>" <?php if($tranquantity==$tranquantity){ echo "selected";} ?> ><?php echo $tranquantity; ?></option>
this has value results, but i cant fetch this value to my existing select option.
If you want to "make that variable an array" as aldrin27 said, append [] to the name attribute of the select tag. The selected value of the option with name selectroomquantity will be available in your script as $_POST["selectroomquantity"] (this is the varible).
<select multiple name="selectroomquantity[]">
<option value="...">...</option>
</select>
It should only be necessary if multiple options can be selected however.
Also, there seems to be a typo:
<?php if($tranquantity==$tranquantity)
That check will always return true. It should probably be:
<?php if($tranquantity==$gettranquantity)
hi all i just got the code on how to fecth the value to dropdown. actually i made a wrong word. pre-selected is the right one, sorry for that. here;s the working code.
<select name="selectmedicine" class="form-control col-sm-4" id="medicinename">
<option id="0" style="width:100px"></option>
<?php
$medicine = mysql_query("SELECT * FROM medicine");
while ($row = mysql_fetch_array($medicine)) {
echo '<option id="' . $row['medicinename'] . '"';
echo ' value="' . $row['medicineid'] . '"';
if($row['medicinename'] == $trandescription) {
echo ' selected="selected"';
}
echo '>';
echo $row['medicinename'];
echo '</option>';
}
?>
</select>
thanks everyone, whos trying to help me on this. actually this is my five revised question sorry for that. and finally i got the right one.
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>';
I am currently working with a dynamic dropdown menu(dependable select boxes). I am pulling the values straight from MySQL DB(if your curious here is how i have the DB SETUP). I am able to get the values of each table and display them accordingly. The problem I am having is echoing the SELECTED value of each selct box. I have a created JS function that will request postfile.php which will then echo the SELECTED value of each box. I am not getting anything echoed. I have checked with firebug but nothing is being posted.
How can I make this work? or Am I approaching this wrong? or Is there a better way? EXAMPLE
Working HTML/PHP
<?php
include ('includes/dbConnect.php');
try {
$pdo = get_database_connection();
$sql = "SELECT *
FROM `categories`
WHERE `master_id` = 0";
$statement = $pdo->query($sql);
$list = $statement->fetchAll(PDO::FETCH_ASSOC);
} catch(PDOException $e) {
echo 'There was a problem';
}
?>
<select name="main" id="main" size="7" class="update">
<option value="">Select one</option>
<?php if (!empty($list)) { ?>
<?php foreach($list as $row) { ?>
<option value="<?php echo $row['id']; ?>">
<?php echo $row['name']; ?>
</option>
<?php } ?>
<?php } ?>
</select>
<select name="subc1" id="subc1" size="7" class="update" disabled="disabled" hidden="hidden">
<option value="">----</option>
</select>
<select name="subc2" id="subc2" size="7" class="update" disabled="disabled" hidden="hidden">
<option value="">----</option>
</select>
<select name="subc3" id="subc3" size="7" class="update" disabled="disabled" hidden="hidden">
<option value="">----</option>
</select>
JS
<script type="text/javascript">
$(document).ready(function () {
$('#main).change(function() {
if ($(this).val()!='
') {
$("#subc1").load("postfile.php",{main_id: $(this).val()});
//$("#subc1").removeAttr('
disabled hidden ');
}
});
//code on change of sel_source
$('#subc1 ').change(function() {
if ($(this).val()!='
') {
$("#subc2").load("postfile.php",{subc1_id: $(this).val()});
//$("#colour").removeAttr('
disabled ');
}
});
$('#subc2 ').change(function() {
if ($(this).val()!='
') {
$("#subc3").load("postfile.php",{subc2_id: $(this).val()});
//$("#colour").removeAttr('
disabled ');
}
});
});
</script>
PHP- postfile.php
if(isset($_REQUEST['main_id']) && !empty($_REQUEST['main_id'])) {
try {
include ('../includes/dbConnect.php');
$pdo = get_database_connection();
$sql = ("select * from `categories` where id='".$_REQUEST['main_id']."' ");
$result = $con->prepare($sql);
$result->execute();
$number_of_rows = $result->fetchColumn();
}catch(PDOException $e) {
echo 'There was a problem';
}
if($number_of_rows > 0) {
$output = '<option value="">Select</option>';
while($row = mysql_fetch_assoc($result)) {
$output .= '<option value="'.$row['id'].'">'.$row['name'].'</option>';
}
} else {
$output = '<option value="">Select</option>';
}
echo $output;
}
if(isset($_REQUEST['subc1_id']) && !empty($_REQUEST['subc1_id'])) {
$result = mysql_query("select * from table where id='".$_REQUEST['subc1_id']."' ");
if($number_of_rows > 0) {
$output = '<option value="">Select</option>';
while($row = mysql_fetch_assoc($result)) {
$output .= '<option value="'.$row['id'].'">'.$row['name'].'</option>';
}
} else {
$output = '<option value="">Select</option>';
}
echo $output;
}
if(isset($_REQUEST['subc2_id']) && !empty($_REQUEST['subc2_id'])) {
$result = mysql_query("select * from table where id='".$_REQUEST['subc2_id']."' ");
if($number_of_rows > 0) {
$output = '<option value="">Select</option>';
while($row = mysql_fetch_assoc($result)) {
$output .= '<option value="'.$row['id'].'">'.$row['name'].'</option>';
}
} else {
$output = '<option value="">Select</option>';
}
echo $output;
}
Maybe you would like to use jQuery UI auto-complete. It is easier to use and less code. It has a remote data source also. Try this one, maybe this can solve your problem. http://jqueryui.com/demos/autocomplete/
Add on submit action to the form. In the function use the following:
var Myvar = $('#subc3 :selected').text();