Php/Html:
Here I add The all Countries list to add/edit page.But Edit page Want Value Selected in dropdown.please help
<select class="form-control" id="country_list" name="country">
<?php
global $wpdb;
$network_lists = $wpdb->get_results("SELECT * FROM media_countries");
foreach($network_lists as $network_list){
?>
<option value="<?php echo $network_list->country_name ?>" selected="<?php
echo $network_list->country_name ?>"><?php echo $network_list>country_name ?></option>
<?php }?>
</select>
This is how I would do it. This will check the value against the value the user selects with the value from the database and properly populate your dropdown menu.
global $wpdb;
$network_lists = $wpdb->get_results("SELECT * FROM media_countries");
echo
'<select class="form-control" id="country_list" name="country">';
foreach($network_lists as $network_list) {
if($_POST['country'] == $network_list->country_name) {
echo '<option value="' . $network_list->country_name . '"' . ' selected="selected"' . '>' . $network_list->country_name . '</option>';
}else {
echo '<option value="' . $network_list->country_name . '">' . $network_list->country_name . '</option>';
}
}
echo
'</select>';
Selected value is in $_POST['country'];
Try:
<select class="form-control" id="country_list" name="country">
<?php
global $wpdb;
$network_lists = $wpdb->get_results("SELECT * FROM media_countries");
foreach($network_lists as $network_list){
?>
<option value="<?php echo $network_list->country_name; ?>" <?php echo ( $_POST['country_name'] == $network_list->country_name ? 'selected' : '' ); ?>><?php echo $network_list>country_name; ?></option>
<?php }?>
</select>
The above code automatically selects the option that has value equals to the submitted $_POST['country'] value.
Related
I want to have dropdown selected option selected after submit/refresh page. I search for other examples but no one works on my code.
How to retain selected value after submit/refresh with this code?
<form name="test" action="test.php?id=<?php echo $row["id"]; ?>" method="post">
<select id="test_email" name="test_email">
<option value="">...select</option>
<?php
$sql2 = "SELECT test_id, test_email FROM test WHERE status='Act'";
$res = $db->query($sql2);
if ($res->num_rows > 0) {
while($row1 = mysqli_fetch_array($res)) {
?>
<option value="<?php echo $row1['test_email'];?>-<?php echo $row1['test_id'];?>"><?php echo $row1['test_id'];?></option>
<?php
}
}
?>
</select>
Solved this way:
Because I could not pass $_POST['test_email'] with <?php echo $row1['test_email'];?>-<?php echo $row1['test_id'];?>
Because I use explode on $_POST['test_email']
I make one more post (insert in db) $test_temp = trim(mysqli_real_escape_string($db, $_POST['test_email'])); to have my dropdown value (whole string) in db.
I added hidden input in my form <input id="test_temp" type="hidden" name="test_temp" value="<?php echo $row["test_temp"]; ?>">
And changed select option line to <option value="<?php echo $row1['test_email'] . '-' . $row1['test_id']; ?>"<?php if($row1['test_email'] . '-' . $row1['test_id'] == $row['test_temp']) echo ' selected="selected"' ; ?>><?php echo $row1['test_id'];?></option>.
Perhaps this could be simpler but it works like a charm.
#roberto06: Thank you for your guidance.
You need to add the selected="selected" attribute on the option matching your $_POST value.
You might also want to concatenate <?php echo $row1['test_email'];?>-<?php echo $row1['test_id'];?> into <?php echo $row1['test_email'] . '-' . $row1['test_id']; ?>, but that's up to you.
Here's a solution (I assumed that $POST['test_email'] has to be equal to $row1['test_email'] . '-' . $row1['test_id'] in order for the condition to be matched). I'm using a shorthand if...else here, but you could also use a classic if :
<form name="test" action="test.php?id=<?php echo $row["id"]; ?>" method="post">
<select id="test_email" name="test_email">
<option value="">...select</option>
<?php
$sql2 = "SELECT test_id, test_email FROM test WHERE status='Act'";
$res = $db->query($sql2);
if ($res->num_rows > 0) {
while($row1 = mysqli_fetch_array($res)) {
?>
<option value="<?php echo $row1['test_email'] . '-' . $row1['test_id']; ?>"<?php echo (isset($_POST['test_email']) && $row1['test_email'] . '-' . $row1['test_id'] == $_POST['test_email']) ? ' selected="selected"' : ''; ?>><?php echo $row1['test_id'];?></option>
<?php
}
}
?>
</select>
</form>
my problem is to show previously selected in a while loop. I found a way to do it in not-loop setup, but a loop is a problem for me.
The following code give me a drop down of countries (and country code):
<?php
foreach($countries as $key => $value) { ?>
<option value="<?= $value . ' ' . $key ?>" title="<?= htmlspecialchars($value) ?>"><?= htmlspecialchars($value) ?></option>
<?php } ?>
And it works good. But imagine someone edit their country, thus I would love to show previously selected country. Of course I have a variable with the specific previously selected country... Thanks.
<?php
$preselected = 'whatever_previous_selected';
foreach($countries as $key => $value) { ?>
<option value="<?php $value; ?>" title="<?= htmlspecialchars($value) ?>" <?php if($preselected == $value) {echo "selected='selected'"; }?>>
<?= htmlspecialchars($value) ?>
</option>
<?php } ?>
try this:
<?php
foreach($countries as $key => $value) { ?>
<?php if ($previousCountry == $value) ?>
<option selected value="<?= $value . ' ' . $key ?>" title="<?= htmlspecialchars($value) ?>"><?= htmlspecialchars($value) ?></option>
<?php else ?>
<option value="<?= $value . ' ' . $key ?>" title="<?= htmlspecialchars($value) ?>"><?= htmlspecialchars($value) ?></option>>
<?php } ?>
Not sure with the syntax but you should detect if the value is equal to the previous country then you put selected attribute on it.
Use selected='selected' when you get id in edit.
<?php
$id = 1; // In edit you get some value here
foreach($countries as $key => $value) {
$selected = (isset($id) && $id == $key) ? "selected='selected'" : "";
?>
<option value="<?= $value . ' ' . $key ?>" title="<?= htmlspecialchars($value) ?>" <?php echo $selected ?>><?= htmlspecialchars($value) ?></option>
<?php } ?>
try this:
<?php
$prevSelectedCountry;
foreach($countries as $key => $value) { ?>
<option <?php echo ($prevSelectedCountry == $value . ' ' . $key)?"selected='selected'":"" ?> value="<?= $value . ' ' . $key ?>" title="<?= htmlspecialchars($value) ?>"><?= htmlspecialchars($value) ?></option>
<?php } ?>
I have table query which is displayed thru select option. The column 1 value is displayed on it. When I select the row 1 value from column 1, I also want the value on column 2 automatically displayed, how would I code that?
Example : medicinename(column1) = medicineprice(column2)
The medicinename is in select option. The medicineprice is not. Just want to display automatically the price every time I select the medicinename.
<div class="col-md-3"><label><h5>Medicine Name : </h5>
<select name="selectmedicine" class="form-control col-sm-4" id="medicinename" style="width:200px">
<option id="0" style="width:100px"> Select here...</option>
<?php
require_once 'config.php';
$medicine = mysql_query("SELECT * FROM medicine");
while ($displaymedicine = mysql_fetch_array($medicine)) {
$medicineid = $displaymedicine['id'];
$medicinename = $displaymedicine['medicinename'];
$medicineprice = $displaymedicine['medicineprice'];
?>
<option id="<?php echo $displaymedicine['medicineid']; ?>"><?php if($displaymedicine ['medicineid'] == $displaymedicine ['medicinename']) echo 'selected="selected"'; ?><?php echo $displaymedicine ['medicinename'] ?></option>
<?php
}
?>
</select>
Do you mean you want to be returned the price of the medicine to your PHP script when you select the name of the medicine? In which case you need to add a value="" attribute to the <option tag. It is the contents of value="" that is returned to the script i.e. if this option was selected from your <select name="selectmedicine">
<option value="999"...>Penicillin</option>
Then PHP would receive $_POST['selectmedicine'] which would contain 999
<div class="col-md-3"><label><h5>Medicine Name : </h5>
<select name="selectmedicine" class="form-control col-sm-4" id="medicinename" style="width:200px">
<option id="0" style="width:100px"> Select here...</option>
<?php
require_once 'config.php';
$medicine = mysql_query("SELECT * FROM medicine");
while ($row = mysql_fetch_array($medicine)) {
echo '<option id="' . $row['medicineid'] . '"';
echo ' value="' . $row['medicineprice'] . '"';
if($row['medicineid'] == $row['medicinename']) {
echo ' selected="selected"';
}
echo '>';
echo $row['medicinename'];
echo '</option>'
}
?>
</select>
Of course a better solution would be to put the unique ID of the medicine table into the value="" attribute. Then when the form is posted to your PHP script you read your database for that one row and you have all the data related to that medicine available to the script.
So that would be
<div class="col-md-3"><label><h5>Medicine Name : </h5>
<select name="selectmedicine" class="form-control col-sm-4" id="medicinename" style="width:200px">
<option id="0" style="width:100px"> Select here...</option>
<?php
require_once 'config.php';
$medicine = mysql_query("SELECT * FROM medicine");
while ($row = mysql_fetch_array($medicine)) {
echo '<option id="' . $row['medicineid'] . '"';
> changed code line
echo ' value="' . $row['id'] . '"';
> changed code line
if($row['medicineid'] == $row['medicinename']) {
echo ' selected="selected"';
}
echo '>';
echo $row['medicinename'];
echo '</option>'
}
?>
Here goes my first post.
I am currently pulling two fields out from my MySQL database with a fetch all and am trying to get the data in those fields to become options in a listbox.
This is my code:
<fieldset class="contact">
<legend>Select a Band</legend>
<!-- Drop List -->
<select id="lst1" name="lst1" tabindex="281" size="1">
<?php
foreach ($bands as $band) {
$name = $band["fldBand"];
$id = $band["pkID"];
$options .= '<option value="' . $id . '">' . $name . '</option>';
}
echo $options;
?>
</select>
and here is the result of echoing out $options:
<option value="1">Rise Against</option><option value="2">Alter Bridge</option><option value="3">Falling In Reverse</option><option value="4">Saosin</option><option value="5">Pennywise</option><option value="6">The Killers</option><option value="7">Thrice</option><option value="8">Four Year Strong</option>
I want to have the foreach loop print out the code that will be the options for my listbox lst1 but it is currently not working. Any ideas as to why?
<?php
foreach ($bands as $band) {
$name = $band["fldBand"];
$id = $band["pkID"]; ?>
<option value = "<?php echo $id; ?>"><?php echo $name; ?></option>
<?php } ?>
Try this code. In your code '<' and '>' makes html entity format. So html does't accept it as a tag.
<select id="lst1" name="lst1" tabindex="281" size="1">
<?php
foreach ($bands as $band) {
$name = $band["fldBand"];
$id = $band["pkID"];
$options .= '<option value="' . $id . '>' . $name . '</option>';
}
echo $options;
?>
</select>
I would like to populate three selectboxes with the same items (from one database table).
This is the code I have so far. I want to avoid three queries. Would you help me with a solution to populate multiple selectboxes?
<select name="resort_3" class="swcomp" data-index="3">
<?php
$today = date("Y-m-d");
$sQuery2 = "SELECT DISTINCT res_id, resort from sv_snow WHERE lud='$today' ORDER by resort";
$sResult2 = mysql_query($sQuery2) or die('Error, query 2 failed');
while($ro = mysql_fetch_assoc($sResult2))
{
echo '<option value="'. $ro['res_id'] . '">'. ucfirst($ro['resort']) . '</option> ';
}
?>
</select>
Save the options in a string and then assign those to all 3 select boxes. Simple as that
<?php
while($ro = mysql_fetch_assoc($sResult2))
{
$options.='<option value="'. $ro['res_id'] . '">'. ucfirst($ro['resort']) . '</option> ';
}
?>
<select name="resort_1" class="swcomp" data-index="1">
<?php echo $options; ?>
</select>
<select name="resort_2" class="swcomp" data-index="2">
<?php echo $options; ?>
</select>
<select name="resort_3" class="swcomp" data-index="3">
<?php echo $options; ?>
</select>