Select form value changes on submission - php

So I have a select feild with various options, the default is an option prompting the user to select from the drop down with a value of "empty". If the user declines to change the drop down before submission, rather than sending a string containing "empty" the form changes the feild to the top option within the select and sends that data instead. My code is included below:
<select class="num" name="Team_1">
<option value="empty" class="red">Please Select</option>
<option value="1">Manchester United</option>
<option value="2">Bristol City</option>
<option value="4">Spurs</option>
<option value="9">Testing</option>
</select>
If you submit the above form with the Please select option chosen, it reverts ti Manchester United and sends a string containing "1"
Any ideas why it might be doing this and/or how i can stop it?

You always should identify which option should be selected by default.
<option value="empty" class="red" selected="selected">Please Select</option>
Change the value of each option tag to that string you want to be send once the form is submitted.

After further investigation I have found the option does not support the class attribute and so by adding class="red" to the option it invalidates the html which then prevents the data from being submitted and so it reverts to the next available value.

Related

Empty url parameter with array not marked empty with PHP empty() function

I have a simple GET form that a user can submit to search for posts (real estates) on my WordPress website.
My HTML search form looks a little like this:
<form method="get" action="estates">
<select name="city[]" multiple>
<option value="">Select an option</option>
<option value="city-1">City 1</option>
<option value="city-2">City 2</option>
</select>
<select name="purpose[]" multiple>
<option value="1">For sale</option>
<option value="2">For rent</option>
</select>
<input type="submit" value="Search">
</form>
Let's say a user selects no other option for city and selects a purpose with value '1'.
The 'purpose'value is correctly added to the URL, and the 'city' field has a value of "".
The URL that is generated would look like this: mysite.com/estates/?city%5B%5D=&purpose%5B%5D=1
Now the problem is with the 'city' field in this example. Because I use a default option for 'city', with a value of "", it is added to the URL but without a value.
However, when I do my PHP checks and build up to search query on my posts page, the empty($GET["city"]) is not returning true, and my query is not working correctly.
I have tried many things, including $GET["city"] == "" and array_key_exists('city', $_GET) but my PHP code always says that $GET["city"] is not empty and should be added to my search query, which then results in a bad query result.
Am I missing something, or is there another way to check if a value is set for this parameter?
When I do print_r($_GET['city']), I get the following return:
Array ( [0] => )
Html: a good practice is to have a default value that is disabled
<option disabled selected value> -- select an option -- </option>
default select option as blank
*Prefer not to pass empty value.
How I usually approach this:
Use the html disabled selected value
Use isset($_GET[.etc.]) then
Sanitize and trim your input
Check if it's a valid option in_array() or other method and then
Allow it to reach your DB
The behavior can vary on your liking. For instance if there is not value you want to inform the user, look for everything or both etc. Consider this, someone passes an argument from url, a city that does not exist. What you want then to happen?

multiple select dropdown - need a hidden selected value or always selected value

I have a multiple select dropdown such as so. I need to ALWAYS include a specific value in 'settings[users][]' when it is passed regardless if other values are selected in the dropdown or not.
I have found some jquery solutions... is there any other way to add a specific value to this array? The actual value does not need to be shown in the selection area it just needs to always be included in the array when posted.
Basically what is happening is a user is selected to be edited which bring up different settings which can be changed. I am using this select box to allow them to set these new settings to other users if they wish. So, the original user value they are editing must always be present in 'settings[users][]' when it passed to my php script for processing.
<select multiple="multiple" name="settings[users][]" size="8">
<option value="" selected="selected">no additional users...</option>
<option value="1">User 1</option>
<option value="2">User 2</option>
<option value="3">User 3</option>
<option value="4">User 4</option>
</select>
EDIT :
I didn't really get the responses I was looking for... here is what I am doing currently.
added hidden field to the form of the always selected user :
<input type="hidden" name="user_id" value="someuserID"/>
in php then :
// push always selected user id to the posted array
array_push($_POST['settings']['users'], $_POST['user_id']);
// filter out any empty value such as if 'no additional users...' was left selected
$userArray = array_filter($userArray);
$userArray now equals all users I need to edit/change in my php
The comments here are trying to (gently) point out the fact that what you want to do is almost certainly unnecessary, and it's needlessly complicating the code -- do you really want to complicate the HTML (the bit that any user can see) just to simplify the PHP?
Having said that I can imagine a possible case where you might need to do this, and I'll get to that. But let me cover the other cases first.
First, if the fixed value is permanently fixed over the entire web site / application then just hardcode it in PHP. As Amir Bawab says in the comments:
$_POST['settings'][] = "someuserID"; // or array_push if you prefer
// Carry on as normal here ...
Second, if the value changes depending on the site context and so is only known to the HTML then your solution in the edit is absolutely the right way to do it. It's clean, it's obvious what's going on in the HTML and PHP, it'll be easy to debug and support later.
But, perhaps you don't have access to the PHP page and absolutely need to post the right structure. As you say you can do this in jQuery/Javascript by intercepting the form submit and inserting the extra hidden value marked as selected.
However, if you're determined to do it as pure HTML (maybe you don't have scripting) then you can do this:
<select multiple="multiple" name="settings[users][]" size="8">
<option value="" selected="selected">no additional users...</option>
<option value="1">User 1</option>
<option value="2">User 2</option>
<option value="3">User 3</option>
<option value="4">User 4</option>
</select>
<select style='display:none' multiple="multiple" name="settings[users][]">
<option value="someusedID" selected="selected"></option>
</select>
If I print_r($_POST) in PHP I get (I selected user 2):
Array
(
[settings] => Array
(
[users] => Array
(
[0] => 4
[1] => someusedID
)
)
)
Which is what you want, I think.
Note, I tried this on Chrome via a local server (XAMPP) I can't swear that all browsers and setups will handle this the same way. That's why the hidden field approach is a much better idea.

