How to use select option value - php

Im trying to figure out how I can use both standard select option value together with a while loop select option value from mysql database. Right now I have this but won´t work any suggestions?
<option value="All">All</option>
<option value="<?php echo $row_list['name'] ;?>"><?php echo $row_list['name'];?></option>

<select ...>
<option value="standard">...</option>
<?php while(...) { output more options here } ?>
</select>

You can try this:
<?php
$cars=array("Mercedes", "Audi");//Let's have simple arrray with two objects
?>
<select>
<option value="All"> All</option>
foreach($cars as $car){//Let's iterate through array elements
?>
<option value="<?php echo $car;?>"> <?php echo $car;?> </option>
<?php
}
?>
</select>

Related

How to create a PHP dropdown list that allows you to select multiple options?

This is the basis for the code I'm using, drawing options from a database but no matter how I alter this code I will only let me select 1 option or return an error.
<select name="sargentid" id="fieldsargentid" class="form-control">
<?php foreach ($sargent as $sargent) { echo "<option value='" . $sargent->getID() .
"'>$sargent</option>"; }?>
</select>
<select name="sargentid" id="fieldsargentid" class="form-control" multiple=multiple>
<?php
foreach($sargent as $sargentKey => $sargentList){
$values = $sargentList['NameofTheRowInTable'];
?>
<options value = <?php echo $values; ?> ><?php echo $values;?></options>
<?php
}
?>
</select>
A drop-down list that allows multiple selections using the multiple attribute:
<select id="animals" name="animal" multiple>
<option value="cat">Cat</option>
<option value="dog">Dog</option>
<option value="mouse">Mouse</option>
<option value="lion">Lion</option>
</select>
Hold down the Ctrl (windows) / Command (Mac) button to select multiple options.
Demo: https://jsfiddle.net/prk6c9q7/
<select name="sargentid" id="fieldsargentid" class="form-control" multiple>
<? foreach($argent as $data){?>
<option value="<?=$data->getID() ?>"><?= $data ?></option>
<?php } ?>
</select>
Try This

PHP: How to display select options if no value from database

I am trying to display my dropdown with a value from the database, but if the value is null I want it to show my options.
Currently it keeps showing me the blank select option.
<select class="form-control col-sm-5" id="freqlevels" name="freqlevels" value="<?php if ($customerinfo['freqlevel']) { echo h($customerinfo['freqlevel']);} else { echo "" ; } ?>"">
<option value=""></option>
<option value="Twice Weekly">Twice Weekly</option>
<option value="Weekly">Weekly</option>
<option value="Fortnightly">Fortnightly</option>
<option value="Monthly">Monthly</option>
</select>
Please can you suggest what I should do?
put your condition outside the value
<?php if ($customerinfo['freqlevel']) { echo value="$customerinfo['freqlevel']";}
hope this will resolve your problem
You need to make use of conditional statements.
<select name="something" id="my-select">
<option value="0">Everyone can see me</option>
<?php if (empty($array['some_key'])) : ?>
<option value="1">I'm only if some_key is empty</option>
..etc..
<?php endif; ?>
</select>
Then you can check values against the option value:
<option value="<?php echo $key; ?>"
<?php echo ($key === $_POST['some_key'] ? 'selected' : ''); ?>>
Hello, world
</option>

prevent double datas in select box

