I have a country dropdown
<select name="country" class="form-control"/>
<option>Select a Country</option>
<?php foreach($country_list as $country) :?>
<option value="<?php echo $account_result->Country;?>"
<?php
if($country->id==$account_result->Country)
{echo 'selected="selected"';};?>>
<?php echo $country->name; ?></option>
<?php endforeach; ?>
</select>
but while updating I am getting only selected value id it's not changing.
You setting same value to all options change this line:
<option value="<?php echo $account_result->Country;?>"
to:
<option value="<?php echo $country->id;?>"
Try below
You have added wrong value in option
<select name="country" class="form-control"/>
<option>Select a Country</option>
<?php foreach($country_list as $country) :?>
<option value="<?php echo $country->id;?>"
<?php
if($country->id==$account_result->Country)
{echo 'selected="selected"';};?>>
<?php echo $country->name; ?></option>
<?php endforeach; ?>
</select>
<?php echo $country->id == $account_result->Country ?"selected":"";?>
your $country->id and $account_result->Country should be same value example
ex: 44=44
then it will be get selected in default page load.
and you have to set the option value as like this.
<option value="<?php echo $country->id;?>"
Related
below is the code which i have problem and the problem is how can i get values if value="php echo $someting;" instead of value="someting"
like how i put value when i use condition if($_POST['city']=='')
how can i put echo value in if statement
anyone got my point then please answer.
<select class="js-example-placeholder-single js-states form-control" id="city" name="city"
title="Select City" data-hide-disabled="true" data-live-search="true">
<optgroup label="Location">
<option value="">No Selection</option>
<?php
$cities = $db->get ("cities");
foreach ($cities as $city){
?>
<option value="<?php echo $city['city_name']; ?>" <?php if($city['city_name'] ==
$selected_city) echo "selected"; ?> >
<?php echo $city['city_name']; ?>
</option>
<?php } ?>
</optgroup>
</select>
Just add empty($_POST['city']) ? 'selected' : '' on empty option:
<?php
$selected_city = $_POST['city'] ?? null;
?>
<select class="js-example-placeholder-single js-states form-control" id="city" name="city" title="Select City" data-hide-disabled="true" data-live-search="true">
<optgroup label="Location">
<option value="" <?= empty($selected_city) ? 'selected' : ''; ?>>No Selection</option>
<?php
$cities = $db->get("cities");
foreach ($cities as $city):
?>
<option value="<?= $city['city_name']; ?>" <?= $city['city_name'] == $selected_city ? 'selected' : ''; ?>>
<?= $city['city_name']; ?>
</option>
<?php endforeach; ?>
</optgroup>
</select>
Here I have a select box where I want to show the value which is stored in the database that is in the JSON format. If the value is present, it shows the selected value, otherwise it shows the default option Delete leads option. It's not working properly.
<div class="col-md-7">
<select class="form-control" id="spm" name="spm" required style="">>
<option value=""> Delete Leads </option>
<?
foreach($slct_optn as $slct_optns)
{
$slctoptn = json_decode($slct_optns['spam_management'],1);
?>
<option value="7" <?php if($slctoptn['delete']==7) {?> selected="selected" <? } ?>>1 Week Older</option>
<option value="30" <?php if($slctoptn['delete']==30) {?> selected="selected" <? } ?>>1 Month</option>
<option value="60" <?php if($slctoptn['delete']==60) {?> selected="selected" <? } ?>>2 Month</option>
<? }
?>
</select>
Can anyone please help me?
I think you could change the $slctoptn['delete'] to $slctoptn[0]['delete'] variable like this :
<div class="col-md-7">
<select class="form-control" id="spm" name="spm" required style="">>
<option value=""> Delete Leads </option>
<?
foreach($slct_optn as $slct_optns)
{
$slctoptn = json_decode($slct_optns['spam_management'],1);
?>
<option value="7" <?php if($slctoptn[0]['delete']==7) {?> selected="selected" <? } ?>>1 Week Older</option>
<option value="30" <?php if($slctoptn[0]['delete']==30) {?> selected="selected" <? } ?>>1 Month</option>
<option value="60" <?php if($slctoptn[0]['delete']==60) {?> selected="selected" <? } ?>>2 Month</option>
<? }
?>
</select>
This will use the only 'delete' array inside the $slctoptn parent array.
I want to make a dropdown list based on options that are obtained from an MySQL database. At this moment my code looks like this:
<?php
if ($resultCheck12 > 0) {
while ($row = mysqli_fetch_assoc($result12)) { ?>
<select name="storage_location[]" required>
<option value=""></option>
<option value="<?php echo $row['id']; ?>"><?php echo $row['storage_name']; ?></option>
</select>
<?php } } ?>
And this code should produce a result that looks like this if it was unsystematically coded:
<select name="sample_group[]" class="sample_group" required>
<option value=""></option>
<option value="water">Water</option>
<option value="pharmaceutical">Pharmaceutical</option>
<option value="food">Food</option>
<option value="food">Swabs</option>
<option value="custom">Custom</option>
</select>
However the results produce something like this:
<select name="sample_group[]" class="sample_group" required>
<option value=""></option>
<option value="water">Water</option>
</select>
<select name="sample_group[]" class="sample_group" required>
<option value=""></option>
<option value="pharmaceutical">Pharmaceutical</option>
</select>
<select name="sample_group[]" class="sample_group" required>
<option value=""></option>
<option value="food">Food</option>
</select>
<select name="sample_group[]" class="sample_group" required>
<option value=""></option>
<option value="swabs">Swabs</option>
</select>
<select name="sample_group[]" class="sample_group" required>
<option value=""></option>
<option value="custom">Custom</option>
</select>
Instead of producing a single dropdown list it makes one for each variable from the MySQL database.
Any ideas how to resolve this issue?
<?php
if ($resultCheck12 > 0) { ?>
<select name="storage_location[]" required>
<option value=""></option>
<?php while ($row = mysqli_fetch_assoc($result12)) { ?>
<option value="<?php echo $row['id']; ?>"><?php echo $row['storage_name']; ?></option>
<?php } ?>
</select>
<?php } ?>
The select tag should be outside the while loop
You are using select tag inside while loop that's why it is repeating it multiple times.
<?php if ($resultCheck12 > 0) { ?>
<select name="storage_location[]" required>
<option value=""></option>
<?php while ($row = mysqli_fetch_assoc($result12)) { ?>
<option value="<?php echo $row['id']; ?>"><?php echo $row['storage_name']; ?></option>
<?php } ?>
</select>
<?php } ?>
I'm trying to insert a PHP script into a selected attribute in HTML5 ,but I have failed with syntax.
Error :"The value of selected attribute is made invalid because it's not supported by the current schema".
It looks like this... How can I solve this problem? Thanks.
<div>
<label>Select author here:</label>
<select name="author">
<option value="">Select an author</option>
<?php foreach ($authors as $author): ?>
<option value="<?php htmlout($author['id']);?>"
selected="<?php ?>">
<?php htmlout($author['name']);?>
</option>
<?php endforeach; ?>
</select>
</div>
try it :
<div>
<label>Select author here:</label>
<select name="author">
<option value="">Select an author</option>
<?php foreach ($authors as $author): ?>
<option value="<?php htmlout($author['id']);?>"
<?php echo $Your_Select_value == $author['id'] ? "selected" : '';?> >
<?php htmlout($author['name']);?>
</option>
<?php endforeach; ?>
</select>
</div>
Proper value of selected attribute is selected. Thus if you wish to apply this attribute to the option that you consider currently selected, try this (I assume that PHP variable that stores selected author's id is $authorid):
<div>
<label>Select author here:</label>
<select name="author">
<option value="">Select an author</option>
<?php foreach ($authors as $author): ?>
<option value="<?php htmlout($author['id']);?>"
<?php if ($author['id'] == $authorid) echo ('selected="selected"'); ?>
>
<?php htmlout($author['name']);?>
</option>
<?php endforeach; ?>
</select>
</div>
I looked at several community forums, and I am unable to figure it out on how to retain select option value after validation fails.
Here is the code that works for me, but values disappear when submit button is submitted.
<select id="service" name="service" class="searchoption">
<option value="">-- Select Service Name --</option>
<?php
$resultservice = mysqli_query($con,"Select * from services") ?>
<?php
while ($line = mysqli_fetch_array($resultservice)) {
?>
<option value="<?php echo $line['serviceid'];?>"> <?php echo $line['service'];?> </option>
<?php
}
?>
</select>
Here is what I tried and doesn't work for me:
<select id="service" name="service" class="searchoption">
<option value="">-- Select Service Name --</option>
<?php
$resultservice = mysqli_query($con,"Select * from services") ?>
<?php
while ($line = mysqli_fetch_array($resultservice)) {
?>
<option value="<?php echo $line['serviceid']; if ($_POST['service'] == $service) {echo 'selected="selected"'} echo $line['serviceid']; ?>"> <?php echo $line['service'];?> </option>
<?php
}
?>
</select>
<form action="" method="POST">
<select name="list" id="list">
<option value="item1">item1</option>
<option value="item2">item2</option>
<option value="item3">item3</option>
</select>
<input type="submit" />
</form>
<script type="text/javascript">
document.getElementById('list').value = "<?php echo $_POST['list']?>";
</script>
May be a small error but noticed a invalid > in the following code
<option value="<?php echo $line['serviceid']; if ($_POST['service'] == $service) {echo 'selected="selected"'} echo $line['serviceid']; ?>"> <?php echo $line['service'];?> </option>
Try this
<option value="<?php echo $line['serviceid']; if ($_POST['service'] == $service) {echo 'selected="selected"'}?> echo $line['serviceid']; <?php echo $line['service'];?>" </option>
hope this helps