Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Jayanthkumar's avatar

Make input field display none/block based on option selected

Hi Team,

i have an Issue this i would have solved using JS but in laravel is there any better way to solve or solution to solve this issue... i have two from-group one is date selection and other is order selection,

if user select option 1 follow up required then follow up date input should be visible, if user selects other options except follow up required then date input field should be is invisible , How to make this...???code is given below

<div class="form-group">
                            <label for="sel5">Enquiry Result</label>
                                <select name="result" class="form-control" id="sel5">
                                  <option value="" disabled selected>--Select--</option>
                                  <option value="1">Follow Up Required</option>
                                  <option value="2">Active Order</option>
                                  <option value="3">Suspened</option>
                                  <option value="4">Error</option>
                                  <option value="5">Cancelled</option>
                                </select>
                            </div>

                            
                            <div class="form-group">
                              <label>Fallow Up</label>
                                  <input name="fallow_up_date" class="form-control" id="fallowupdate" placeholder="MM/DD/YYYY" type="date"/>
                            </div> 
0 likes
7 replies
katifrantz's avatar

I assume you are using jQuery :


<script>

jQuery(document).ready(function(){
  $("#sel5").change(function() {
      if($(this).val() == 1){

          $("#fallowupdate").css('display', 'block');

      }else{
          $("#fallowupdate").css('display', 'none');
      }
  });
});


</script>

Jayanthkumar's avatar

yeah i am followin same method but its not working @maitrefrantz

and this should happen in real time(page reload should not happen)...

i need Jquery Ajax method....please help me

Jayanthkumar's avatar

my code worked...but its giving error in controller

QueryException in Connection.php line 761: SQLSTATE[22007]: Invalid datetime format: 1292 Incorrect date value: '' for column 'fallow_up_date' at row 1 (SQL: insert into enquiry (customer_id, customertype, enquiry_for, language, enquired_through, result, fallow_up_date, quantity, comments) values (18, 2, 1, 4, 7, 2, , 10, ))

my code

<div class="form-group">
                            <label for="sel5">Enquiry Result</label>
                                <select name="result" class="form-control" id="sel5" onchange="showresult(this.value)">
                                  <option value="" disabled selected>--Select--</option>
                                  <option value="1">Follow Up Required</option>
                                  <option value="2">Active Order</option>
                                  <option value="3">Suspened</option>
                                  <option value="4">Error</option>
                                  <option value="5">Cancelled</option>
                                </select>
                            </div>

                            
                            <div class="form-group">
                              <label>Fallow Up</label>
                                  <input name="fallow_up_date" class="form-control" id="fallowupdate" placeholder="MM/DD/YYYY" type="date"/>
                            </div> 
<script>
function showresult(str) {
  if (str == "1") {
    $("#fallowupdate").css('display', 'block');
    return;
  }else{
    $("#fallowupdate").css('display', 'none'); 
    
  } 
}
</script>
andersfloor's avatar

That's not related to javascript. You might want to check your input names with the database column names. My guess is that your input name fallow_up_date should be follow_up_date.

katifrantz's avatar

That error means your fallow_up_date in the database is expecting a date, but then you are passing a string or something else . You need to make sure that the input field in the form is giving a value in date format . And btw , most browsers do not support the type = "date" , and they would just return the value of that field as text . So you probably would want to use a dedicated jquery datepicker.

Snapey's avatar

If the user does not select a follow-up then you want to hide the date field, but then you will have no date to enter in your model.

You need to decide if the date is required if there is no follow-up

Please or to participate in this conversation.