simple html dom parser - setting dropdown value - php

i'm having the following html markup:
<select id=gender>
<option value=''>Please select</option>
<option value='m'>male</option>
<option value='f'>female</option>
</select>
i want to set the value using the simple html dom parser - here's my code - which doesn't work:
$combo = $el->find("#gender",0);
$combo->value = "m";
i also tried $combo->setAttribute('value', 'm'); without success
any ideas?
thanks

<select> has no value attribute. What you need to do is find the option you want selected (e.g. something like #gender/option[value='m']) and set the selected attribute on that.

In order to set the selected option out of a <select>, you need to set selected on the corresponding <option>:
<select id=gender>
<option value=''>Please select</option>
<option value='m' selected>male</option>
<option value='f'>female</option>
</select>

Related

Posting the selected html option attribute via php

Posting the selected html option attribute via php..Here is my html code and I'm trying to select one option from the event-drop down menu, but really finding it hard to configure with php variable $event. Any help will be much appreciated.
HTML Code:
<div>
<h4>Event</h4>
<p class="cd-select icon">
<select class="budget">
<option value="0">Select Occasion</option>
<option value="1">alpha</option>
<option value ="2">Bravo</option>
<option value="3">Charlie</option>
<option value="4">Delta</option>
</select>
</p>
</div>
PHP version
$event = $_POST['selected'];
In PHP form fields are posted with their name. In your case you'd have to add the name attribute to the select.
<select class="budget" name="selected">
<option value="0">Select Occasion</option>
<option value="1">alpha</option>
<option value ="2">Bravo</option>
<option value="3">Charlie</option>
<option value="4">Delta</option>
</select>

change browser href in combobox option

I try to make a list of data from a table in a database. I do this with an select option, because the user is only allowed to choose one.
I do it on this way:
<select name="test">
<option value="line1">Line1</option>
<option value="line2">Line2</option>
<option value="other.php" onClick="window.location.href='other.php'">
Other
</option>
</select>
This works in IE and Firefox, but not in Google Chrome.
It's not an option to put the onClick in the <select>-tag, because they don't have a href and I need their values to get the primary key and store something in the table.
I hope you can help me.
UPDATE: this is the solution for my case, solved by Harry
<select name="test" onchange="this.options[this.selectedIndex].value.indexOf('.php')!=-1 &&
(window.location = this.options[this.selectedIndex].value);">
<option value="line1">Line1</option>
<option value="line2">Line2</option>
<option value="other.php">Other</option>
</select>
Try the below code and check if it solves your problem. You needn't have the href values in the select tag for this to work. The href can be in your option's value tag.
UPDATE: The below code would do the redirect only if the value of the selected option tag has .php in it. For the first two options, no action will be taken.
<select name="test"
onchange="this.options[this.selectedIndex].value.indexOf('.php')!=-1 &&
(window.location = this.options[this.selectedIndex].value);">
<option value="line1">Line1</option>
<option value="line2">Line2</option>
<option value="other.php">Other</option>
</select>
this isn't valid HTML. it should at least be:
<option value="other.php" onClick="window.location.href='other.php'">
try this
<select name="test"
onchange="var val = this.value;
if (val.indexOf('other.php')!=-1) window.location=loc">
<option value="line1">Line1</option>
<option value="line2">Line2</option>
<option value="other.php">Other</option>
</select>

select menu html value

How can I add html value for select menu options:
<select name="symbol">
<option>select</option>
<option value="<img src="start.jpg"">start</option>
<option value="<img src="end.jpg"">end</option>
</select>
I want post value to show an image.
I read this : How to add a images in select list
but I want send image value.
If you're handling the post with PHP or some similar server-side code, you should pass through only the reference to the image. Any tags can be output using the server-side code. For example:
<select name="symbol">
<option>select</option>
<option value="start.jpg">start</option>
<option value="end.jpg">end</option>
</select>
And then handle this by doing something like:
echo '<img src="'.$_POST['symbol'].'" />';
Use single quotes instead of double qoutes inside value...
<select name="symbol">
<option>select</option>
<option value="<img src='b.jpg'>">start</option>
<option value="<img src='a.jpg'>">end</option>
</select>
You have to use single quote and close tag. Try this,
<select name="symbol">
<option>select</option>
<option value="<img src='start.jpg'>">start</option>
<option value="<img src='end.jpg'>">end</option>
</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