using variables within a SELECT option php - php

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

Related

unable to get value from select tag to $_POST array

i have this in my from, i am getting values from other input tags and check boxes but not getting value from select tag in $_POST array
here is the code
<select class="selectpicker form-control" name="fcity" value="<?php echo $res['f_city'];?>" data-live-search="true" data-width="100%" title="Choose one of the following...">
<option data-tokens="Lahore" <?php echo ($res['f_city']=='Lahore')?'selected':'' ?>>Lahore</option>
<option data-tokens="Islamabad" <?php echo ($res['f_city']=='Islamabad')?'selected':'' ?>>Islamabad</option>
<option data-tokens="Karachi" <?php echo ($res['f_city']=='Karachi')?'selected':'' ?>>Karachi</option>
</select>
I think you need to put the 'value' attribute on each 'option' tag instead of in the 'select' tag
e.g.:
<option value="Lahore" data-tokens="Lahore" <?php echo ($res['f_city']=='Lahore')?'selected':'' ?>>Lahore</option>

Html Select Box Get value from mysql

Hi I have a stored value in mysql from a select box. When i call a edit page I would like the select box to populate with the value i have stored in mysql
$taskCompany = $mysqli->real_escape_string($_POST['taskCompany']); This returns 1
<select id="taskCompany" required name="taskCompany" class="form-control">
<option value="" disabled selected>Select a Company</option>
<option value="1">building</option>
<option value="2">maintenace</option>
</select></div>
I would like the select box to show the correct option when loading the edit page. I do not store any select option in the database values or text just the value that is select when job was created.
Thanks Jon
I'd I have understood correctly, you have he value/values in your MySQL database and you want them to be shown in your drop down list:
You could do it like:
<option value = "1" <?php if($taskCompany == 1) echo "select='selected'";?>>building</option>
Or if you have the names coming from the database too.
<select id="taskCompany" required name="taskCompany" class="form-control">
<option>SELECT</option>
<?php
$res = musql_query("SELECT * FROM yourtablename");
while($row = mysql_fetch_array($res))
{
$taskCompany = $row['taskCompany'];
$co_name = $row['taskCompanyName'];
?>
<option value = "<?php echo $taskCompany; ?>" ><?php echo co_name; ?></option>
<?php } ?>
Here I have used $co_name to be displayed as a name assuming that you have building, maintenance etc stored in your database.
This is for the time being, as I wanted to demonstrate but Please
consider mysqli/PDO as mysql_* is deprecated.
<?php $taskCompany = $mysqli->real_escape_string($_POST['taskCompany']); ?>
<select id="taskCompany" required name="taskCompany" class="form-control">
<option value="" disabled selected>Select a Company</option>
<option value="1" <?php if($taskCompany == 1) echo "selected='selected'"; ?>>building</option>
<option value="2" <?php if($taskCompany == 2) echo "selected='selected'"; ?>>maintenace</option>
</select></div>
You can do this
<?php foreach($taskCompany as $row){ ?>
<option value="unique id for select options">name for select options</option>
<?php } ?>

How to use select option value

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>

select combo box based on get value

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'];
?>

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