How to insert a PHP script into attribute "selected" (HTML 5) - 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>

Related

how to show the selected value from database in select box in codeigniter

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.

How to set_value On Dropdown Form Codeigniter

I Have this form,but whenever the form_validation return FALSE. The Dropdown form always set to default which was PLEASE SELECT.
How do i Set the value like the input form,i dont know where to put the set_value
Dropdown Form:
<div class="form-group">
<select class="form-control form-control-sm" id="exampleFormControlSelect1" name="jenisKelamin">
<option value="">PLEASE SELECT</option>
<?php foreach ($system as $d) : ?>
<?php if ($d->system_type=="JENIS_KELAMIN"): ?>
<option value="<?= $d->system_value_txt ?>"><?= $d->system_value_txt ?></option>
<?php endif ?>
<?php endforeach; ?>
</select>
<small class="text-danger"><?= form_error('jenisKelamin') ?></small>
</div>
To set a default dropdown menu, you could use the set_select() method on your option input like this :
<div class="form-group">
<select class="form-control form-control-sm" id="exampleFormControlSelect1" name="jenisKelamin">
<option value="">PLEASE SELECT</option>
<?php foreach ($system as $d) : ?>
<?php if ($d->system_type=="JENIS_KELAMIN"): ?>
<option value="<?= $d->system_value_txt ?>" <?php echo set_select('jenisKelamin', $d->system_value_txt); ?> ><?= $d->system_value_txt ?></option>
<?php endif ?>
<?php endforeach; ?>
</select>
<small class="text-danger"><?= form_error('jenisKelamin') ?></small>
</div>

Update Dropdown Value

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;?>"

Convert a JS array into JSON n display it

I am new in programming i just want that the value i choose in drop downs must retain selected when i click the search button ...
here is the code
<select name="campaigntype" id="campaigntype" class="text dropDownNewsletter" onchange="GetCampaigns()" style="float:left; margin-right:15px;">
<option value="">Select Campaign</option>
<option value="1" >OB Campaign</option>
<option value="2">Casbo Campaign</option>
</select>
<div id="selected_campaigns" style="float:left">
<select name="campaignid" id="campaignid" class="text dropDownNewsletter">
<option value="">Select Campaign</option>
<?php foreach($campaigns as $key=>$row){?>
<option value="<?php echo $row['id'];?>" <?php if( $row['id'] == $campaign_id) {?> selected="selected" <?php } ?> ><?php echo $row['id'].'-'.$row['title'];?></option>
<?php }?>
</select>

How to retain Select option after submitting the form in php

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

Categories