通过ajax发送PHP变量

时间:2022-09-26 10:56:57

I want to send the result of an SQL query which is a PHP variable to the index file. Here's from the PHP I want to send from:

我想将SQL查询的结果作为PHP变量发送到索引文件。这是我想发送的PHP:

$result = mysqli_query($conn, $sql);
$remaining = array();
while($row = mysqli_fetch_array($result)){
array_push($remaining, $row['_id']);
}
echo json_encode($remaining);

?>
<script>
      $.ajax({
        url: "index.php", 
        data: {result: result}, 
        dataType: "json",
        success: function(result){
}});
</script>

Here is where I want to use the code:

这是我想要使用代码的地方:

$var_value = $_GET['remaining'];
$values = implode(", ", $var_value);

$sql = "SELECT *
        FROM `species` 
        WHERE `_id` IN (".$valus.") ";

$results = mysqli_query($conn, $sql);

I have tried many methods, running a second ajax call within one already running since the $remaining variable is sent back to a javascript using ajax. $_cookies doesn't work for my webserver. So this seems the best solution. Can anyone see what is going wrong?

我已经尝试了很多方法,在已经运行的一个中运行第二个ajax调用,因为$ remaining变量使用ajax发送回javascript。 $ _cookies对我的网络服务器不起作用。所以这似乎是最好的解决方案。任何人都可以看到出了什么问题?

1 个解决方案

#1


As reply to your comment: easy:

回复你的评论:容易:

session_start(); // important!!

if ( !isset($_SESSION['remaining']) )
{
    $result = mysqli_query($conn, $sql);
    $remaining = array();
    while($row = mysqli_fetch_array($result)){
        array_push($remaining, $row['_id']);
    }
    $_SESSION['remaining'] = $remaining;
}
else
{
    $values = implode(",", $_SESSION['remaining']);
    /// $_SESSION['remaining'] = null - depends on logic.
    $sql = "SELECT *
        FROM `species` 
        WHERE `_id` IN (".$valus.") ";

    $results = mysqli_query($conn, $sql);
}

#1


As reply to your comment: easy:

回复你的评论:容易:

session_start(); // important!!

if ( !isset($_SESSION['remaining']) )
{
    $result = mysqli_query($conn, $sql);
    $remaining = array();
    while($row = mysqli_fetch_array($result)){
        array_push($remaining, $row['_id']);
    }
    $_SESSION['remaining'] = $remaining;
}
else
{
    $values = implode(",", $_SESSION['remaining']);
    /// $_SESSION['remaining'] = null - depends on logic.
    $sql = "SELECT *
        FROM `species` 
        WHERE `_id` IN (".$valus.") ";

    $results = mysqli_query($conn, $sql);
}