I have written below lines to display multiselect box and I want to few option disabled which is previously selected.
<select multiple="multiple" name='cars[]'>
<option value="volvo" disabled selected='selected'>Volvo</option>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
</select>
So here In POST method I want to get value of all the selected option including first one volvo.
Afaik, no modern browser will submit a disabled option/field in a form. The easiest solution is to make it a "readonly" field by using the "readonly" attribute.
However, you will have to style the field to look like it has been disabled, as readonly does not change the field's appearance it just prevents the user from modifying it.
The ugly part is, however, that some browsers will NOT allow the readonly attribute to be set for a select field.
What I have usually done in this case is to actually disable the select and store the value in a hidden field instead using some not so ugly javascript to control this on the fly.
Update:
You could easily write a "serializeDisabled" function, iterating over the disabled form elements which have a name attribute and using the jQuery.param function at the end, to generate the serialized string (function comes from the user CMS here):
(function ($) {
$.fn.serializeDisabled = function () {
var obj = {};
$(':disabled[name]', this).each(function () {
obj[this.name] = $(this).val();
});
return $.param(obj);
}
})(jQuery);
You could create a javascript function to loop around the options onSubmit and enabled them again. Disabled elements do not get posted.
when you set the disabled never get in the POST data.... replace select to input type text..and use "readonly" property of that...
Thanks
Related
How to get the value of the selected option in php
<select id="select" name="select">
<option value="option1">Option 1</option>
<option value="option2">Option 2</option>
<option value="option3">Option 3</option>
<option value="option4">Option 4</option>
</select>
PHP is a server-side language. So without submitting anything to the server, you can't find what option has been selected. However, if you want to find it with JavaScript or the jQuery library, you could do something like this:
Using vanilla JavaScript:
document.getElementById("select").onchange = function() {
// find what option the user changed to!
var option = document.getElementById("select").value;
// alert with the option
alert("Select changed to: " + option);
};
Using jQuery library:
// uses jQuery's .change() for a <select> element
$("#select").change(function() {
// find what option the user changed to!
var option = $("#select option:selected").text();
// alert with the option
alert("Select changed to: " + option);
});
You have to make a php file which creates a database table( mysql or mariadb can be used) then in that table you have to redirect the value of form by assigning the php file to the action button and then the value of that option will get restored in that database.
I will prefer to use phpmyadmin, you can create database in it without code and can link it to your file. But if you want to really learn this then i will suggest that first learn php and mysql from a decent website like w3schools because if you simply copy the code than you will not understand anything.
PHP is serverside programming language and it can get values from post , get and cookies and ... . you have to submit form to send values through GET or ... . Best way to do this is using ajax .
`$('#select').on('change',function(){
$.ajax(
/* your code here */
);
}).
Is that possible to get option text instead of value?
Somehow I need to use same option for 2 purpose. one of them need to get value which will be id of selected option and for another purpose i need to get text of that option.
here is how it looks like in HTML:
<select class="form-control" name="address_id">
<option value="1">Street 3.</option>
<option value="2">Street 4.</option>
</select>
I saw solutions base on Jquery and JavaScript but that's not gonna work for me I need PHP solution.
here is my saving method:
// codes....
$order->address = $request->input('address_id'); // basically will save value
//codes...
Auth::user()->orders()->save($order); //saving
I'm a beginner in cakephp, help me please. I have stuck when I think to validate input type selected in form cakephp, the data is detail, so many rows there, first line.
<select id="AirWayBillDetail0ItemId" name="data[AirWayBillDetail][0][item_id]">
<option value="">-- Select an Item --</option>
<option value="9">Handphone</option>
<option value="10">Accecoris</option>
<option value="11">Alat Tulis Kantor</option>
<option value="12">Voucher Fisik</option>
</select>
I want the option, just one time can be used.
Example:
Add first line, I select handphone,
so, the second line, cannot used that option. Is there any way to clear my trouble?
Can I validate in Model or in View or Controller? With javascript or jquery?
You want to check this in the model or controller layers. Never rely on JavaScript for validation as this can lead to vulnerabilities in your app (JavaScript can easily be disabled). You should always validate on the server-side and can supplement this with client-side validation if you want some additional inline validation.
You want to add a check that there are no duplicate item_id in your submitted data. So you could do something like the following in your controller:-
$itemIds = Hash::extract($this->request->data, 'AirWayBillDetail.{n}.item_id');
if (count($itemIds) !== count(array_unique($itemIds))) {
// Invalid item_ids
}
You'd then need to handle how you'd want the error messages showing and ensure it doesn't proceed to save. If you want to do this in the model you need to look at using the beforeSave() callback to check $this->data before saving and return false if duplicated are identified.
You can implement this with jQuery.
Please refer this fiddle http://jsfiddle.net/sqy1n6n3/1/
$('select').on('change', function() {
$('select').find('option').prop('disabled', false);
$('select').each(function() {
$('select').not(this).find('option[value="' + this.value + '"]').prop('disabled', true);
});
});
My select box that is generated by PHP:
<select>
<option selected>Select something</option>
<option data-value="test"></option>
</select>
So far I have tried the following ways to retrieve the value within the data-value attribute:
$('select').children(":selected").data('value');
$('select option:selected').data('value')
$('select option:selected').attr('data-value');
$('select').find(':selected').data('value');
Of course there are a couple of more ways to do this. Once the one of these options gets executed I "sometimes" get the value within the data-value attribute. Even using
$('select').on('change', function () { // code });
gives me the same result. Am I missing some underlying thing?
FIXED:
After every select I use the data information for something else. Then I refresh the select box data by retrieving the remaining data information from the database. At that point I forgot to add the attribute data-value to every option..
Is there a way to mimic the C input/select box where you have a pull down with a blank text-entry at the top? Users would either type a new value or select from the list. Has anyone figured a way to do this with PHP/Javascript? An AJAX type of solution would even be better.
I'm not sure what to call this type of box, I don't think it is a "combo box" like most people think of.
You have a few options.
Here's a quick function I threw together in jQuery that will do what you want. This option required the client have JavaScript enabled to work.
http://jsfiddle.net/QrA4N/25/
If you don't want to make JavaScript a requirement
If you want a solution without JavaScript (client side code). You are stuck with placing an input box with the same "name" as your select box next to it or after it and adding an "Other" option to your select box.
<select name="selAnimals" id="selAnimals">
<option value="dog">Dog</option>
<option value="cat">Cat</option>
<option value="bird">Bird</option>
<option value="guineapig">Guinea Pig</option>
<option value="">Other</option>
</select>
<input type="text" name="selAnimals_Other" id="selAnimals_Other" />
Now your PHP will have to check both $_POST["selAnimals"] and $_POST["selAnimals_Other"] to derive the correct value.
The best option is to combine the HTML above and the JavaScript above that to create a gracefully degrading solution for those with JavaScript enabled or disabled.
http://jsfiddle.net/QrA4N/26/
I added the extra HTML INPUT tags to the jsfiddle from the top of the answer and only changed 1 line of the jQuery function (makeInputSelect).
var $inp = $("#" + id + "_Other");
You can use famous Chosen library for that :)
BTW, see the second example of countries.