【问题标题】:Codeigniter - Posting form variables and retrieving through jQuery AJAXCodeigniter - 发布表单变量并通过 jQuery AJAX 检索
【发布时间】:2011-01-27 07:57:35
【问题描述】:

我需要使用 Ajax jQuery 在 Codeigniter 中发布一个变量并获得结果。

虽然我的数据库有内容,但代码仍然返回空数组。 - 数组(0){}。 问题是变量没有从表单中发布。这是我的代码:

表格代码 - 查看:

<form id = "myform1" onsubmit="return false">
 <select name="field_country">
 <option value="0">Argentina</option>
 <option value="1">Chile</option>
 </select>
</form>

Javascript - 内联视图

jQuery(document).ready(function($){
 $("select[name='field_country']").change(function(){
  var country_name= $("select[name='field_country'] option:selected").text();
  $.post("<?= base_url(); ?>index.php/catalog/getCities/", {country:country_name}, function(data){
   alert(data);
  })
 })
})

catalog.php 控制器 - 功能:

function getCities(){
 $country_name = $this->input->post('country', 'Argentina');
 $result = $this->catalog_model->getCitiesNames($country_name);
 var_dump($result);
}

catalog_model 模型函数:

function getCitiesNames($country_name) {
 $cities=array();
 $result = $this->db->query("SELECT title FROM cities WHERE country = '".$country_name."'");
 foreach ($result->result() as $row) {
  array_push($cities, $row->title);
 }
 return $cities;
}

【问题讨论】:

    标签: php jquery codeigniter


    【解决方案1】:

    您需要对 Javascript 和 Controller 代码进行一些小修改。为了让 Javascript 和 PHP 在彼此之间进行通信,您必须告诉两段代码您使用什么格式来传输信息。最简单和最常见的格式是 JSON。因此,您需要修改代码以发送和接收 JSON。

    表格视图

    <form id="myform1" onsubmit="return false">
        <select id='country' name="field_country">
           <option value="0">Argentina</option>
           <option value="1">Chile</option>
        </select>
    </form>
    

    我已将id='country' 添加到您的select 以方便联系。

    JAVASCRIPT

    $(document).ready(function(){
       // When the SELECT is changed
       $("#myform1 #country").change(function(){
    
        var country_name= $(this).find("option:selected").text();
        $.post ( "<?= base_url(); ?>index.php/catalog/getCities/",
                 { country:country_name },
                 function(data){
                    ...on success do something here
                 },
                 'json'
        );
    
    });
    

    });

    你需要告诉 Javascript 你期待一个 JSON 对象返回。

    控制器

    function getCities(){
       $country_name = $this->input->post('country');
       $result = $this->catalog_model->getCitiesNames($country_name);
    
       echo json_encode($result);
    }
    

    您的 PHP 代码应该返回一个 JSON 对象。

    【讨论】:

    • 非常感谢。我解决了这个问题,问题不在于返回变量。问题是变量在发布时会被破坏。我通过将 post uri 更改为“getCities”而不是之前的绝对路径找到了解决方案。我正在使用多语言 uri 标识符,所以我怀疑它可能会产生干扰,因为应用程序看起来像是自动生成 2 个数据请求(1 个 POST 和 1 个 GET)。不过,它现在已经解决了,谢谢。
    【解决方案2】:
    jQuery(document).ready(function($){
    $("select[name='field_country']").change(function(){
        var country_name= $("select[name='field_country'] option:selected").text();
        $.ajax({
      type: "POST",
       url: "<?= base_url(); ?>index.php/catalog/getCities/",
       data: "country="+country_name,   
       success: function(data){
                alert(data);
           }
        });
       });
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-01-21
      • 1970-01-01
      • 1970-01-01
      • 2019-06-09
      • 2015-05-20
      • 1970-01-01
      • 1970-01-01
      • 2015-11-04
      相关资源
      最近更新 更多