简体   繁体   English

$ _SESSION变量显示意外行为

[英]$_SESSION variables showing unexpected behaviour

I'm currently working on a table with onclick() <td> elements that send info to a PHP page. 我目前正在处理带有onclick() <td>元素的表,该元素将信息发送到PHP页面。

The basic idea is that if a course is clicked (egFrench, Maths, English, ...) the PHP page retrieves the value and afterwards executes the SQL code to give me all of the tutors for that specific course. 基本思想是,如果单击一门课程(例如法语,数学,英语等),则PHP页面将检索该值,然后执行SQL代码以为该课程提供所有的辅导老师。

<script>
    function courseToTutor(id) {
        var courseClicked = document.getElementById("course" + id).value;
        $.post('loadDivs/loadTutors.php', {postname: courseClicked});
        $('#divTutors').load('loadDivs/loadTutors.php');
    }
</script>

When a <td> is clicked, it sends its id (generated in php) as a parameter. 单击<td> ,它将发送其ID(在php中生成)作为参数。 Inside the function I use that parameter to find the values within the <td> (stored inside an input type="hidden" ). 在函数内部,我使用该参数在<td> (存储在input type="hidden" )中查找值。

I have a single PHP page that both gets the posted value, like this: 我有一个PHP页面,都可以获取发布的值,如下所示:

if (isset($_POST['postname'])) {
    $course = $_POST['postname'];
    $_SESSION['clickedCourse'] = $course;
    echo $course;
}

The other part of the page is 页面的另一部分是

else {
  //generate table with tutors for a specific course (with SQL)
}

This works about 85% of the time. 大约有85%的时间有效。 Yes, I use correct session_start(); 是的,我使用正确的session_start(); s, I have a proper database connection and so on. s,我有正确的数据库连接,依此类推。

When I click a <td> , I have an alert on the loadTutors.php that always triggers, it alerts my $_SESSION['clickedCourse]; 单击<td>总是触发的loadTutors.php上有一个警报,它警报我的$_SESSION['clickedCourse]; .

85% of the time this alert show the correct info, the other 15% of the time, it is empty. 此警报有85%的时间显示正确的信息,其他15%的时间为空。

It's not my other code on the page, because I trigger the alert before I execute that code. 这不是页面上的其他代码,因为我在执行该代码之前会触发警报。

Thanks in advance 提前致谢

EDIT This is the php I use to generate the WHOLE table of courses. 编辑这是我用来生成整个课程表的php。

<?php
$counter = 1;
if (!$res = $link->query($sqlSelectCourses)) {
    trigger_error('Error in query ' . $link->error);
} else {
    while ($row = $res->fetch_assoc()) {
        ?>
        <tr>
            <td onclick="courseToTutor(this.id);" id="<?php echo $counter; ?>">
                <?php echo $row["course"]; ?><input type="hidden" value="<?php echo $row["course"]; ?>" id="course<?php echo $counter; ?>">
            </td>
        </tr>
        <?php
        $counter+=1;
    }
}
?>
 $.post('loadDivs/loadTutors.php', {postname: courseClicked});
 $('#divTutors').load('loadDivs/loadTutors.php');

You are sending two requests, one to send data and one to receive new data. 您正在发送两个请求,一个发送数据,另一个接收新数据。 It can happen, that the second requests reaches the server first and i think this is what happens 15% the time (therefore wrong result). 可能会发生,第二个请求首先到达服务器,我认为这是15%的时间(因此结果错误)。 What you should do instead is using the first request and use its response: 相反,您应该使用第一个请求并使用其响应:

 $.post('loadDivs/loadTutors.php', {postname: courseClicked}, function(response){
    $('#divTutors').html(response);
});

Edit: If you can't set your PHP-script up so it sends your new content directly at the "click-request", use this instead: 编辑:如果您无法设置PHP脚本,那么它将直接在“单击请求”处发送新内容,请改用此命令:

 $.post('loadDivs/loadTutors.php', {postname: courseClicked}, function(){
    $('#divTutors').load('loadDivs/loadTutors.php');
});

This will wait for the first request to finish before it starts the second. 这将等待第一个请求完成,然后再启动第二个请求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM