【问题标题】:Retrieving data from PHP using AJAX to send to different PHP file使用 AJAX 从 PHP 中检索数据以发送到不同的 PHP 文件
【发布时间】:2014-03-20 14:28:50
【问题描述】:

好的,我有:

查询数据库并显示查询结果的 PHP 文件, 使用 AJAX 显示结果的 HTML 文件和 我需要将数据从第一个 PHP 文件发送到的另一个 PHP 文件。

我该怎么做?

我的第一个 PHP 文件显示:

$result = mysqli_query($db_server, $query); 
    if (!$result) die("Database access failed: " . mysqli_error($db_server)); 
    while($row = mysqli_fetch_array($result)){ 
$str_shopresult .= "<div class='result'><a id='shoplink' href='#shop'><strong><div id='hiddenid'>" .
$row['id'] . "</div>" .  
$row['name']  . "</strong><br><br>" .
$row['address'] . "<br><br><i>" . 
$row['sold'] . "</i></div></a>"; 
} 

所以 AJAX 将其引入并显示在 HTML 中。有没有办法在单击链接时发送 ID 字段,以便在另一个 PHP 文件中使用它来显示该特定 ID 的数据。

$( "#result" ).click(function() {
$.ajax({
    type: "POST",
    url: "external-data/shop.php",
    data: $("#hiddenid"),
    success: function (data) {
    $("#individualshop").load("external-data/shop.php");
    }
});

这样来自#hiddendiv ($row ['ID']) 的数据将被发送到新的PHP 文件。这不起作用。

shop.php 有代码:

//get shop ID
    $shopid = $_POST['ID'];

    mysqli_select_db($db_server, $db_database);
    $query = "SELECT * FROM shops WHERE ID='$shopid'"; 
    $result = mysqli_query($db_server, $query); 
        if (!$result) die("Database access failed: " . mysqli_error($db_server)); 
        while($row = mysqli_fetch_array($result)){ 
    $str_shops .= "<div class='searchresult'><strong>" . 
    $row['name']  . "</strong><br><br>" .
    $row['address'] . "<br><br><i>" . 
    $row['sold'] . "</i></div>"; 
 } 

但我不确定如何检索 ID?

基本上我需要能够点击每个搜索结果并让另一个 PHP 文件引入与搜索结果 ID 相关联的特定数据。有什么建议吗??

提前致谢!

【问题讨论】:

  • “这不起作用”是什么意思? external-data/shop.php 上的内容是什么?
  • @leonardo 我更新了上面的帖子,对不起!
  • 你通过传递这个来检索 ID:data: { id: $("#hiddenid")}, 而不是 data: $("#hiddenid"),
  • @Kathryn 看到我的回答,这个 $("#individualshop").load("external-data/shop.php"); 是为了 ajax 成功吗?
  • @HüseyinBABAL 是引入 shop.php 查询的结果,但我认为我应该使用 $("#individualshop").html(data); ?

标签: javascript php jquery mysql ajax


【解决方案1】:

我不能 100% 确定这是您想要的,但这是一种方式。

这是您的第一个 php 文件。我已经为其添加了一个 onclick 监听器,因此当用户单击一行时,它将触发一个传递 $row['id'] 变量的 JavaScript。

$result = mysqli_query($db_server, $query); 
if (!$result) die("Database access failed: " . mysqli_error($db_server)); 
while($row = mysqli_fetch_array($result)){ 
$str_shopresult .= "<div onclick='someFunction(" . $row['id'] . ")' class='result'><div id='shoplink' href='#shop'><strong><div id='hiddenid'>" .
$row['id'] . "</div>" .  
$row['name']  . "</strong><br><br>" .
$row['address'] . "<br><br><i>" . 
$row['sold'] . "</i></div></div>"; 
}

然后将此脚本添加到页面头部。

<script>
function someFunction(d){
var response = httpGet("http://yourdomain.com/newphpfile.php?page_id=" +d);

document.getElementById('someResultArea').innerHTML = response;

}
function httpGet(theUrl)
    {
        var xmlHttp = new XMLHttpRequest();
            xmlHttp.open( "POST", theUrl, false );
            xmlHttp.send();
        return xmlHttp.responseText;
    }
</script>

那么你的新 php 文件应该做这样的事情。

newphpfile.php

<?php 
session_start();

$page_id = strip_tags($_GET['page_id']);

$processResult = 'handle your result';

echo $processResult;

?>

希望对你有所帮助。

【讨论】:

  • 由于某种原因,它不会让我在php文件中添加onclick函数
  • 我修正了我的语法。再试一次。
  • 谢谢。尽管当我单击结果并回显 $page_id 时,它作为 undefined
  • 它在哪里未定义?当您的第一页出现时,您应该能够检查该行的 html 并查看侦听器中是否写入了正确的行 ID。此外,您可以切换到控制台选项卡并观察您的 JS 触发,看看是否是问题所在。
  • 在新的 php 文件中,当我单击结果并在新的 php 文件中回显 id 时,它只是说未定义。检查元素时,它显示
    括号中没有任何内容,然后在新页面上显示未定义。顺便说一句,感谢您的帮助!
【解决方案2】:

使用这个;

$( "#result" ).click(function() {
    $.ajax({
        type: "POST",
        url: "external-data/shop.php",
        data: "ID=" + $("#hiddenid"),
        success: function (data) {
            $("#individualshop").html(data);
        }
    });
});

注意:不要忘记在shop.php返回html

【讨论】:

    【解决方案3】:

    您可以使用javascript或jquery获取点击的元素id,并将其放入php链接的GET参数中

    ajax 目标是 ajax.php?id=test

    然后通过$_GET['id']获取id的值

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-02
      • 1970-01-01
      • 1970-01-01
      • 2012-04-08
      • 1970-01-01
      • 2012-01-04
      相关资源
      最近更新 更多