2

I'm currently working with these two parts of code.

One is for receiving the data and one for displaying it in a list, but for some reason the variable $emp_name always returns null when I'm trying to display it in the second part.

The idea is to search an employee and then displaying his data.

Receiving data:

if (isset($_POST['search_button'])) {
$cnt = new Connector();
$employee = new Search($cnt);
$name = $_POST['name'];
$found = array('name' => $employee->getEmployeeByName($name)[0]['name']);
$emp_name = $found['name'];
global $emp_name;


     $display =  "<br><br>


    <table class='table table-striped'>
    <thead>
      <tr>
        <th width='100%';>Name</th>
        <th width=100%;>Action</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td width=100%;>$emp_name</td>
        <td width=100%;><div class='container'>                               
  <div class='dropdown'>

    <button class='btn btn-default dropdown-toggle' type='button' id='menu1' data-toggle='dropdown'>Please choose
    <span class='caret'></span></button>
    <ul class='dropdown-menu' role='menu' aria-labelledby='menu1'>
      <li role='presentation'><a role='menuitem' tabindex='-1'href='index.php?action=data'>Daten anzeigen</a></li>
      <li role='presentation'><a role='menuitem' tabindex='-1' href='#'>Send message to $emp_name</a></li>
    </ul>
  </div>
</div>
</td>

      </tr>
    </tbody>
  </table>




";

         $_SESSION = $display;
}



?>


Displaying it in a list:

<?php


print_r($_SESSION);

if (isset($_SESSION['action'])) {

    echo "
<h4>Employee Data</h4><br>
    <ul class='nav nav-tabs'>
        <li class='active'><a data-toggle='tab'href='#sectionA'>Address</a></li>

        <li><a data-toggle='tab' href='#sectionB'>Personal Data</a></li>

       <li><a data-toggle='tab' href='#sectionB'>Communication Data</a></li>
            </ul>

        </li>

    </ul>

    <div class='tab-content'>

        <div id='sectionA' class='tab-pane fade in active'>

            <ul class='list-group'>
  <li class='list-group-item'>First Name ".$emp_name."
  </li>
  <li class='list-group-item'>Last Name</li>
  <li class='list-group-item'>Street</li>
  <li class='list-group-item'>Zip</li>
  <li class='list-group-item'>City</li>
  <li class='list-group-item'>Country</li>
</ul>

        </div>

        <div id='sectionB' class='tab-pane fade'>

            <p>Section B content…</p>

        </div>

        <div id='dropdown1' class='tab-pane fade'>

            <p>Dropdown 1 content…</p>

        </div>

        <div id='dropdown2' class='tab-pane fade'>

            <p>Dropdown 2 content…</p>

        </div>

    </div>
    ";
}





?>
Hexaholic
  • 3,299
  • 7
  • 30
  • 39
Jeremy
  • 270
  • 5
  • 19
  • 1
    Storing the variable as a global doesn't help here. You need to put it in a session variable to make it available across pages in your site. – Jay Blanchard May 13 '15 at 13:23
  • Are these two separate pages or two bits of code of the same page? If they're separate pages global variables don't get saved across pages, you need to use `$_SESSION` for that. – Styphon May 13 '15 at 13:24
  • It's on the same page. I've tried it with a session:
    $_SESSION['data'] = $emp_id; and then outputting it like this :
  • First Name ".$_SESSION['data']."
    And it still retuns null
  • – Jeremy May 13 '15 at 13:27
  • Are these in 2 different functions? – DMcP89 May 13 '15 at 13:45
  • @BeardFace just in 2 different if statements, just like shown in the code. – Jeremy May 13 '15 at 13:49
  • I don't see `session_start()` anywhere in the code. Will need that early in the php script in order to store/retrieve `$_SESSION` vars. – Adam T May 13 '15 at 14:34
  • @AdamT But I have already different sessions? Will it affect them? – Jeremy May 13 '15 at 14:37
  • I would also try declaring `global $emp_name;` first, and then assigning a value to it. Better yet, why not use `define()` function? – Adam T May 13 '15 at 14:38
  • @JeremyPüringer what do you see when `print_r($_SESSION)` ? – Adam T May 13 '15 at 14:38
  • @AdamT An array from a different session – Jeremy May 13 '15 at 14:40
  • @JeremyPüringer as long as the $_SESSION [varname] is not already being used, its value won't be modified unexpectedly, so you should be able to append a unique var to the session. – Adam T May 13 '15 at 14:41
  • @AdamT Understood, thanks! When doing session_start() do I have to put the varname somewhere or how will it recoginize to start this specific session? – Jeremy May 13 '15 at 14:45
  • @JeremyPüringer I would also advise checking to see if a session is already started. `if(isset($_SESSION)) { ...` then you should be able to read/write to session. If session is not started you will get unexpected results if trying to use Session vars. However you mentioned you do get an array from a different session. Check out http://php.net/manual/en/function.session-start.php – Adam T May 13 '15 at 14:49
  • Forgot to ask. Is an actual data value stored in `$emp_name` ? – Adam T May 13 '15 at 14:53
  • @JeremyPüringer the line ` $_SESSION = $display;` I would change that to something like `$_SESSION['display'] = $display;` – Adam T May 13 '15 at 14:54