Dropdown form with selected value from variable - php

I have the current theme for my script stored in my config file (JQUERY_THEME) how can i make that the preselected value in the dropdown list.
$jquerytheme = JQUERY_THEME;
$themes = array("base", "black-tie", "blitzer", "cupertino", "dark-hive", "dot-luv", "eggplant", "excite-bike", "flick", "hot-sneaks", "humanity", "le-frog", "mint-choc", "overcast", "pepper-grinder", "redmond", "smoothness", "south-street", "start", "sunny", "swanky-purse", "trontastic", "ui-darkness", "ui-lightness", "vader");
echo "<p><select name=\"jquerytheme\">";
foreach ($themes as $value) {
echo "<option value=\"$value\">". ucfirst($value) . "</option>";
}
echo "</select></p>";

If you want a option preselected you must use the selected attribute in the option:
echo "<p><select name=\"jquerytheme\">";
foreach ($themes as $value) {
$selected = ($value == $jquerytheme) ? 'selected="selected"' : '';
echo "<option $selected value=\"$value\">". ucfirst($value) . "</option>";
}
echo "</select></p>";

Related

Isset with multi select dropdown and bootstrap

I have a wordpress site with a search form that searches for posts based on the form field selections, for custom fields etc. It works fine, however on the search results page I have an exact copy of the form except that I am trying to preset the form selections based on the search query/url string.
I am using a regular select dropdown and I have set it to "multiple" so that it can use bootstraps multiselect with the checkboxes. I've asked a similar question HERE but that was for checkboxes and even though the bootstrap multiselect uses checkboxes, I still have to work with the select dropdown first.
So after trying several things, I've come close but have ran into several problems. In the code below I made notes to further explain exactly what I mean.
<select name="property_type[]" id="pt-multi" class="form-control multi-select2" multiple="multiple">
<?php
$terms = get_terms( "property-type", array( 'hide_empty' => 0 ) );
$count = count($terms);
if ( $count > 0 ){
echo "<option value='Any'>All</option>";
foreach ( $terms as $term ) {
if (isset($_GET['property_type'])) {
foreach ($_GET['property_type'] as $proptypes) {
// FIRST EXAMPLE
$selected .= ($proptypes === $term->slug) ? "selected" : ""; // shows first correct selected value but also selects everything after it up until the second correct value, which it doesn't select.
//$selected = ($proptypes === $term->slug) ? "selected" : ""; // shows only last correct selected value
//if ($proptypes === $term->slug) { $selected = 'selected'; } // shows first correct selected value then selects every value after, even if it wasn't selected
// SECOND EXAMPLE
//$selected .= ($proptypes === $term->slug) ? "selected" : ""; // shows first correct selected value then selects every value after, even if it wasn't selected
//$selected = ($proptypes === $term->slug) ? "selected" : ""; // shows only last correct selected value
//if ($proptypes === $term->slug) { $selected = 'selected'; } // shows first correct selected value then selects every value after, even if it wasn't selected
}
}
echo "<option value='" . $term->slug . "' " . $selected . ">" . $term->name . "</option>"; // FIRST EXAMPLE
//echo "<option value='" . $term->slug . "' " . ($selected?' selected':'') . ">" . $term->name . "</option>"; // SECOND EXMAPLE
}
}
?>
</select>
Create and array and use in_array() to check.
<select name="property_type[]" id="pt-multi" class="form-control multi-select2" multiple="multiple">
<?php
$terms = get_terms("property-type", array('hide_empty' => 0));
$count = count($terms);
// Setup an array of $proptypes
$proptypes = array();
if (isset($_GET['property_type'])) {
foreach ($_GET['property_type'] as $proptype) {
$proptypes[] = $proptype;
}
}
if ($count > 0) {
echo "<option value='Any'>All</option>";
foreach ($terms as $term) {
$selected = (in_array($term->slug, $proptypes)) ? 'selected' : '';
echo "<option value='" . $term->slug . "' " . $selected . ">" . $term->name . "</option>";
}
}
?>
</select>

selecting option from select element with php

I have a database field country which I want to query and select that country option from a select element. Is there any way to do this without adding:
if (query->country == "<some country>"){echo "selected"}
in every single option tag? As there are hundreds of country options. Here is a little example of the code. Thank you.
$query = $query->fetch_object();
// which ever country is held in the variable `$query->country` should be selected
echo"<select>
<option>Afghanistan</option>
......
......
<option>Zimbabwe</option>
</select>";
Don't you have a list of all countries on your server?
$countries = ["Afghanistan", ... , "Zimbabwe"];
You could do something like this:
$selection = "Some country";
echo "<select>";
foreach($countries as $country)
{
if($country == $selection)
echo "<option selected>" . $country . "</option>";
else
echo "<option>" . $country . "</option>";
}
echo "</select>";
The IF still needs to be "placed" on every <option> but as a programmer you should do something like this:
$options = array( 'Afghanistan', '...', 'Zimbabwe' );
foreach( $options as $option )
{
$selected = ( $query->country == $option )? ' selected': '';
echo '<option' . $selected . '>' . $option . '</option>';
}
and if you're unfamiliar with ternary operator, the $selected = ... part above can be written like this:
if ( $query->country == $option )
{
$selected = ' selected';
}
else
{
$selected = '';
}

Country list selection box - Set one as selected

