Is it possible to have multiple pre-selected options in a box? Below is my PHP code which makes logical sense to me and appears to output the correct HTML with everything in the right place, but it doesn't actually display with any options selected on the page.
<?php
$options = $wpdb->get_results( $query);
$number = count($options);
$optionsChosen = explode(",", $userInfo->userOptions);
// This is the list of options the user has chosen, separated by commas, i.e. "service1, service2,"
echo "<select name=\"option[]\" id=\"option\" size=\"$number\" multiple>\n";
foreach ( $options as $thisOption ) {
echo "<option value=\"$thisOption->optionName\" title=\"";
echo stripslashes($thisOption->optionDescription)."\" ";
for($i=0; $i<count($optionsChosen); $i++){
if($optionsChosen[$i]==$thisOption->optionName){
echo "selected=\"selected\" ";
} // End if()
} // end for()
echo ">$thisOption->optionName</option>\n";
} // end foreach()
?>
</select>
This is my PHP, and the HTML it's outputting looks fine and includes the selected="selected" in the right place for the appropriate values, but for whatever reason they aren't actually selected. Is this just not a possible thing in HTML? This works fine for me if I only have one possible selection, but I can't seem to find anywhere online mentioning that its only supported for single tags. If this isn't possible, is there a way to achieve something similar?
Edit: As everybody says, it works fine. For whatever reason, refreshing the page (which was built using get variables) in Firefox would refresh the source code but not what was shown inside the browser, closing the page and re-opening it made it work as predicted.
Yes, a <select> tag with multiple attribute can have multiple options with selected="selected" attrubute, which will automatically make these options selected on page load.
Yes Element in HTML can have multiple options selected. If it has multiple attribute.
Below is code
<select multiple>
<option>India</option>
<option selected>USA</option>
<option>Canada</option>
<option selected>China</option>
</select>
Related
Currently i have multiple select tags that have almost the same option choices, what i did now is used file_get_contents to get the value of option tags and assign them to the select tags. but i wanted to create a more efficient way of doing this since when i tried to add an if condition inside the file for the file_get_contents to filter out some extra option tags that should only be available to some select tags it did not work. so i was wondering what is the best approach for this?
basically i used file_get_contents to get the file that contains the basic option tags and assign them to the select tags.
thanks
-magician
Code for some:
some.php
<option>1</option>
<option>2</option>
... you get the picture
for other files.php
<select>
$hello = file_get_contents("some.php");
echo $hello;
</select>
now when i add a condition at some.php ie
if($right=1)
{
this is only for this page
}
//this does not work instead it displays it to all select tags.
in my opinion, file_get_contents is used to retrieve data from a static file (like a txt file), store it into a string and do further processing.
To achieve what you are trying to do, you would need to use the include directive, since you obviously want variables from the first page to be available for the if statement in the second page
As the manual states, "When a file is included, the code it contains inherits the variable scope of the line on which the include occurs. Any variables available at that line in the calling file will be available within the called file"
You need to tell the file generating the options what set of options you want and to do that, use the query string.
So, first have this:
<!-- basic set of options -->
<select>
$hello = file_get_contents("some.php?o=basic");
echo $hello;
</select>
<!-- advanced set of options -->
<select>
$hello = file_get_contents("some.php?o=advanced");
echo $hello;
</select>
<!-- ... you get the picture ... -->
Then in the PHP code check for this query string variable and according to it output proper options:
echo "<option>1</option>"
echo "<option>2</option>"
if ($_GET['o']=="advanced")
{
echo "<option>3</option>"
echo "<option>4</option>"
echo "<option>5</option>"
}
I wonder if it's possible to populate <select> elements with option values generated by PHPto a page (after load) whenever a user is clicking a button withid='add'and then be able to store each value selected as an array then send this via $_POST to a PHP page?
I would like to populate the <select> element like this:
/theloader.php
echo "<select class='plan_id'>\n";
while ($row = mysql_fetch_array($course_elements)) {
echo "<option value='".$row['plan_id']."'>".$row['plan_name']."</option>\n";
}
echo "</select>\n";
?>
This will output
<select class='plan_id'>
<option value='1'>Description</option>
<option value='2'>Description</option>
<option value='3'>Description</option>
</select>
Then when the user has populated the needed elements and selected values, click save and send everything to anotherfile.php:
$("#save").click(function() {
// Store each value as a variable then send to /anotherfile.php
});
I lack knowledge in how to perform this kind of action and would really appriciate som tips in the right direction.
Thank's in advance.
Is there any reason the population of the selects has to be dynamic? Does the data change, or are you just letting the user add more line items to choose from pre-set options?
If the data in the dropdown itself is dynamic, you will need a combination of javascript and ajax calls to get the data from the php in real time.
If the options are not dynamic, then you do not need ajax at all, just populate the variables in javascript and create an action to add more dropdowns using the already defined variables.
I do not know if it makes sense but i am trying to echo a hidden field in select box but does not work. How can i echo
echo "<input name='testt' type='hidden' id='testt' value='".$ver["cats_fee"]."'>";
in here:
<select size="1" name="parentcat">
<option value='0'>---- Top Category ----</option>
<?
$al=mysql_query("select * from cats where cats_parentid='0' order by cats_id desc");
while($ver=mysql_fetch_array($al))
{
echo "<option value='".$ver["cats_id"]."'>".$ver["cats_name"]."</option>";
$al2=mysql_query("select * from cats where cats_parentid='".$ver["cats_id"]."' order by cats_id desc");
while($ver2=mysql_fetch_array($al2))
{
echo "<option value='".$ver2["cats_id"]."'> > ".$ver2["cats_name"]."</option>";
}
}
?>
</select>
It looks like you're trying to send multiple pieces of data based on a user-selection. It's easier just to send the unique id (as you are already doing) then lookup the 'fee', along with any other data you need' in the script that the form sends to.
As an aside, it also looks like you are making many database calls to, effectively, get all cats back. If that's true, it's probably more efficient to just get them all with a single call and handle the hierarchy in your script.
Because both the select box, ancd an input field are form elements, you can't nest them. But because you only use hidden input fields for storing data on your page, out of sight for the user, it doesn't really matter where you place it, as long as it's inside the form.
You can't see which option the user is selecting on the client side when you are at the server side. The solution is using javascript - which is a client side language, search like this or this would help alot I think.
I am trying to use php (and if not php, javascript) to link to a page and include a "?type=foo" or "#foo" at the end of the link so that when the linked page loads it will automatically select a specific option, depending on the end of the link, from a dropdown menu in a form. I have tried to search for this but do not know what this action is actually called, but I've seen it done before. Does anyone know how to achieve this? Thanks very much.
When I have created these in the past, for ease of use, I have simply inserted the selected value in the top of the select object, rather than roll through the whole list and mark the selected one as such when it is encountered.
Assuming the page is called like page.php?theSelectedList=Cougar:
<select name="theSelectList">
<?php
if( isset( $_GET['theSelectList'] ) )
echo '<option selected="selected">'.$_GET['theSelectList'].'</option>';
?>
<option>Aardvark</option>
<option>Baboon</option>
<option>Cougar</option>
<option>Dog</option>
<option>Elephant</option>
</select>
In that case, the option for Cougar would be displayed twice - once at the top of the list, and selected, and again further down the list in the normal location. This may sound confusing, but, in the instances I have used it, it has been quite intuitive.
It's nice and simple, but, I will mention the caveat, that the above would need a bit of re-jigging if the Option Labels and the Option Values are different (like it each option has a numeric value, but a text label.
If the option is in a <select> then the name you are looking for is the selected attribute. It can be applied to each <option> tag, like so: W3Schools.
Using that you can simply use a PHP if-statement, eg:
<?php
$options = array('Norway', 'United States', 'Springfield');
echo '<select>';
foreach($options as $country) {
if(array_key_exists('selected', $_GET) && $_GET['selected'] === $country) {
echo '<option selected="selected">'.$country.'</option>';
}
else {
echo '<option>'.$country.'</option>';
}
}
echo '</select>';
If the query is ?country=Norway then Norway would be selected when the page loads.
It can of course be solved using javascript as well.
I'm building a theme options page for my WordPress theme and I would like to have a functionality to select multiple items from a list.
The "one option" select code I use looks like this: http://pastie.org/684800 and it works perfectly.
I'm a PHP newbie so I tried to modify the above code to achieve the result I want. Here's what I came up with: pastie.org/684804. As you can see, I basically added a some html values multiple="yes" hoping it will work ;)
The code displays the select item properly, but seems to only save the last selected one. Could someone please give some advice on how to achieve saving multiple chosen items?
If you change the name of the select element to end with "[]", PHP will treat it as an array. All of the selected items will be elements in the array. For example:
<select name="myChoices[]" multiple="multiple"> ... </select>
<?php
$selectedChoices = $_POST['myChoices']; // selectedChoices is an array
?>
If you give the select a name followed by [] in the form,
name="my_select[]"
you will get an array in the target PHP script that you can parse.
<select name="mySelection[]" multiple="multiple">
<option></option>
<option></option>
<option></option>
</select>
This will let you to access the multiple choice you in php