HTML Drop down menu with text input also - php

I want to create a drop down html button with numbers 1 through 100. That also allows for text input. I want it too look something like this:
I know that html has a select input type where I can specify what I want in the dropdown menu, but it does not have the text input option that I also want. How would I go about this using html and php?

See this https://www.w3schools.com/tags/tag_datalist.asp
Is that a datalist what are you looking for?
<input list="browsers">
<datalist id="browsers">
<option value="Internet Explorer">
<option value="Firefox">
<option value="Chrome">
<option value="Opera">
<option value="Safari">
</datalist>

Related

anchor tag is not appearing in bootstrap options

I am making pagination sorting but the anchor tag is not appearing . When i inspect element there is no anchor tag in the option . I cant understand why this happening. I am using bootstrap. Here is my code
<select class="wide">
<option data-display="Select">Nothing</option>
<option value="Relevance">
Relevance
</option>
<option value="Name, A to Z"> Name, A to Z
</option>
<option value="Name, Z to A">Name, Z to A</option>
<option value="Price, low to high">
Price, low to high
</option>
<option value="Price, high to low">Price, high to low</option>
</select>
The HTML <option> element allows only text in its content, possibly with escaped characters (e.g. é).
You can't add an <a> inside, but you can handle the onclick event with JavaScript.

echo html variable, that includes a php include

I have this issue. I have HTML code stored in the database. I read it, and I display it in my pages using echo $page_content;
I want to add somewhere in the middle of this HTML code a php include. Something like this:
Inside this file I have also html code which is basically the country options for a select:
<option selected="selected" value="">Choose...</option>
<option value="AFGHANISTAN">AFGHANISTAN</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>
etc etc
Until now I have hard-coded all the countries in many places, which I don't like.
But this is not displayed at all when i do my
echo $page_content;
One of the easiest ways is to use tokens that get replaced with the content.
This can avoid the use of eval - which is not recommended ane would get me down voted by the herd.
For example:
HTML CODE
Hello, _TOKEN2_!
You can then use string replace functions to replace the TOKEN2 (the underscores are there but seemed to disappear here) with whatever you want:
str_replace("_TOKEN2_","World",$page_content);
And so on, you get the idea.
I would recommend you look into using something like Smarty to separate you're PHP and layout.

Grabbing all options from HTML select list

I need to create a php array from a very large HTML select list. I searched for "dropdown to array" generators and programs but can't find anything. Can someone please suggest a php code that I can use to grab all options from a HTML drop-down list and output an array of them?
update: php or js. Anything that will output an array which I can copy and paste in my script.
Include brackets in the select name; PHP will accept your selection as an array:
<?php print_r($_GET['cars']);?>
<form>
<select name="cars[]" multiple="multiple" size="5">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
</select>
<input type="submit" />
</form>
Upon postback the print_r outputs (depending on selection):
Array ( [0] => saab [1] => mercedes )
If it's javascript:
document.getElementById(<!--the_id_of_the_select-->). Options and just treat it as an array that will create and append (document.createElement('input'), document.forms[0].appendChild(previous_item)) during the onsubmit stage of the forms process.
Alternatively in php:
When you do the loop for the option tags created in the select area, just do a second foreach and create a bunch of hidden elements somewhere else on the form.
Alternatively for both, instead of lots of hidden inputs (with a certain prefix), you could just create a single one that lists all the options in a csv format, or something similar

How do to I send my select box value to javascript

I have a random amount of select boxes generated by javascript, all containing the same options. Now I wish to have a "master-selectbox" which sets the value for each and every one of them.
Currently I have <select id="changeKlas" onChange="javascript:changeClass(this.parentNode, getElementById(changeKlas))">
At the javascript I've gotten as far as to find each and every select box and I already know how to set the value but the value is not being send to javascript.
On each attempt I have made the getElementById(changeKlas) is null. How can I fix this so I can get the text and value of the selected textbox in the given select?
Try this.
<select id="changeKlas" onChange="changeClass(this)">
And in your JavaScript, this will be transformed to document.getElementById(changeKlas)
You need to quote the id: [...], document.getElementById('changeKlas') -- notice the single quote, double quote needs to be escaped because of the outer one from onChange. Also, notice that getElementById belongs to document
<select id="changeKlas" onChange="changeClass();">
<option value="red">Red</option>
<option value="blue">Blue</option>
<option value="green">Green</option>
<option value="black">Black</option>
</select>
<script>
function changeClass()
{
var changeKlas = document.getElementById('changeKlas').value;
alert(changeKlas);
}
</script>
// On change It will alert the SELECTED VALUE

php get selected option

I am processing files that contain HTML markup.
I need to get the selected option from the drop down box. In this case Australia is the selected option ..
<select name="cCountry" id="cCountry" style="width:200" tabindex="5">
<option value="251">Ascension Island</option>
<option selected="selected" value="14">Australia</option>
<option value="13">Austria</option>
Another scenario :
<select name="cBirthYearM" id="cBirthYearM">
<option value="1974">1974</option>
<option value="1975">1975</option>
<option value="1976">1976</option>
<option selected="selected" value="1977">1977</option>
<option value="1978">1978</option>
<option value="1979">1979</option>
In this case '1977" is the value that I need to extract as it's the selected option .
To make it clear I need to get the value from the markup not from user GET/POST input
The value specified in the value part of the option will be in $_GET ['cCountry'] or $_POST ['cCountry'] (depending if you're using GET or POST to submit the form).
It should be simple matter to look up the value from a lookup table or DB query.
EDIT: This question didn't make it clear if you meant how to get the value the user selected when the form is submitted, or how to get the value from the markup. If it's the latter, then you should look at DOMDocument.
If you wish to parse HTML in PHP then the preferred method is the DOM hierarchy of objects, especially DOMDocument. You can use Javascript-like methods of accessing the DOM tree such as getElementById to grab the select control (in this case getElementById ('cCountry')) and then examine its children (the options) to find the selected one and get its attributes.
It can take a little getting used to using the DOM objects but they're far more powerful for parsing and manipulating HTML than regex would be, so they're well worth learning.
http://php.net/manual/en/book.dom.php
http://php.net/manual/en/class.domdocument.php

Categories