HTML define what selection option is active - php

I have a select box with the following (it has been shortened):
<select id="viewSelector" name="when" style="width:92%;">
<option value="USA">USA</option>
<option value="Australia">Australia</option>
<option value="Germany">Germany</option>
</select>
If the user logs into the control panel and wants to change his country, he gets presented with this form. My problem is that everytime USA is the default and I can not change this depending on the users country. For example, the user lives in Australia. He wants to change his country to USA and goes to this page. I want the country that is displayed to be Australia. Let me know if this makes sense.

To preselect a value, just add the selected attribute to the desired option.
<select id="viewSelector" name="when" style="width:92%;">
<option value="USA">USA</option>
<option value="Australia" selected="selected">Australia</option>
<option value="Germany">Germany</option>
</select>
This will preselect Australia for example.

You need to add a selected attribute to the option you want to select, as described in the spec.
<select id="viewSelector" name="when"">
<option value="USA">USA</option>
<option value="Australia" selected="selected">Australia</option>
<option value="Germany">Germany</option>
</select>
In your script, you need to emit this attribute for whatever default you want to display.

Use the selected attribute of the option tag.
<select id="viewSelector" name="when" style="width:92%;">
<option value="USA">USA</option>
<option value="Australia">Australia</option>
<option selected value="Germany">Germany</option>
</select>

Use selected on the <option> tag that names the user's current country:
<option value="Australia" selected="selected">Australia</option>
So in PHP:
<select>
<?php
$countries = array('USA', 'Australia', 'Germany');
$current_country = 'Australia';
foreach($countries as $country) {
if($country == $current_country) {
echo '<option selected="selected">'.$country.'</option>';
} else {
echo '<option>'.$country.'</option>';
}
}
?>
</select>

The html looks like:
<option selected value="Australia">Australia</option>
In the php loop that builds this html, compare the current option name with the user's current country (retrieved from a database or where-ever). When the two match, add selected

A bit shorter:
foreach($paises as $key=>$value)
{
if($key == "PE"){$activo = ' selected="selected" '; }else{$activo = "";}
echo "<option $activo value=\"$key\">$value</option>";
}

Related

jquery.validationEngine.js dropwdown

Im using jquery.validationEngine.js everything is setup and working fine, however I need to make a change.
I have two dropdown boxes.
Is The Vehicle Imported? [yes, no]
What Is The Country Of Origin? [France, Germany, Japan, USA, UK] etc
What I require is that if the car is imported then to show the country of origin, else display nothing.
At the moment I have both fields working on a live form,
<label>Is The Vehicle Imported?</label>
<select class="validate[required] target" name="strLeadData39" id="strLeadData39">
<option selected="selected" value="">Please Select</option>
<option value="No">No</option>
<option value="Yes">Yes</option>
</select>
<label>What Is The Country Of Origin?</label>
<select class="validate[required] target" name="strLeadData40" id="strLeadData40" >
<option selected="selected" value="">Please Select</option>
<option value="Not Imported">Not Imported</option>
<option value="France">France</option>
<option value="Germany">Germany</option>
<option value="Japan">Japan</option>
<option value="Canada">Canada</option>
<option value="USA">USA</option>
<option value="EU-Other">EU-Other</option>
<option value="Other">Other</option>
</select>
How do Ш complete this?
Thanks
So you want to show/hide the country select depending on the value of the imported select?
$('#strLeadData39').change(function(){
if ( $(this).val() == "Yes" ){
$('#strLeadData40').show();
} else {
$('#strLeadData40').hide();
}
});
Here is a fiddle with a basic example
The above code has nothing to do with jQuery validationEngine,just hiding/displaying the element. To make the form validate you will probably have to dynamically remove/add the validate[required] class using jQuery removeClass/addClass, something inside the if/else like:
$('#strLeadData40').removeClass("validate[required]");

How to keep showing selected option from drop down list?

I have a drop down list where I select options
<form action="" method="POST" class="styled-select">
<select name="seasons" onchange='this.form.submit()'>
<option value="">Select a Season</option>
<option value="1">2002/2003</option>
<option value="2">2003/2004</option>
<option value="3">2004/2005</option>
<option value="4">2005/2006</option>
<option value="5">2006/2007</option>
<option value="6">2007/2008</option>
<option value="7">2008/2009</option>
<option value="8">2009/2010</option>
<option value="9">2010/2011</option>
<option value="10">2011/2012</option>
<option value="11">2012/2013</option>
<option value="12">2013/2014</option>
</select>
<noscript><input type="submit" value="Submit"></noscript>
</form>
You can see the list here footystat
I am using the following PHP
if(isset($_POST['seasons'])){ $seasonette = $_POST['seasons']; }
if(isset($_POST['year'])){ $yearette = $_POST['year']; }
if(isset($_POST['comp'])){ $competitionette = $_POST['comp']; }
if(isset($_POST['which'])){ $whichette = $_POST['which']; }
When I select something from the list, I want selected item in the list to continue showing. At the moment when I select (for example) 2013/2014, it will show the results but the drop down menu goes back to its original state instead of showing 2013/2014.
Get Option value selected when it gets posted value, like this,
<option value="1" <?php if(isset($_POST['seasons']) && $_POST['seasons'] == '1'){ ?> selected="selected" <?php } ?>>2002/2003</option>
Set value like this for each option
You can set the "selected" property to the option , just like you set a value !
<option value="8" selected>2009/2010</option>
Use a if statement in PHP to determine which one should be selected.
Thats because the page refreshes.
On page load check if there is post variable than match the value with each option's HTML and write selected attribute.
The shorter way is
<option value="1" <?php echo $_POST['seasons']==1?"selected":""; ?>2002/2003</option>

