optgroup get in php label associated - php

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

Related

Issue With option Tag In HTML While Storing in Database By PHP

I want to save value as Hsn/product code and option text as product.
i defined value for hsn because i want to show it on my active webpage as Selected option Hsn code.
but problem is that when i submit form, then i recieve same value in productName & ProductHSN in our Database
And i want to store productName as saree, lehga, etc
And ProductHSN as W1, M1,etc .
plase help
<select class="form-control" id="colTwo2" name="colTwo[]" data-
type="productName" required="">
<option value=""> --SELECT-- </option>
<option value="W1">Saree</option>
<option value="W2">Lehga</option>
<option value="G1">Kurti</option>
<option value="G2">Salwar Suit</option>
<option value="G3">Lagi</option>
<option value="G4">Hairam</option>
<option value="G5">Long Suit</option>
<option value="W3">Goun</option>
<option value="W10">Chunri</option>
<option value="M1">Suiting</option>
<option value="M2">Shirting</option>
<option value="G6">Jeans</option>
<option value="M4">T-Shirt</option>
</select>
You are getting same value because the value in option value="" is only sent. Not the option text.
You could use PHP explode function.
HTML
<select class="form-control" id="product" name="product" required>
<option value=""> --SELECT-- </option>
<option value="W1|Saree">Saree</option>
<option value="W2|Lehga">Lehga</option>
<option value="G1|Kurti">Kurti</option>
<option value="G2|Salwar Suit">Salwar Suit</option>
</select>
PHP
<?php
$result = $_POST['product'];
$result_explode = explode('|', $result);
$producthsn = $result_explode[0];
$productname = $result_explode[1];
?>
In the select box we are giving 2 options with a '|' separator to split the values. In action page result is exploded into an array. You can then insert each one separately in the database.

How to retrieve <option> id to insert data into database

I have a form with a dropdown list using <select> and <option> tags.
below that I have a textarea field.
The option ID's are the databases names, and I need to insert the textarea-input into the selected database.
Code:
HTML FORM:
<select class="form-control" id="selection" name="selection_name">
<option id="option1">First item</option>
<option id="option2">Second item</option>
</select>
PHP:
<?php
$dbToInsert = $_POST[''] // <-What do I insert here to get the selected option?
...
?>
You Should use "value" in option tag
<select name="vehicle">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
</select>
In the above example, if you have chosen "Audi", on form submission you will get the value "audi" in $_POST["vehicle"]

Form with disabled select option selected as default not sending anything to validate

I have a form like so:
<select name="employment">
<option disabled="" selected="">-- What is your employment status --</option>
<option value="employed">Employed</option>
<option value="self_employed">Self Employed</option>
</select>
In PHP I am running through each of the $_POST variables and then checking if they have a value. If not I then add that field to the array for an error message.
The issue is that if I leave the default 'disabled' message selected nothing is passed through a post value so theres nothing for me to validate.
If I print_r my $_POST variable then it contains no 'employment' field unless I select an option.
How can I solve this?
<select name="employment">
<option disabled selected>-- What is your employment status --</option>
<option value="employed">Employed</option>
<option value="self_employed">Self Employed</option>
</select>
disabled attribute
The disabled attribute is a boolean attribute.
When present, it specifies that an option should be disabled
A disabled option is unusable and un-clickable.
Syntax:
<select disabled>
Not
<select disabled="">
In case of XHTML, syntax differs like
<select disabled="disabled">
hidden attribute
So, If you want to validate it. Use hidden in option.
<select name="employment">
<option hidden>-- What is your employment status --</option>
<option value="employed">Employed</option>
<option value="self_employed">Self Employed</option>
</select>
When, nothing got selected, then it will output as -- What is your employment status --
<?php
echo $Employment=$_POST['employment'];
?>
Output: -- What is your employment status --
So, Now you can easily use your validation in dropdown
For more info, click disabled attribute - W3 Schools
Try this:
<select name="employment">
<option disabled="" selected="" value ="">-- What is your employment status --</option>
<option value="employed">Employed</option>
<option value="self_employed">Self Employed</option>
</select>
PHP:
if(empty($_POST['employment'])){
return false;
}else {
// I got value;
}

populate form based on multiple select elements

I want a form to have multiple drop down selects that change based on the previous selection. This is how I want it to work.
<select id="format">
<option selected="selected" value="NULL">-- Select a product Format --</option>
<option value="PS3">PS3</option>
<option value="Xbox 360">Xbox 360</option>
<option value="Wii U">Wii U</option>
</select>
So if you select PS3 you are then faced with more options for PS3 product type
<select id="ps3-product-type">
<option selected="selected" value="NULL">-- Select a product PS3 product type --</option>
<option value="chargers">chargers</option>
<option value="controllers">controllers</option>
<option value="headsets">headsets</option>
</select>
So then you select headsets for example and you get another set of final options of products
<select id="ps3-headsets">
<option selected="selected" value="NULL">-- Select a PS3 headset --</option>
<option value="product-1">product 1</option>
<option value="product 2">product 2</option>
<option value="product 3">product 3</option>
</select>
How can I do this with jquery or PHP? Please bare in mind that I will also have selects for Xbox 360 and Wii U too.
UPDATE PROGRESS:
http://jsfiddle.net/maximus83/r7MN9/639/
Here is my HTML, I have got the first set of arrays working for selecting product type, But I cant get the 3rd box working, I need 3rd box to say the product, where do I go from here.
<select id="cat">
<option val="PS3">PS3</option>
<option val="Xbox360">Xbox360</option>
<option val="WiiU">WiiU</option>
<option val="Multiformat">Multiformat</option>
</select>
<select id="item">
</select>
<select id="product">
</select>
Here is my script
PS3=new Array('Headsets(PS3)','Controllers(PS3)','Chargers(PS3)','Cables(PS3)');
Xbox360=new Array('Headsets(360)','Chargers(360)');
WiiU=new Array('Controllers(WiiU)','Headsets(WiiU)');
Multiformat=new Array('Headsets(Multi)','Chairs(Multi)','Cables(Multi)');
populateSelect();
$(function() {
$('#cat').change(function(){
populateSelect();
});
});
function populateSelect(){
cat=$('#cat').val();
$('#item').html('');
eval(cat).forEach(function(t) {
$('#item').append('<option val="#item">'+t+'</option>');
});
}
This should do the trick for you. It's a previously answered thread on the exact same topic - how to populate a drop down based on the value in another. It takes advantage jQuery (And I've used it before myself as reference).

simple html dom parser - setting dropdown value

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>

Categories