【问题标题】:Undefined variable ajax未定义的变量ajax
【发布时间】:2016-06-17 18:14:35
【问题描述】:

这个问题有点像错误。我不知道这段代码中的问题。
我正在使用 ajax 来显示我的表格。它说未定义的变量:输出。
这是我的代码:

控制器帐户:

function get_accounts()
{
    $accounts=$this->Model_admindly->get_accounts();
    $output.= '
      <table id="example" class="table table-striped table-bordered" cellspacing="0" width="100%">
              <thead>
                  <tr>
                      <th>ID</th>
                      <th>Email</th>
                      <th>Username</th>
                      <th>Last Logged</th>
                      <th>Last IP</th>
                      <th>Date Reg</th>
                      <th>Status</th>
                      <th>Actions</th>
                  </tr>
              </thead>
              <tfoot>
                  <tr>
                      <th>ID</th>
                      <th>Email</th>
                      <th>Username</th>
                      <th>Last Logged</th>
                      <th>Last IP</th>
                      <th>Date Reg</th>
                      <th>Status</th>
                      <th>Actions</th>
                  </tr>
              </tfoot>
              <tbody>';
    foreach($accounts as $key)
    {
        $output.= '<td>'.$key->id.'</td>
                     <td>'.$key->email.'</td>
                     <td>'.$key->username.'</td>
                     <td>'.$key->date_lastlogged.'</td>
                     <td>'.$key->last_ipaddress.'</td>
                     <td>'.$key->date_registered.'</td>
                     <td>'.$key->status.'</td>
                     <td>'.$key->status.'</td>';
    }
    $output.='</table>';
    echo $output;
}

使用 ajax 显示它:

<script>
  $(document).ready(function(){
    function fetch_data()
    {
       $.ajax({
        url:"<?php echo base_url()?>Accounts/get_accounts",
        method:"POST",
        success:function(data)
        {
          $("#live_data").html(data);
        }
       });
    }
    fetch_data();
  });
</script>

【问题讨论】:

标签: php jquery ajax codeigniter


【解决方案1】:

您正在尝试与尚未定义的变量连接:

$output .= '<table...

因为这是你第一次引用这个变量,所以直接赋值:

$output = '<table...

这将删除Notice: Undefined variable: output 消息。

【讨论】:

    【解决方案2】:

    在尝试 .= 之前,您从未初始化 $output

    基本上你有

    $output = $output . 'foo';
                  ^---undefined at this point
    

    导致错误。你需要有

    $output = ''; // create/initialize
    $output .= 'foo'; // use
    

    【讨论】:

      【解决方案3】:

      您没有显示完整的消息,但我怀疑您收到了通知?无论如何,如果你这样做:

      $output.= '...
      

      然后你说“取变量output并添加这个字符串。”

      如果变量不存在,您会收到通知/警告!

      您应该从第一次开始这样做:

      $output = '....
      

      或者初始化它

      $output = '';
      

      【讨论】:

        【解决方案4】:

        简单:

        在创建变量 $output 之前,您正在运行 $output .= "blabla";

        先创建它,然后您就可以将字符串连接到它。

        function get_accounts()
        {
            $output  = "";
            $accounts=$this->Model_admindly->get_accounts();
            $output.= '
        

        或者简单地删除它的第一个引用上的点:

        function get_accounts()
        {
            $accounts=$this->Model_admindly->get_accounts();
            $output = '
        

        【讨论】:

          猜你喜欢
          • 2017-11-18
          • 2023-01-26
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-06-11
          • 1970-01-01
          • 1970-01-01
          • 2023-03-18
          相关资源
          最近更新 更多