Retain drop-down list values in PHP without using array

I have a drop-down list containing all the countries in the world. The first few countries are:
Country: <select name="Country">
<option value="Afghanistan">Afghanistan</option>
<option value="Aland Islands">Aland Islands</option>
<option value="Albania">Albania</option>
<option value="Algeria">Algeria</option>
<option value="American Samoa">American Samoa</option>
<option value="Andorra">Andorra</option>
<option value="Angola">Angola</option>
<option value="Anguilla">Anguilla</option>
</select>
Forming an array would require over 200 elements. Is there a way to retain the drop-down value in PHP after a form post without using an array? Thank you.
You really are best using an array, it won't take any real noticable time to iterate through 200 items
Something like this should keep the selected item after post
<select name="country" method="POST">
<?php
for($i=0;$i<count($countries);$i++)
{
$selected="";
if($countries[$i]==$_POST['country'])
{
$selected="selected";
}
?><option <?php echo $selected;?> value="<?php echo $countries[$i];?>"><?php echo $countries[$i];?></option><?php
}
?>
</select>

to get selected value in select tag

<div>
<select name="mySelect" id="mySelect" class="select" onchange="if(this.options[this.selectedIndex].value != ''){window.top.location.href=this.options[this.selectedIndex].value}">
<option value="english.php">English</option>
<option value="french.php">French</option>
<option value="spanish.php">Spanish</option>
</select>
</div>
i want to change language it work fine when i change the language but in dropdown it always shows english
Add a selected attribute to the option you want to be selected when the page loads.
You need to add another tag that is blank.
Working example:
http://jsfiddle.net/XYSXA/
<select name="mySelect" id="mySelect" class="select" onchange="if(this.options[this.selectedIndex].value != ''){window.top.location.href=this.options[this.selectedIndex].value}">
<option value=""></option>
<option value="english.php">English</option>
<option value="french.php">French</option>
<option value="spanish.php">Spanish</option>
</select>​
This way none of the options will be selected by default.
Alternatively, you may use the set the attribute selected inside one of the opening <option> tags to specify which language should be selected by default. Example selecting French by default:
<select name="mySelect" id="mySelect" class="select" onchange="if(this.options[this.selectedIndex].value != ''){window.top.location.href=this.options[this.selectedIndex].value}">
<option value=""></option>
<option value="english.php">English</option>
<option value="french.php" selected>French</option>
<option value="spanish.php">Spanish</option>
</select>​
Working example: http://jsfiddle.net/XYSXA/1/
<?
$file=basename($_SERVER['PHP_SELF'],".php");
$file=ucwords($file);
?>
<option value="french.php" <?=($file=="French")?" selected='selected'":""?>>French</option>
I m assuming that you're using the pattern like French for french.php, Spanish For spanish.php,etc

optgroup get in php label associated

I have following code in a html form
<select name="category" class="input" onchange="ShowTB(this,'suggest');">
<option value="0" selected="selected">
[choose yours]
</option>
<optgroup label="Item">
<option value="SubItem1"SubItem1</option>
<option value="SubItem2">SubItem2</option>
</optgroup>
<option value="Item2">Item2</option>
<optgroup label="Item3">
<option value="SubItem4"SubItem4</option>
<option value="SubItem5">SubItem5</option>
</optgroup>
<option value="Item4">Item4</option>
<option value="Item5">Item5</option>
<option value="Item6">Item6</option>
<option value="Item7">Item7</option>
</select>
in php i get the value of field selected with:
$category = $_POST['category'];
in this mode if i select in the form ie: SubItem1 , in php i get value SubItem1 but i want also get associated label ie: Item or if i select SubItem5 i get SubItem5 but i want also get associated label ie: Item3
How to ?
Indeed, you only get the value. If you need more, just encode whatever you want into the value, for example:
<option value="Item3.SubItem5">SubItem5</option>
Alternatively, you could use javascript to catch onChange events on the select field and update a hidden input field with the desired label.
you could make the values arrays e.g.
<option value="Item3[SubItem5]">SubItem5</option>
so then your $_POST['category'] should return an array

Categories