gpt4 book ai didi

php - 使用php从数据库中检索值后如何使用其id获取div的值

转载 作者:行者123 更新时间:2023-11-28 01:45:30 24 4
gpt4 key购买 nike

我正在尝试使用 Simple HTML Dom Parser 获取 div 元素的值。它仅适用于硬编码值。这是硬编码的东西。

<table>
<tr><td class='none'><div class='drag' id='d1'>1</div></td></tr>
<tr><td class='none'><div class='drag' id='d2'>2</div></td></tr>
<tr><td class='none'><div class='drag' id='d3'>3</div></td></tr>

除了这些常量值外,我还需要获取根据下拉菜单中的选择显示的其他 id 值。

<form method="post" name="order" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<select id='test' name='test'>
<option value="">Please Select</option>
<option value='test1'>Test1</option>
<option value='test2'>Test2</option>
</select>
</form>

选择后,他们单击继续按钮,将他们重定向到同一页面,这是我的脚本,显示来自数据库的 ID。

<?php
$i=4;
if ($_SERVER['REQUEST_METHOD']=="POST")
{
$query = mysql_query("select * from test_demo where test_requested='$_POST[test]'");
while($result=mysql_fetch_array($query))
{
echo "<tr><td class='none'><div class='drag' id='d$i'>". $result['oid'] ."</div></td></tr>";
$i++;
}
}
echo "</table>"
?>

现在我想从数据库中获取订单 ID 的 1、2、3 和剩余的 ID。为了获得这些值,我在单击时使用“值”按钮转到 retrieve.php 文件。

在 retrieve.php 文件中,我使用简单的 html dom 解析器来获取值。这是代码。

<?php
include_once('simple_html_dom.php');
//test.php contains my hard coded html and php code
$html = file_get_html('test.php');
$temp = '1';
//usig div name we are retrieving the values
foreach($html->find('div#d5') as $e)
$result[]= $e->innertext;
print_r($result);

?>

如果我可以提及 d1 或 d2 或 d3,则代替 d5。我可以分别得到值 1、2、3。但是我无法获取从数据库中检索到的订单 ID 值。这些元素的 div id 从 d4 开始,依此类推。我可能哪里出错了?

最佳答案

有几个问题。

首先,如果您将 PHP 脚本作为普通文件访问,则它们不会运行。您需要更改 URL 才能通过服务器:

$html = file_get_html('http://localhost/path/to/test.php');

其次,您需要在 URL 中提供 test 参数的值,因此它应该是:

$html = file_get_html('http://localhost/path/to/test.php?test=test1');

第三,在 test.php 中,您需要使用 $_GET 访问参数,而不是 $_POST,当以这种方式访问​​参数时:

if ($_SERVER['REQUEST_METHOD'] == "GET") {
$test = mysql_real_escape_string($_GET['test']);
$query = mysql_query("select * from test_demo where test_requested='$test'");
...
}

关于php - 使用php从数据库中检索值后如何使用其id获取div的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22998245/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com