【问题标题】:Is there a way to display data to textbox after search? Codeigniter Ajax有没有办法在搜索后将数据显示到文本框?代码点火器阿贾克斯
【发布时间】:2020-03-22 17:34:03
【问题描述】:

从文本框中正确获取数据后,我需要在文本框中获取并显示 id。

我需要从文本框中查看其他表格字段数据

这是我的视图和 Ajax

   <div class="form-group">
        <h5>Class Code:</h5>
            <small><li>Only teachers shall give you code</li></small><br>
            <input type="text" id="code" class="form-control col-md-6" placeholder="Enter class code to join" name="code" required />
            <span id="code_result"></span>  
            <input type="text" name="id" id="id" class="form-control col-md-2" />
    </div>

Ajax - 所以我需要获取 id 但对于我的代码,它不显示

//Verify
$('#code').on("change keyup paste", function(){
    var code = $('#code').val();  
       if(code != '')  
       {  
            $.ajax({  
                 url:"<?php echo base_url(); ?>students/check_code_avalibility",  
                 method:"POST",  
                 data:{code:code},  
                 success:function(data){  
                      $('#code_result').html(data);  
                      $('input[name=id]').val(data.id); // I need to fetch this data
                 }  
            });  
       }    
});

Controller - 这里我验证数据是否正确

function check_code_avalibility()  
{  

          if(!$this->student_model->is_code_available($_POST["code"]))  
          {  
               echo '<label class="text-danger"><span class="glyphicon glyphicon-remove"></span> Invalid Code</label>';  
          }  
          else  
          {  
               echo '<label class="text-success"><span class="glyphicon glyphicon-ok"></span> Code Available </label><hr>';  
               echo '<button type="submit" class="btn btn-primary btn-md">Join to Class</button>'; 
          }  

}     

型号

function is_code_available($code)  
{  
     $this->db->where('code', $code);  
     $query = $this->db->get("groups");  
     if($query->num_rows() > 0)  
     {  
          return true;  
     }  
     else  
     {  
          return false;  
     }  
}  

【问题讨论】:

  • 您似乎是复制粘贴解决方案,但没有试图理解。你的data 是一个字符串,怎么会有id 的值?
  • @AnuragSrivastava 我已经对此进行了修改并清楚地理解了它。你能提供一个输出吗?谢谢。
  • 您对data.id 有什么期望?
  • 那么在您的代码中,从模型中检索 id 的查询在哪里?
  • @AnuragSrivastava 查询是什么意思?是这个$this-&gt;db-&gt;where('code', $code); $query = $this-&gt;db-&gt;get("groups");

标签: php jquery ajax codeigniter codeigniter-3


【解决方案1】:

要实现您想要的,您必须修改您的check_code_avalibility 函数以返回数据而不是回显html。当你在 javascript 中做这样的事情时:

.val(data.id)

这意味着您期待一个具有id 属性的对象。但是您的 PHP 函数没有返回对象。它返回一个字符串(您的 html 输出)。没有id,所以不能用。

如果你想为 javascript 返回一个对象,最好的方法是使用 JSON。请参阅json_encode example。因此,与其在函数中回显 html,不如将该 html 存储在某个键下的数组中:

$output['html'] = '<label class="... and so on';

请注意,如果您要回显多行,则需要在添加到数组之前将它们连接起来。在你的 javascript 中,这个 html 输出现在可以在 data.html 下使用(因为我是这样命名数组键的)。

此外,您需要将所需的 ID 添加到同一个数组中:

$output['id'] = $this->student_model->id;

如果您返回此数组的 JSON 编码版本,您将在 javascript 中同时获得 html 和 ID。

【讨论】:

  • 这让我有点困惑
  • 你也可以参考我的代码吗?我不能很好地理解这一点。我应该更换控制器还是什么?
  • 这不能回答我的问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多