<select class="txtbx1" name="country" disabled>
<option value='FR' >FRANCE</option><option value='CH' selected>SWITZERLAND</option>
</select>
the above code is inside a form whose method is post
but echo $_POST['country'] is showing nothing.. on the other hand if I remove disabled from select $_POST['country'] is showing the correct result
This is how the disabled attribute works. When a form control is disabled, the value will be ignored when the form is submitted and the key will not be present in $_POST (or $_GET).
If you want the value to be present in the submitted data, but you don't want the user to be able to change the value on the page (which I imagine is what you are trying to acheive) use readonly="readonly" instead of disabled="disabled".
EDIT
The <select> element does not have a readonly attribute. The above information still stands as it will work for <input>s and <textarea>s.
The solution to your problem here would be to disable the select and use a hidden input to send the value back to the server - e.g.
When the select is enabled:
<select class="txtbx1" name="country">
<!-- options here -->
</select>
...and when it is disabled:
<select class="txtbx1" name="country_disabled" disabled="disabled">
<!-- options here, with appropriate value having `selected="selected"` -->
</select>
<input type="hidden" name="country" value="value_of_field" />
This is the correct behavior. disabled disables the element, and does not send it's value when a form is POSTed.
You can use JavaScript to un-disable the form before you submit it. Something like this (untested):
document.getElementById('myForm').addEventListener('submit', function() {
for(var i = 0; i < this.children.length; i++){
var child = this.children[i];
if(child.disabled){
child.disabled = false;
}
}
});
How your form tag looks like? You may have forgotten the method="post" attribute...
If your goal is have a "readonly select" - that is show the user what the choices are without allowing it to change and have it sent with the POST variables, you want to use "selected" on the option with the current value, and "disabled" on all the other options. This will essentially show the select with all its options but only allow the current one to be selected.
I find this more helpful than simply having a disabled select.
As others have noted, you still need to insure that the server side doesn't accept changes to the field (as with any readonly field).
Related
I have two pages lets say first.php, second.php.
first.php:
<form name=register method="post" action="second.php">
Name:<input type="text" name="username" value=<?=$username?> >
Email:<input type="email" name="email" value=<?=$email?> >
Hobbies:<select name="hobbies" id="hobbies" disabled>
<option value="cricket">cricket</option>
<option value="football">football</option>
</select>
</form>
second.php:
<?
print_r($_POST);
I am getting only name and email and not select value.
If I print the post value I am not getting the select value, but when I am removing the disabled in select, I am getting the select value in hidden.
In this scenario, I don't want the user to select the select box and when I submit the form, I should get the value in hidden in second.php.
I have tried many possible ways, to no avail.
Can anybody suggest me how can I achieve this.
Disabled tags can't be send to POST. Change disabled to readonly or hidden
You cant post a disabled field. Maybe you can disable the selectbox right before you post it or you could (like the comments tell you) get a hidden value to be posted that has the standard value you want. If the select box is able to be posted(so not disabled) you can overwrite the hidden value with the select value.
You can do this by Jquery before the form is submitted.
<script>
$(document).ready(function () {
$('#register').submit(function() {
$('select').removeAttr('disabled');
});
});
</script>
In
second.php
you have to create a validation to check that user can be registered or no.
if($this->user->can_register()){
$this->user->save($_POST['hobbies']);
}
Because if only with html or js exception is not enough to handle that case. Some users can just inspect element and remove the disabled element.
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.
My form contains a radio-button & a drop-down. So depending upon the selected value of radio button i need to enable/disable the drop-down.
I am doing this:
<select name="bas_type" id="bas_type" disabled="disabled">
<option value="Monthly">Monthly</option>
<option value="Quarterly">Quarterly</option>
<option value="Annualy">Annualy</option>
</select>
But i want to get drop-down value as empty in $_POST if form is disabled..
So this won't work. Please any suggestions..
Simply add a hidden field with the value you want to receive when the control is disabled and the same name as the dropdown. Be sure to place it before the dropdown in the HTML:
<input type="hidden" name="bas_type" value="" />
<select name="bas_type" id="bas_type" disabled="disabled">
<option value="Monthly">Monthly</option>
<option value="Quarterly">Quarterly</option>
<option value="Annualy">Annualy</option>
</select>
Of course you shouldn't really have to do this, since in PHP you should be receiving incoming parameters with at least a helper function like
function param($name, $default = null) {
return isset($_REQUEST[$name]) ? $_REQUEST[$name] : $default;
}
Don't rely on data you get from form. What you should actually do is check serverside if radiobutton is active or not, and depending on that ignore or accept value from dropbox.
Set the value in the form to "" and that should do the trick:
<option value="">Annualy</option>
or use a space like this (not sure which will work better for you, haven't tried to do it):
<option value=" ">Annualy</option>
Edit: The data contained in the value="something" is the actual data that is passed to the URL via a get. Hence, if you blank it out, the data won't get passed.
This question already has answers here:
Get Text From <option> Tag Using PHP
(6 answers)
Closed 8 years ago.
in the following:
<form action="test.php" method="POST">
<select id="test" name="test">
<option value="1">Test One</option>
<option value="2">Test Two</option>
</select>
</form>
in test.php, I can get 1 or 2 as follow:
$result=$_POST['test'];
How can I get the text of the selected option (i.e. "Test One" or "Test Two") using php
This is not something that can be done through PHP alone. The PHP script can only "see" the information which is posted (the value for the selected option that is posted). You can use javascript to alter a hidden input field with the text contents of a selected option, and this will be included in the $_POST array:
<form action="test.php" method="POST">
<select id="test" onchange="document.getElementById('text_content').value=this.options[this.selectedIndex].text">
<option value="1">Test One</option>
<option value="2">Test Two</option>
</select>
<input type="hidden" name="test_text" id="text_content" value="" />
</form>
This will make the $_POST['test_text'] available with the selected index (but you should also force the onchange() function when the page loads so that it will be populated even if the user leaves the select field at the default value.
Only the value of the form control will be sent to the server. An easy way to send both is to simply include both in the the value:
<select name="test">
<option value="1|Test one">Test one</option>
<option value="2|Test two">Test two</option>
</select>
And then:
$test = explode('|', $_POST['test']);
Then you'll end up with $test[0] being "1" and $test[1] being "Test one".
You can't; that information is not sent back to the server. You will need to look at how you generated the HTML in the first place and get the text from there.
It is not sent so the only way to get it is having an array mapping values to titles in your PHP code.
This is a solution I could use except I already have the onChange calling a function to show a hidden block based on the selected building type in a statement.
I have building types I need the user to select. Several building types have a unique set of questions, e.g. Bank Branch, Data Center, Courtroom, etc. But there are many that have the same set of questions which I call Other Type. Each option has their associated value, e.g. Bank Branch has a value of "Bank_Branch", Data Center has "Data_Center", but Other Type has a value of "Other_Type". The text for "Other_Type" differs based on the building type, such as Convention Center, Museum, Performing Arts, etc. So I need the value "Other_Type" to "show" the questions in the "Other_Type" DIV block while using the text value to send in an email identifying the type of building, e.g. Convention Center, Museum, Performing Arts, etc.
Any way to use PHP to get the text value of the selected item? I'm already using the following inside the HTML
var sele = document.getElementById('building_type');
var seleVal = sele.options[sele.selectedIndex].value;
var seleTxt = sele.options[sele.selectedIndex].text;
document.getElementById("other_text").innerHTML = seleTxt;
I'm not seeing a way to do this.
SOLVED: I can simply create the hidden div and in my check for Other Type set the innerHTML for the hidden div.
The text for the selected option is not passed through the HTML form. If you want the text, you have to store that in a hidden HTML input on your page, or you need to add some business logic in your PHP code to translate the value of ìd into the text (through a switch statement, or by querying a database, etc.)
Unfortunately when you submit a form and a variable it only takes one parameter which is it's value. You would need to make the value. Test One the value in order for it to pass on to the PHP script. What is the purpose of value="1" cause you can probably use it in a different attribute?
Im not a experienced php programer, but u can check the value selected in SELECT tag with php.
i dont know why people say u can not.
if ( $_POST['test'] == 1)
{ do something }
else
{ option value 2 is selected, do something else }
Im sure this will work.
Try it if 1, 2 not needed. you will get required text as value
<form action="test.php" method="POST">
<select id="test" name="test">
<option value="Test One">Test One</option>
<option value="Test Two">Test Two</option>
</select>
</form>
I have this simple Select box that is suppose to store the selected value in a hidden input, which can then be used for POST (I am doing it this way to use data from disabled drop down menus)
<body>
<?php
$Z = $_POST[hdn];
?>
<form id="form1" name="form1" method="post" action="test.php">
<select name="whatever" id="whatever" onchange="document.getElementById('hdn').value = this.value">
<option value="1">1Value</option>
<option value="2">2Value</option>
<option value="3">3Value</option>
<option value="4">4Value</option>
</select>
<input type="hidden" name ='hdn' id="hdn" />
<input type="submit" id='submit' />
<?php
echo "<p>".$Z."</p>";
?>
</form>
</body>
The echo call works for the last 3 options (2,3,4) but if I select the first one it doesnt output anything, and even if i change first one it still doesnt output anything.
Can someone explain to me whats going on, I think it might be a syntax issue.
EDIT: As mentioned in first paragraph: I am using the hidden field instead of just using the value of selected because i plan on disabling the select drop down in my actual website, and when you disable the menu the POST data is no longer accessible. By putting it into a hidden field you can still access the information
The first one is the default, so when you "change" to the first one, it hasn't actually changed and the event does not fire.
You should be reading the value directly from the select and not depending on JS to set a hidden field though. The JS is just pointless, unreliable complexity.
Does anything else in your client-side application depend on that hidden field? If the ONLY purpose of the hidden field is to duplicate the selected value of the dropdown, then there's absolutely no reason to use an onchange handler.
Best solution would be to set an onsubmit handler on the form itself to copy the dropdown value into the hidden field.
<form ... onsubmit="document.getElementById('hdn').value = document.getElementById('whatever').value">
Try your post access like this:
<?php
if (array_key_exists('hdn', $_POST)) {
$Z = $_POST['hdn'];
}
else {
$Z = 1;
}
?>
change your input:
<input type="hidden" name='hdn' id="hdn" value= <?php echo "$Z"; ?> />
this.value has no value. That is why $_POST['hdn'] doesn't have a value when you initially load the form.
As #David said, if you use Firefox you can see the post data for hdn is empty/null.