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

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"]

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 make dropdown field mandetory(i.e required) while entering in to database

<select name="location" id="category1" required>
<option value="location">Select City</option>
<option value="anantapur>Anantapur</option>
<option value="guntakal" required>Guntakal</option>
<option value="guntur" required>Guntur</option>
<option value="hyderabad" required>Hyderabad/Secunderabad</option>
<option value="kakinada" required>kakinada</option>
<option value="kurnool" required>kurnool</option>
<option value="nellore" required>Nellore</option>
<option value="nizamabad" required>Nizamabad</option>
<option value="rajahmundry" required>Rajahmundry</option>
<option value="tirupati" required>Tirupati</option>
<option value="vijayawada" required>Vijayawada</option>
<option value="visakhapatnam" required>Visakhapatnam</option>
<option value="warangal" required>Warangal</option>
</select>
how to make dropdown field mandetory(i.e required) while entering in to database
you have already made dropdown field mandatory, the only thing you have to do is to set value for first option field empty, and mark it "selected" as default
i.e:
<option value="" selected>Select City</option>
this will prevent to submit your form until you select a value from dropdown.
That's it :)

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>

How to store array in usermeta in wordpress

I have a custom form and am trying to store multiple values in the usermeta. This is a sample code I am using, however it just stores the last option selected (i.e. if I select from Volvo to Opel, it will only store Opel in the database), whereas what I am wanting is all the values selected to be stored. Im not sure what Im doing wrong.
Any help would be appreciated.
<select name="test" id="test" multiple>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="opel">Opel</option>
<option value="audi">Audi</option>
</select>
update_usermeta( $new_user, 'test', $_POST['test'] );
according to the wordpress codex arrays and objects are automatically serialized
http://codex.wordpress.org/Function_Reference/update_user_meta
so you shouldn't have any problems with it. if you need to grab multiple values from the select you will need to treat it as an array name="test[]" like this
<select name="test[]" id="test" multiple>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="opel">Opel</option>
<option value="audi">Audi</option>
</select>

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