I have a country list of every country in a form to get a parcel quote.
When a user presses the "Get Quote" button, all the text forms retain the information previously entered using PHP.
How can I do this with the country list box? As I can't have PHP on every option checking if that is the country selected and adding "Selected" to the html.
Is there a better way other than generating the country list from a file in a loop?
EDIT:
Going for the method of looping through a file, and checking..
This is what I have so far:
$countries = fopen("includes/countries.txt", "r");
$countries = explode(";", $countries);
Then in the HTML:
<select id="countries" name="countries">
<?php
foreach ($countries as $country){
echo("<option value=\"" . $country . "\">" . $country . "</option>");
}
?>
</select>
Not yet finished.
I assume you have an array with your countries stored. You could try something like this:
$countries = array('Albania', 'Egypt');
$selected_country_id = $_GET['c_id']; // You may need to change this to match with your code
$country_selected = array();
foreach($countries as $country) {
if($country['id'] == $selected_country_id) {
$country_selected[ $country['id'] ] = ' selected ';
} else {
$country_selected[ $country['id'] ] = '';
}
}
Then, assuming that you dynamically add your Select-Options, do this:
// In your each-fn
echo '<option value="' . $country['id'] . '" ' . $country_selected[ $country['id'] ] . '>' . $country['name'] . '</option>';
Something like this would be better
foreach ($countries as $country) {
?>
<option value="<?php echo $country" <?php echo ($country == $_POST['country'] ? 'selected' : ''; ?>><?php echo $country; ?></option>
<?php
}
<select id="countries" name="countries">
<?php
foreach ($countries as $country){
if(isset($_POST["country"]) && $_POST["country"] == $country){
$sel = "selected";
}else { $sel= ""; }
echo("<option value=\"" . $country . "\"" .$sel.">" . $country . "</option>");
}
?>
</select>
Get All Country-State-City Selectbox ....!!!
See Link : GitHub

How to put selected on one particular option inside a foreach loop

I am trying to echo out "selected" on a value inside my array, within a foreach. If the form is false and my customer has already entered a particular value in my select, return it so he doesn't fill it again! That's what I am trying to do...
<?php
$marques = array('Word','Word1','Word20','Word46','Word9797');
foreach ($marques as $marque => $value)
{
if (isset($_POST['marque']) && $_POST['marque'] == $value[$_POST['marque']]) {
echo '<option value="'.$_POST["marque"].'">'.$value[$_POST["marque"]].'</option>';
}
echo '<option value="'.$marque.'">'.$value.'</option>';
}
?>
<?php
$marques = array('Word', 'Word1', 'Word20', 'Word46', 'Word9797');
foreach ($marques as $marque)
echo '<option value="'.$marque.'" '.(($marque == $_POST['marque']) ? 'selected' : '').'>'.$marque.'</option>';
?>
You mean this?
<?php
$marques = array('Word','Word1','Word20','Word46','Word9797');
foreach ($marques as $marque => $value) {
$setItSelected = '';
if (isset($_POST['marque']) && $_POST['marque'] == $marque) {
$setItSelected = 'selected';
}
echo '<option value="'.$marque.'" '.$setItSelected.'>'.$value.'</option>';
}
?>
Changing what's inside the value attribute doesn't make the item more or less selected. You need to add the selected attribute, see http://www.w3schools.com/tags/att_option_selected.asp
The output:ed html should look something like:
<option value="id" selected>some text</option>
<?php
$marques = array('Word','Word1','Word20','Word46','Word9797');
foreach ($marques as $marque)
{
if (isset($_POST['marque']) && $_POST['marque'] == $marque')
echo '<option value="'.$marque.'" selected>'.$marque.'</option>';
else
echo '<option value="'.$marque.'">'.$marque.'</option>';
}
?>
Your Array is one dimension, you can't use "=>"
For select option you must add the "selected" attribute too at your option tag

How to set multiple select options by default from array

I'm trying to retrieve options/values from my database in the from of an array i would like to set these option/values as selected by default in a multiple select list and display them to the user where they will be able to updated their data if necessary.
//data in database
$mytitle = array(
'Arbitrator',
'Attorney',
'Student',
'Other'
);
//data for multiple select
$title = array(
'Judge' ,
'Magistrate' ,
'Attorney' ,
'Arbitrator',
'Title Examiner' ,
'Law Clerk','Paralegal' ,
'Intern' ,
'Legal Assistant',
'Judicial Assistant',
'Law Librarian' ,
'Law Educator' ,
'Attorney',
'Student',
'Other'
);
echo "<select name='title[]' multiple='multiple'>";
$test = implode(',', $mytitle);
for ($i=0; $i<=14; $i++) {
if($test == $title[$i]) {
echo "<option selected value='$title[$i]'>$title[$i]</option>";
}
else {
echo "<option value='$title[$i]'>$title[$i]</option>";
}
}
echo "</select>";
I think you may have a logic error. Try this as your loop:
foreach ($title as $opt) {
$sel = '';
if (in_array($opt, $mytitle)) {
$sel = ' selected="selected" ';
}
echo '<option ' . $sel . ' value="' . $opt . '">' . $opt . '</option>';
}
Use the in_array() function.
for ($i=0; $i<=14; $i++) {
if(in_array($title[$i], $mytitle)){
echo "<option selected value='$title[$i]'>$title[$i]</option>";
}else {
echo "<option value='$title[$i]'>$title[$i]</option>";
}
}
Very simple with the help of jQuery where the select has the id test
$('#test option').attr('selected', 'selected');
JSFiddle Example

Categories