How to fix php getting select option value instead of text? - php

I have issues posting my select option values from html form to php back-end
Here are my codes:
<form action="submit.php" method="POST">
<select name="approver" class="form-control" autofocus>
<option selected disabled value="0">— Select Approver —</option>
<option selected disabled value="1">User A</option>
<option selected disabled value="2">User B</option>
</select>
</form>
submit.php:
$approver = mysqli_real_escape_string($conn, $_POST['approver']);
Is there a way for php to receive the value of the option, eg. 1, instead of the text inside the option tag?
Thanks in advance

"selected disabled" attributes should be used for the first option(generally which is not really an option for the user to select). It is used so that the user cannot choose the first option again after changing the option. Remove the "selected disabled" attributes from other options.
<select name="approver" class="form-control" autofocus>
<option selected disabled value="0">— Select Approver —</option>
<option value="1">User A</option>
<option value="2">User B</option>
</select>

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.

Select input of form doesn't render on php email

I have a form that includes some select inputs like this
<select class="motive" name="motive">
<option value="" disabled selected>Select a Number</option>
<option value="">One</option>
<option value="">Two</option>
<option value="">Three</option>
</select>
On my php form side I have this piece of code
$motive = $_POST['motive'];
In my received email I'm not getting the motive displayed, is showing as a blank space. Can someone tell me why?
thanks!
For each of your <option> tags, you must have a value attribute set.
<select class="motive" name="motive">
<option value="" disabled selected>Select a Number</option>
<option value="1">One</option>
<option value="2">Two</option>
<option value="2">Three</option>
</select>
If user has option One selected and submits the form, then in PHP you
$selected_motive = $_POST['motive']; // equals "1"
It's probably because <select> inputs are not allowed in emails. Most email clients will just remove them.

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;
}

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>

Working with "multiple" select

In my profile advanced search i have an mutiple select type..
<form method="POST">
<select size="6" name="Cities" multiple="multiple" id="Cities">
<option value="0">All</option>
<option value="1">City1</option>
<option value="2">city2</option>
<option value="3">city3</option>
<option value="4">city4</option>
<option value="5">city5</option>
</select>
</form>
How do I use it, I mean how should i call what they have chosed...As you can choose one or more options (by holding in control, or just holding in mouseclick and point to other options).
Give it the name Cities[] instead of Cities, and you'll have an array in $_POST['Cities'] on submit.
google helps
http://www.onlinetools.org/tricks/using_multiple_select.php

Categories