-2

This problem is kind of a bug. I don't know the problem in this code.
I'm using ajax to display the my table. and it said undefined variable: output.
Heres my code :

Controller Accounts:

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;
}

To display it using 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>
Jonas Dulay
  • 283
  • 4
  • 18
  • You have an XSS vulnerability; – SLaks Jun 17 '16 at 18:16
  • 1
    Possible duplicate of [PHP: "Notice: Undefined variable" and "Notice: Undefined index"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) – Phiter Jun 17 '16 at 18:21

4 Answers4

4

You are trying to concatenate with a variable that hasn't been defined yet:

$output .= '<table...

Since this is the first time you are referencing this variable, just do a direct assignment:

$output = '<table...

That will get rid of the Notice: Undefined variable: output message.

jszobody
  • 28,495
  • 6
  • 61
  • 72
3

You never initialized $output before you tried to .= it.

Essentially you have

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

causing the error. You need to have

$output = ''; // create/initialize
$output .= 'foo'; // use
Marc B
  • 356,200
  • 43
  • 426
  • 500
2

You didn't display the complete message, but I suspect you got a notice? Anyway, if yo do this:

$output.= '...

then you say "take the variable output and add this string."

If the variable doesn't exist, you get a notice/warning about it!

You should either start with the first time doing this:

$output = '....

Or initialise it with

$output = '';
Nanne
  • 64,065
  • 16
  • 119
  • 163
1

Easy:

You're running $output .= "blabla"; before you even create the variable $output.

Create it first, then you'll be able to concatenate strings to it.

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

Or simpl remove the dot on it's first reference:

function get_accounts()
{
    $accounts=$this->Model_admindly->get_accounts();
    $output = '
Phiter
  • 14,570
  • 14
  • 50
  • 84