duanhuan6336 2014-09-04 22:20
浏览 51
已采纳

意外的会话标识符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.

  • 写回答

2条回答 默认 最新

  • duancheng6500 2014-09-05 14:23
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?