我正在尝试从为每个循环创建的链接传递PHP变量。
以下是生成要发送的链接和变量的页面上的代码:
$xmlDoc = simplexml_load_file("products.xml");
$storeArray = array();
foreach($xmlDoc->product as $Product) {
echo "Name: " . $Product->name . ", ";
echo "Price: " . $Product->price . ", ";
$store = (string)$Product->store;
if (!array_key_exists($store, $storeArray)) {
$storeArray[$store] = "<a href='searchResults.php?storeSearch=<?php echo $store; ?>'>" .
$store . "</a>";
}}
foreach ($storeArray as $store) {
echo $store . "<br>";
}
以下是接收变量的页面上的代码: $ searchByStore = $ _GET [“storeSearch”]; echo“存储搜索变量是:”。 $ searchByStore;
未回显searchByStore变量。有什么建议?
以下是此网址在浏览器中的显示方式: ?... / searchResults.php storeSearch =%3C PHP%20echo%20Best%20Buy;?%20%3E 而不是正常的方式,这是: ... / searchResults.php?storeSearch = Best%20Buy
这是XML文件:
<product type="Electronics">
<name> Desktop</name>
<price>499.99</price>
<store>Best Buy</store>
</product>
<product type="Electronics">
<name>Lap top</name>
<price>599.99</price>
<store>Best Buy</store>
</product>
<product type="Hardware">
<name>Hand Saw</name>
<price>99.99</price>
<store>Lowes</store>
</product>
</products>
答案 0 :(得分:0)
您无法在PHP代码中嵌入PHP代码:
$storeArray[$store] = "<a href='searchResults.php?storeSearch=<?php echo $store; ?>'>" .
^^^^^^^^^^^^^^^^^^^^^
应该是
$storeArray[$store] = "<a blah blah storeSearch=" . $store . ">";
或
$storeArray[$store] = "<a blah blah storeSearch={$store}>";
答案 1 :(得分:0)
这是错误的:
$storeArray[$store] = "<a href='searchResults.php?storeSearch=<?php echo $store; ?>'>" .
$store . "</a>
";
将其更改为
$storeArray[$store] = "<a href='searchResults.php?storeSearch=".$store."'>".$store."</a>";