dongyulan6251 2017-03-19 06:45
浏览 46

如何在Laravel 5中通过ajax和php自动化我的html表单

everyone, I'm new to PHP and Ajax I want to create a function in my project, What I want to do in that When I select disease Type in my form as shown in picture enter image description here

When I select Viral Disease so I want to get data of viral disease from database without refreshing the page enter image description here

And my code is

<legend>Patient Details</legend>
   <div class="form-group">

      <label for="select" class="col-lg-2 control-label">Disease Type</label>
      <div class="col-lg-4">
        <select class="form-control" id="disease_type">
          <option>Select Disease Type</option>
          <option value="ViralD">Viral Diseases</option>
          <option value="InfectionD">Infection Diseases </option>
          <option value="CommonD">Common Diseases</option>
          <option value="NewD">New Diseases </option>
        </select>  

    </div>
      <label for="select" class="col-lg-2 control-label">Disease Name</label>
      <div class="col-lg-4">
        <select class="form-control" id="select">
          <option>Select Disease Name</option>
          <option>2-5</option>
          <option>5-10</option>
          <option>10-20</option>
          <option>20-40</option>
        </select>


    </div>
    </div>

This is my Script

<script type="text/javascript">
    $('#disease_type').change(function () {
    var id = $(this).find(':selected')[0].id;
    //alert(id); 
    $.ajax({
        type: 'POST',
        url: 'fetching_names',
        data: {
            'id': id
        },
        success: function (data) {
            // the next thing you want to do 
            var $country = $('#disease_type');
            $country.empty();
            $('#disease_name').empty();
            for (var i = 0; i < data.length; i++) {
                $country.append('<option id=' + data[i].sysid + ' value=' + data[i].name + '>' + data[i].name + '</option>');
            }

            //manually trigger a change event for the contry so that the change handler will get triggered
            $country.change();
        }
    });

});
</script>

This is my controller function

class PatientController extends Controller
{
    public function get_disease_type()
   {

    $disease_type = DB::table('disease_type')->get();

    return view('admin/add_patient', compact('disease_type'));

   }

   public function get_disease_name(Request $request)
   {

    echo $request->disease_type;
    die();
    $disease_name = DB::table('disease')
    ->where("disease_name",$request->disease_type)
    ->get();

    //return view('add-patient', compact('disease'));
    return response()->json($disease_name);
   }

}

And my route

Route::get('/fetching_names', 'PatientController@get_disease_name');
  • 写回答

2条回答 默认 最新

  • doulanyan6455 2017-03-19 07:29
    关注

    I'll advice you go through some tutorials or content online which teaches you ajax. Here's a link which might help:

    https://www.w3schools.com/js/js_ajax_intro.asp

    评论

报告相同问题?

悬赏问题

  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 路易威登官网 里边的参数逆向
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏
  • ¥15 gg加速器加速游戏时,提示不是x86架构
  • ¥15 python按要求编写程序
  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入