Convert a JS array into JSON n display it - php

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>

Related

How the proper way to explode array from database make it as option in dropdown in Codeigniter

i have array, and want to explode the array as option in dropdown, here is the table
id value option
K1 1,2,3 AAA,BBB,CCC
K2 1,2,3 DDD,EEE,FFF
i want the output like this
<div class="form-group">
<label for="val[]">Parameter K1</label>';
<select class="form-control" name="val[]">
<option value="" selected="" disabled="" hidden="">Choose one</option>
<option value="1">AAA</option>
<option value="2">BBB</option>
<option value="3">CCC</option>
</select>
</div>
<div class="form-group">
<label for="val[]">Parameter K2</label>';
<select class="form-control" name="val[]">
<option value="" selected="" disabled="" hidden="">Choose one</option>
<option value="1">DDD</option>
<option value="2">EEE</option>
<option value="3">FFF</option>
</select>
</div>
If I understood correctly, you want to suppress the data in a table with php in this way.
I wrote a mysqli query for you, it's $query variable.
$query = mysqli_query($db_connect,"SELECT * FROM tbl_variables");
Then I wrote this query with the while loop with the help of mysqli_fetch_object and read it line by line.
<?php
while ($row = mysqli_fetch_object($query)){
echo '
<option value="'?><?php echo $row->optionValue; ?><?php echo'">'?><?php echo $row->optionText; ?><?php echo'</option>
';
}
?>
All code parts;
<?php
$query = mysqli_query($db_connect,"SELECT * FROM tbl_variables");
?>
<div class="form-group">
<label for="val[]">Parameter K1</label>';
<select class="form-control" name="val[]">
<option value="" selected="" disabled="" hidden="">Choose one</option>
<?php
while ($row = mysqli_fetch_object($query)){
echo '
<option value="'?><?php echo $row->optionValue; ?><?php echo'">'?><?php echo $row->optionText; ?><?php echo'</option>
';
}
?>
</select>
</div>

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.

sql queries on ajax php page

i am trying to get content inside a div using ajax, on the php page that am accessing using ajax, i want to fill dropdown options using another query but it just closes the select tag before the options.
<select class="form-control data" data-t="integer" name='role' required/>
<option value="">--Select--</option>
<?php
foreach($con -> query("select id,name,permissions from role") as $role){
?>
<option value="<?php echo $role['id']; ?>"><?php echo $role['name']; ?></option>
<?php
}
?>
</select>
But here is what i get inside the div
<select class="form-control data" data-t="integer" name="role"
required=""></select>
<option value='1' >option1</option>
<option value='2' >option2</option>
<option value='3' >option3</option>
please remove forward / at the end of opening select tag as below:
<select class="form-control data" data-t="integer" name='role' required>
<option value="">--Select--</option>
<?php
foreach($con -> query("select id,name,permissions from role") as $role){
?>
<option value="<?php echo $role['id']; ?>"><?php echo $role['name']; ?></option>
<?php
}
?>
</select>

Drop-down list in HTML with options based on php rows

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 } ?>

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