意外的会话标识符php

So I looked into other threads but didnt find a solution that worked for me. Here is my problem: I have two php pages: http://codepad.org/VhblM76K and http://codepad.org/W9bz8L3E.

The first page is supposed to get information from a form, look for it in a database, store it in a variable $_SESSION['$dataArray'] and send it to the second page.

On the second page I get the information in javascript from php with json_encode, which gives an error:

Uncaught SyntaxError: Unexpected token < result.php:20.

When I look in the source in chrome it says:

var schoolData = <br />
<b>Notice</b>: Undefined index: $dataArray in <b>C:\xampp\htdocs\highschools.bgesult.php</b> on line <b>23</b><br />
null;

How is this an unidentified index, when i can only go to the second page after visiting the first one, where I assing a value to $_SESSION['$dataArray'].

How can I fix this? I have written session_start() in both pages and it didnt work for me. I need the variable schoolData to show the information on the page.

The reason why you're getting the error is because you're not setting any session data, because you are redirecting directly to the second page from the form.

You need to update the form so it redirects back to the same page the form is on, then replace your PHP code with the following:

if (isset($_POST['submit'])) {

    $user = 'root';
    $pass = '';
    $db = 'highschools';

    $con = mysqli_connect('localhost', $user, $pass, $db) or die("Unable to connect");
    if (mysqli_connect_errno()) {
        echo("Failed to connect to MySQL: " . mysqli_connect_error());
    }

    $givenCity = $_POST['city'];
    $givenClass = $_POST['class'];
    $givenName = $_POST['name'];
    $result = mysqli_query($con,
        "SELECT * FROM highschools WHERE name like '%$givenName%' AND city like '%$givenCity%' AND class like '%$givenClass%'; ") or die(mysqli_error($con));

    $row_count = mysqli_num_rows($result);

    $_SESSION['dataArray'] = array();
    while($row_count > 0) {
        $curRow = mysqli_fetch_array($result);
        $_SESSION['dataArray'][] = $curRow;
        $row_count--;
    }

    header('location: http://YOUR_SECOND_PAGE_ADDRESS');

Notice, the only line I've added is the last line, hedaer('location: ... ');

Don't forget to change the http://YOUR_SECOND_PAGE_ADDRESS to the actual page address of your second piece of code.

Then in the second page, replace $_SESSION['$dataArray'] with $_SESSION['dataArray']

and it should work.

Your problem is that you are writing the session as $_SESSION['$dataArray']. If you have a variable somewhere called $dataArray then you need to write it $_SESSION[$dataArray].

Alternately, if you don't have a variable named $dataArray, then you need to write it as $_SESSION['dataArray'].

EDIT: Try print_r($_SESSION) beneath the session_start() at the top of the page and see if you have set that the $_SESSION['dataArray']:

<?php
    session_start();
    // See if the session contains your $_SESSION['dataArray']
    // If not, you can force it to have a default value to avoid errors
     print_r($_SESSION);
    $_SESSION['dataArray'] = (isset($_SESSION['dataArray']))? $_SESSION['dataArray']:array(); ?>
<!DOCTYPE html>
<html>
    <head>
        <title>Намерени училища</title>
        <meta charset="utf-8">
        <link rel="stylesheet" type="text/css" href="styles/main.css">
    </head>
    <body>
        <div id="contentWrapper" style="height:1000px;">
            <header id="pageHeader">
            </header>
            <section id="schoolsContainer">
                <ul id="schoolsList"></ul>
            </section>
        </div>

        <script type="text/javascript" src="handlebars-v2.0.0.js"></script>
        <script type="text/javascript" src="navTemplate.js"></script>
        <script type="text/javascript"> 
            var schoolData = <?php echo json_encode($_SESSION['dataArray']) ?>;
            for (var i = 0; i < schoolData.length; i++) {
                console.log(schoolData[i]['name']);
                console.log(schoolData[i]['id']);
            }
        </script>


    </body>
</html>

On the other page, this also has to be set before it works:

$_SESSION['dataArray'] = array();
      while($row_count > 0) {
                $curRow = mysqli_fetch_array($result);
                $_SESSION['dataArray'][] = $curRow;
                $row_count--;
        }