Is it right to say when no options are selected from menu in a form, neither name or value passed to the server

I am doing PHP validation, and I was reading w3.org on forms. Can anyone clarify this statement
"When no options are selected, the control is not successful and neither the name nor any values are submitted to the server when the form is submitted". To test this, I have created
<form method="get" action="#">
<select name="select_name" >
<option value="">Select one</option>
<option value="one">Choice one</option>
<option value="two" >Choice two</option>
<option value="three">Choice three</option>
</select>
</form>
When I print the GET array, I see that the name "select_name" always passed to the server. For radio and checkbox, this is not true. But I do not understand what that statement trying to say. Is it possible for "neither name nor value" to be passed during select option?
The specification you quoted:
When no options are selected, the control is not successful and neither the name nor any values are submitted to the server when the form is submitted.
...indicates what to do when no options are selected, but by default most browsers will automatically select the first option of a (drop-down) select element. Do you not see on your test page that the first option "Select one" is automatically selected when the page loads? In your case where the first option has an empty string as its value if you then submitted the form with that option selected you should have a request parameter with the name "select_name" and a value that is an empty string.
In the case of a (non-drop-down) select multiple element:
<select name="select_name" multiple>
...no option is selected by default so then if you submit without selecting any options you should see that you don't get a "select_name" request parameter.
NOTE: You tagged your question with "PHP", but this is not a PHP thing. It's the browser that forms the request according to the state of the form controls at the time.

2 select boxes with same id's but one hidden

I've 2 select boxes with same id's as well as names but at a time only one is shown.
But when form is posted i do not get the exact value.It only displays the first value from the dropdown.
Following is a sample code of the same.
<?php
var_dump($_POST)
?>
<form method="POST" action="">
<select name="test" id="test">
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<select name="test" id="test" style="display:none" >
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<input type="submit" value="Add"/>
</form>
I even tried surrounding the select box with a div and then hidding it.
Not sure why you have two select's with the same name, but when you hide one you also need to disable it too (disabled="disabled").
As per HTML specification, disabled fields are not submitted so you should have your true value posted from the select that isn't disabled.
Presumably you're using some javascript to switch which select is displayed?
If that's the case, then start off with
<select name="test" id="test"></select>
<select name="test_dummy" id="test_dummy" style="display:none" ></select>
And whichever code changes the style attribute will also have to change the name and id attributes
Even when one of these is hidden at every point, they should have different names in the markup so you can identify which one is which.
Otherwise it'll pass only the last element's (in the markup) value through the form (no matter if it's hidden via CSS or not).
I'm not quite sure why your hiding/showing only one of these at a time anyway. It'd likely be better if you replaced the <option>'s inside the select to the new ones, instead of using show/hide.
You would have to have different id's and in your code conditionally check which select box value is visible fetch it's value.
When you submit a form, the POST data contains data basing on the attribute name. If they have the same name, the value of the second select overwrites the value chosen from the first one. Even if you hide it in the page.
If you want to have just the value of the first select, you should disable the second one, by adding disabled="disabled" or by changing its name.

HTML select tag

How to specify each option at a select tag?,using which attribute (name,id,....)?
and how to recieve it in the server script to detect which option is selected?
Here's a standard select box, in which value and content are different:
<select name="color_id">
<option value="1">Red</option>
<option value="2">Blue</option>
<option value="3">Green</option>
</select>
Upon form submission, $_GET['color_id'] or $_POST['color_id'] (depending on the form method) would be set to 2 if the user had selected Blue.
If the content of the option is the value you want to send, no need to repeat yourself; the option's value is set to the content if no other value is specified.
<select name="color_id">
<option>Red</option>
<option>Blue</option>
<option>Green</option>
</select>
Upon form submission, $_GET['color_id'] or $_POST['color_id'] (depending on the form method) would be set to Blue if the user had selected Blue.
The index in $_GET or $_POST will be the name of the select tag. The contents of the array element will be the value of the option.
You use this:
<select name="select1">
<option value="val1">Option 1</option>
<option value="val2">Option 2</option>
</select>
As for the server side, it depends on which technology you are using.
edit sorry I didn't see you tagged it as php. $_POST or $_GET will contain the "select1" item, depending on which method you're using in your form tag.

Categories