Saud Saud - 16 days ago 5
PHP Question

Get select option value from HTML with php to disable input field

I use

select
as below:

<select name="account_type" required>
<option value="">Choose Account Type</option>
<option value="1">Asset</option>
<option value="2">Bank</option>
<option value="3">Capital</option>
<option value="4">Cash</option>
<option value="5">Expense</option>
<option value="6">Income</option>
<option value="7">Liability</option>
</select>


Now I want to catch those option values using
php
variable, and here is the important part: I will have an input field, and based on the values I want to enable/disable that input filed.
How can I do that?

Answer

Using jQuery give your select an id like below

<select name="account_type" id="account" required>
    <option value="">Choose Account Type</option>
    <option value="1">Asset</option>
    <option value="2">Bank</option>
    <option value="3">Capital</option>
    <option value="4">Cash</option>
    <option value="5">Expense</option>
    <option value="6">Income</option>
    <option value="7">Liability</option>
</select>

<!--input to be disabled -->
<input type="text" id="disable">

jQuery

$('#account').on('change', function(){
    if($(this).val() === '1') {
    $("#disable").prop('disabled', true);
    //you can also send data to PHP here using AJAX
    //var data = {one: $(this).val()};
    //$.post( "test.php", data, function( data ) {
    //$( ".result" ).html( data );
    //});
 }else if($(this).val() === '2') {
    //code here 
 }
   //check for other values
});

example