Here is my HTML + PHP :
<select name="type" class="form-control form-update-user" id="type" tabindex=1>
<option selected="selected"><?php echo $thirds['type']; ?></option>
<option value="CLIENT">CLIENT</option>
<option value="AFFRETE">AFFRETE</option>
<option value="DEPOT">DEPOT</option>
</select>
I am using JQUERY as Framework for javascript.
My problem is probably simple. But how can I prevent selectbox to have the same data multiple times? For example, if
$third['type'] = "AFFRETE"
Then, I will have one time AFFRETE as selected value, and again this value in my select box. I tried to remove the selected value with ready function in javascript :
<script type="text/javascript">
$(document).ready(function(){
$("#type option:selected").remove();
})
</script>
But then I have another value selected... logical.
Thank you in advance.
You can do this directly in PHP. Check the array value before adding the element as an option:
<select name="type" class="form-control form-update-user" id="type" tabindex=1>
<?php if (!in_array($thirds['type'], array('CLIENT', 'AFFRETE', 'DEPOT'))): ?>
<option selected="selected"><?php echo $thirds['type']; ?></option>
<?php endif; ?>
<option value="CLIENT">CLIENT</option>
<option value="AFFRETE">AFFRETE</option>
<option value="DEPOT">DEPOT</option>
</select>
Or maybe you're looking for:
<?php $options = array('CLIENT', 'AFFRETE', 'DEPOT'); ?>
<select name="type" class="form-control form-update-user" id="type" tabindex=1>
<?php foreach ($options as $option): ?>
<option value="<?php echo $option; ?>"
<?php if ($option === $thirds['type']): ?>
selected="selected"
<?php endif;
><?php echo $option; ?></option>
<?php endforeach; ?>
</select>
Try doing this:
<select name="type" class="form-control form-update-user" id="type" tabindex=1>
<option value="CLIENT" <?php echo $thirds['type']=='CLIENT'?'selected=selected':''; ?>>CLIENT</option>
<option value="AFFRETE" <?php echo $thirds['type']=='AFFRETE'?'selected=selected':''; ?>>AFFRETE</option>
<option value="DEPOT" <?php echo $thirds['type']=='DEPOT'?'selected=selected':''; ?>>DEPOT</option>
</select>
Remember that this is a quick and dirty way to do this. Prefer making a loop creating the options, instead of hardcoding it.

value="<?php echo htmlspecialchars($_POST['whatever']); ?>" not working in select tag

my code is written below
<select name="year" required value="<?php echo htmlspecialchars($_POST['year']); ?>" >
<option>----SELECT----</option>
<option value="1">1<sup>st</sup></option>
<option value="2">2<sup>nd</sup></option>
<option value="3">3<sup>rd</sup></option>
<option value="4">4<sup>th</sup></option>
</select>
htmlspecialchars() is not working for select tag.is there any mistake or there is some other way to do it
if you want to set the default value of a select you have to set the option that contains this value as selected like this:
<select name="year" required="required">
<option>----SELECT----</option>
<option <?php if((int)($_POST['year'])==1){?>selected<?php } ?> value="1">1<sup>st</sup></option>
<option <?php if((int)($_POST['year'])==2){?>selected<?php } ?> value="2">2<sup>nd</sup></option>
<option <?php if((int)($_POST['year'])==3){?>selected<?php } ?> value="3">3</sup>rd<sup></b></option>
<option <?php if((int)($_POST['year'])==4){?>selected<?php } ?> value="4">4<sup>rth</sup></option>
</select>
You don't assign a value attribute to a select tag; you assign it to option tags. It's not clear what you are trying to do, but <select ... value="..."> is just invalid HTML. See the specification.
you cannnot give value in <select> tag. Value is given in <option> tag.
like
<select name="year">
<option>----SELECT----</option>
<option value="<?php echo htmlspecialchars($_POST['year']); ?>">
<?php echo htmlspecialchars($_POST['year']); ?></option>
<option value="1">1<sup>st</sup></option>
<option value="2">2<sup>nd</sup></option>
<option value="3">3</sup>rd<sup></b></option>
<option value="4">4<sup>rth</sup></option>
</select>

Multiple PHP dropdowns and getting the POSTed value from the array

I have , within a foreach , a dropdown:
`<select name="position[]">
<option value="1st">First</option>
<option value="2nd">Second</option>
<option value="3rd">Third</option>
</select>`
I need to be able to get the values from position[]when the form is posted
I had assumed it was $_POST['position'][0] , $_POST['position'][1] etc.
But that doesn't work .
Try this:
<?php
foreach($array as $key=>$value){ ?>
<select name="position[<?php echo $key; ?>]">
<option value="1">First</option>
<option value="2">Second</option>
<option value="3">Third</option>
</select>
<?php } ?>
You should then be able to access each select like this:
$_POST['position'][$key]
You have not included multiple in html code of select.
You should use
<select name="name[]" multiple size"5">
Try this:
$test=$_POST['position'];
if ($test){
foreach ($test as $t){
echo 'You selected '.$t.'<br />';
}
}
and also in select tag enable multiple selection by:
<select name="position[]" multiple="multiple">

Categories