0

I want to fill the other fields on the form from database on the basis of option selected, but unable to do using php and javascript.I have attached json data I received in question. I want to fill other form fields when i select option from the form field houseowner_select and i want to show div app_houseowner when i select option from the select box having id applicant_is.

$("#applicant_id").on("change", function() {
  var selected = $(this).val();
  makeAjaxRequest1(selected);
});

function makeAjaxRequest1(opts) {
  $.ajax({
    type: "POST",
    data: {
      opts: opts
    },
    url: "get_houseownerinfo_applicant.php",
    success: function(res) {
      console.log(res);
      debugger;

      $("#applicant_wardno").val(res.ho_wardno);
      $("#applicant_citizenship_no").val(res.ho_citizenship_number);
      $("#applicant_phone").val(res.ho_phone);
    }
  });
}
<select name="applicant_is" class="form-control" ng-model="applicant.applicant_is" ng-change="callback_change_applicant_is($event)">
  <option value="">--select--</option>
  <option value="app_houseowner">घरधनी</option>
  <option value="landowner">जग्गाधनी</option>
  <option value="waris">वारिश</option>
</select>
<div class="app_houseowner">
  <select class="form-control" name="houseowner_select" id="applicant_id">
    <option value="">--Select--</option>
    <option value="<?php if (isset($row11['id'])){ echo $row11['id'];}?>">
      <?php if (isset($row11['ho_name_en'])){ echo $row11['ho_name_en'];}?>
    </option>

  </select>
</div>
<input type="text" name="applicant_phone" class="form-control" id="applicant_phone">
<select name="applicant_wardno" class="form-control" id="applicant_wardno">
  <option value="">--Select--</option>
  <?php for ($x = 1; $x <= 19; $x++) {
       echo "<option value=".$x.">".$x."</option>";
    }?>
</select>
<input type="text" name="applicant_citizenship_no" class="form-control" id="applicant_citizenship_no">

enter image description here

Prabina
  • 127
  • 8

2 Answers2

1

First of all remove or comment $("#applicant_salutation").val(res.ho_salutation) and $("#applicant_district").val(res.ho_citizenship_district) line in from your javascript code bcoz both id is not available in your html.

Also add dataType: 'json' in you ajax request

As i have checked applicant_wardno is select box and you have to set selected for displaying value in the select box instead of setting val.

check this link for setting selected value

Priyank
  • 470
  • 2
  • 11
0

You can debbug with an alert: alert(res.ho_wardno); and see if the value is being returned from the ajax query;

      $("#applicant_wardno").val(res.ho_wardno);
      alert(res.ho_wardno);
      $("#applicant_citizenship_no").val(res.ho_citizenship_number);
      $("#applicant_phone").val(res.ho_phone);
  • I need your help . https://stackoverflow.com/questions/57619673/unable-to-dynamically-add-multiple-input-fields-and-submit-to-database-with-java @shrys – Prabina Aug 23 '19 at 03:53