从$ _GET方法的ajax中的php文件的头中获取值

时间:2018-08-24 17:33:42

标签: php ajax get

您好朋友,我是AJAX的初学者,因此我遇到了一个问题,即我无法从通过$_GET方法发送的页面标题中获取值。帮助我,请找到解决方案

这是我的PHP代码

<?php include '../../db_connect/connection.php'; ?>

<?php

$select = "SELECT * FROM leave_requests WHERE teacher_name = '$teacher_id' ORDER BY id DESC";
$select_result = mysqli_query($connection,$select);
while ($row = mysqli_fetch_assoc($select_result)) {
    $date = $row['date'];
    $teacher = $row['teacher_name'];
    $days = $row['days'];
    $reason = $row['reason'];
    $status = $row['status'];
    $type_of_leave = $row['type_of_leave'];

    $principal_remarks = $row['principal_remarks'];
    $id = $row['id'];
    ?>

    <td><?php echo $date; ?></td>
    <td><?php echo $days; ?></td>
    <td><?php echo $type_of_leave; ?></td>
    <td><?php echo $reason; ?></td>


    <td><?php echo $status; ?></td>
    <td><?php echo $principal_remarks; ?></td>
    </tr>

<?php
} ?>

这是我不完整的AJAX代码

<script type="text/javascript">
$(document).ready(function(){
    $.ajax({
        url:'display_leave_request_response.php',
        data:
    });
});
</script>

如何使用Ajax捕获标头中的get请求中的值,请解释

很抱歉,由于系统未接受该问题,因此我必须这样做

如果代码中有任何错误,请向我解释

1 个答案:

答案 0 :(得分:0)

data:选项设置为包含要发送的参数的对象:

data: { teacher_id: some_variable }

然后在PHP中可以使用:

$teacher_id = $_GET['teacher_id'];

您还应该学习使用准备好的语句,而不是将$teacher_id变量直接替换为SQL,以防止SQL注入。参见How can I prevent SQL injection in PHP?