Saud
Saud

Reputation: 17

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?

Upvotes: 0

Views: 2614

Answers (3)

Patrick Mlr
Patrick Mlr

Reputation: 2965

You could do this with jQuery. If you don't know jQuery, check this out: jQuery.com

After you "installed" jQuery, you can use this in your js-File. I hope that you know how to create and use a js-File.

The next thing is to implement a good method for your select-button.

This could look like this in your js-File:

$('#my-select').change(function(){ // "#my-select" is the ID of your select. You need to implement a ID.
    //do stuff here, eg. 
    if ($(this).val() == '5') { //check the selected option etc.
        $("input").prop('disabled', true);
    } else {
        $("input").prop('disabled', false);
    }
});

Here's a jsFiddle.

Hope this helps you.

Upvotes: 1

Darshan Kini
Darshan Kini

Reputation: 110

html code

<select>
<option data-id="1">1</option>
<option data-id="2">2</option>
</select>

JQuery code

 $(document).ready(function(){
       $("select").change(function(){
        var display= $("select").val();
        if(display == 1){
        $("select").attr("disabled", 'disabled');
        }
    });
    });

I am not sure what you want exactly.check out this may be it will be helpful for you JSFiddle

Upvotes: 0

Junius L
Junius L

Reputation: 16132

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

Upvotes: 1

Related Questions