简体   繁体   English

使用CodeIgniter中的AJAX执行查询并显示结果

[英]Execute query & display results using AJAX in CodeIgniter

I need some help getting AJAX to work within a CodeIgniter project. 我需要一些帮助,以使AJAX在CodeIgniter项目中工作。 Based off of a user's selection, I want to use AJAX to send a variable (Agency_Number) to my controller to execute and get query results back within my view. 基于用户的选择,我想使用AJAX将变量(Agency_Number)发送到控制器,以执行并在视图内返回查询结果。

Example: 例:

  1. User selects the Agency Number from the dropdown list 用户从下拉列表中选择代理商编号
  2. Agency Number sent to the controller using AJAX 使用AJAX发送到控制器的代理商编号
  3. Agency Number sent to model to execute query to select that agency's info from database. 发送给模型的代理商编号,以执行查询以从数据库中选择该代理商的信息。
  4. Form inputs, such as Program_Host_Name, get populated with Agency information. 表单输入(例如Program_Host_Name)将填充有代理商信息。

I'm able to successfully execute the query and get the Program Host Name to populate within my form, however, my select input disappears from the page when the information is returned. 我能够成功执行查询并获得要在表单中填充的程序主机名,但是,当返回信息时,我的选择输入将从页面中消失。 I suppose this is caused by reloading my view, which I don't think I should be doing in the first place. 我想这是由重新加载视图引起的,我认为我本来不应该这样做。 I'm not sure how to pass the Program_Host_Name value back to my view without doing this though. 我不确定如何不将Program_Host_Name值传递回我的视图。

Thanks for any help y'all can give! 谢谢大家的帮助!

Here's my view (create_opportunity.php) 这是我的观点(create_opportunity.php)

<form submit="opportunity/create_opportunity" id="validation-form" method="POST">
<select id="Agency_Number">
  <option value=""></option>
  <?php foreach ($agencies as $agency):?>
  <option value="<?php echo $agency->Agency_Number;?>"><?php echo $agency->Agency_Number;?></option>
  <?php endforeach;?>
</select>

<input type="text" id="Program_Host_Name" value="<?php echo $Program_Host_Name ?>"/>
</form>

Here's my script within the view: 这是视图中的脚本:

<script type="text/javascript">
$('#Lookup').click(function() {

    var AgencyNumber = $('#Agency_Number').val();

    if (!AgencyNumber || AgencyNumber == 'Agency_Number') {
        alert('Please enter an Agency Number');
        return false;
    }

    var form_data = {
        Agency_Number: $('#Agency_Number').val(),
        ajax: '1'
    };

    $.ajax({
        url: "<?php echo site_url('opportunity/create_opportunity'); ?>",
        type: 'POST',
        data: form_data,
        success: function(msg) {
            $('body').html(msg);
        }
    });

    return false;
});
</script>

Here's my controller (opportunity.php) 这是我的控制器(opportunity.php)

function create_opportunity() {

  $this->data['agencies'] = $this->ion_auth_model->get_agencies();

  if($this->input->post('ajax')) {
    $Agency_Number = $this->input->post('Agency_Number');
    $agency = $this->ion_auth_model->get_agency($Agency_Number)->row();
    $this->data['Program_Host_Name'] = $agency->Name;
    $this->data['Address_1'] = $agency->Address_1;
  } 

    $this->data['main_content'] = 'create_opportunity';
    $this->load->view('./_blocks/template', $this->data);
}

I had to use json_encode() to convert an associative array from PHP into JSON and then used $.getJSON() to return the JavaScript array. 我必须使用json_encode()将关联数组从PHP转换为JSON,然后使用$ .getJSON()返回JavaScript数组。

Here's my working script: 这是我的工作脚本:

<script type="text/javascript">
$('#Lookup').click(function() {

    var AgencyNumber = $('#Agency_Number').val();

    if (!AgencyNumber || AgencyNumber == 'Agency_Number') {
        alert('Please enter an Agency Number');
        return false;
    }

    var form_data = {
        Agency_Number: $('#Agency_Number').val(),
        ajax: '1'
    };

    $.ajax({
        url: "<?php echo site_url('opportunity/get_agency'); ?>",
        type: 'POST',
        data: form_data,
        dataType: 'json',
        cache: false,
        success: function(data) {
            $('#Program_Host_Name').val(data.Program_Host_Name);
            $('#Address_Line_1').val(data.Address_1);
        },
        error: function(thrownError) {
            alert(thrownError);
        }
    });

    return false;
});
</script>

Here's my working controller: 这是我的工作控制器:

function get_agency() {

    if($this->input->post('ajax')) {
        $Agency_Number = $this->input->post('Agency_Number');

        $agency = $this->ion_auth_model->get_agency($Agency_Number)->row();

        echo json_encode(
            array(  "Program_Host_Name" => "$agency->Name",
                    "Address_1" => "$agency->Address_1"
            )
        );
    }

}

Thanks for the help! 谢谢您的帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM