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">
Related
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
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>
I'm collecting data from a form into a session, on another form, I use a few variables to feed a SELECT option, which works as I want. as shown here
<select class="select2_category form-control" data-placeholder="Choose a Category" tabindex="1" id="householder" name="householder">
<option value=""></option>
<option value="A"><?php echo $_SESSION["name1"];?></option>
<option value="B"><?php echo $_SESSION["name2"];?></option>
<option value="C"><?php echo $_SESSION["name3"];?></option>
<option value="D"><?php echo $_SESSION["name4"];?></option> </select>
My issue is how I can change this, currently if within the form, any names are left blank then when it comes to the SELECT, there is a blank line for every option blank, is there a way to change this and if a variable is blank then ignore and use the next variable, this would then make my SELECT look tidy
First of all you should consider using an array instead of a series of uniquely named session variable names, e.g.:
$_SESSION['names'] = array('A' => 'First', 'B' => 'Second');
Then you can loop over them with foreach instead. Also the benefit of that would be you can use the array_filter function (documentation) to filter out empty values from your array of names before looping over them:
<select class="select2_category form-control" data-placeholder="Choose a Category" tabindex="1" id="householder" name="householder">
<option value=""></option>
<?php
$options = array_filter($_SESSION['names']);
foreach ($options as $option_value => $option_name): ?>
<option value="<?php echo $option_value; ?>"><?php echo $option_name;?></option>
<?php endforeach; ?>
</select>
<?php if (isset($_SESSION["name1"]) && $_SESSION["name1"]!='') {?><option value="A"><?php echo $_SESSION["name1"];?></option><?php } ?>
this for every line/option of your select
use an if statement to check if your variable exists.
<?php if (isset($_SESSION["name1"])) : ?>
<option value="C"><?php echo $_SESSION["name1"];?></option>
<?php endif; ?>
I have this HTML piece of code already read into $html. I had extracted some correct information, but I'm stuck getting the selected option value of a select.
<select name="a" id="selstart">
<option value="00">Jan</option>
<option value="01">Feb</option>
<option value="02">Mar</option>
<option value="03">Apr</option>
<option value="04">May</option>
<option value="05">Jun</option>
<option selected value="06">Jul</option>
<option value="07">Aug</option>
<option value="08">Sep</option>
<option value="09">Oct</option>
<option value="10">Nov</option>
<option value="11">Dec</option>
</select>
and need to extract the value "06" into a variable.
I tried:
foreach($html->find('select') as $element) {
if ($element->id == 'selstart')
{
$v = $element->find('option selected',0)->value . '</br>';
}
}
and many other combinations following the idea found in php , simple_html_dom.php, get selected option but did not work.
Any ideas?
Use element[attr] to select elements with the specified attribute.
$v = $element->find('option[selected]', 0)->value . '</br>';
Thank you, for your reply, if you want to set option values after reading the select you can do like this:
First loaded the html select from another file for example,
It is something like this:
<div>
<select id="select1">
</select>
</div>
Then you can do like this:
<?php
$html=str_get_html($filename_or_method_to_the_html_content);
$products='<option></option>'.
'<option val="1">Val 1</option>'.
'<option val="2" selected="selected">Val 2</option>'.
'<option val="3">Val 3</option>';
$html->find('select[id=select1]','0')->innertext=$products;
?>
It worked well for me ;)
try this simple code
$element = $html->find('#selectIDGoesHere',0)->find('option');
foreach($element as $elemen) {
echo "Display text:".($elemen->plaintext)."<br>";
echo "value:".($elemen->value)."<br>";
}
I'm using search with combo box
Here is my combo box source code
<select name="salary" class="styled">
<option selected="selected" value=''>Any</option>
<option value="10000">10,000</option>
<option value="20000">20,000</option>
<option value="30000">30,000</option>
<option value="40000">40,000</option>
<option value="50000">50,000</option>
<option value="60000">60,000</option>
<option value="70000">70,000</option>
<option value="80000">80,000</option>
<option value="90000">90,000</option>
<option value="100000">100,000</option>
<option value="110000">110,000</option>
<option value="120000">120,000</option>
<option value="130000">130,000</option>
<option value="140000">140,000</option>
<option value="150000">150,000</option>
</select>
I would like to select value based on $salary=$_GET['salary']; if $salary empty I need to select first one as selected
To set a default value for an HTML select element, you need to give the appropriate <option> element the selected attribute. It would look like this:
<option value='50000' selected='selected'>50,000</option>
In your PHP code, you would add this by setting a string for each option, to either "selected='selected'" or blank, depending on whether that option is the one you want to have selected.
This is obviously far easier to put into a loop rather than writing the same code twenty times, so you would need to rewrite your output to create a loop for creating your options.
Hope that helps.
You can do something like this on creation...[not tested]
<?php
$options = array(
'10000' => '10,000',
...
'150000' => '150,000'
); //From MySQL
array_unshift($options, '' => 'Any');
$salary = isset($_GET['salary'])?$_GET['salary']:'';
?>
<select name="salary" class="styled">
<?php foreach ($options as $value=>$text){ ?>
<option <?php if($value == $salary){echo 'selected="selected"'; }?> value="<?php echo $value; ?>"><?php echo $text; ?></option>
<?php } ?>
</select>
If I understand your question correctly you want that if the person doesn't select any thing you want 10000 to be automatically selected for him. And I also assume that the options in the select list are static. So..
<?php
if($_GET['salary']=='')
$salary = 10000;
else
$salary = $_GET['salary'];
?>