将数据从父页调用到嵌套页


Called data from parent page to nested page

我正在使用ajax将php页面上的下拉菜单中的数据调用到嵌套的php页面。我可以从数据库中填充下拉菜单,但除了回显数据库数据之外,我无法使用corrisponding数据库数据。我试图让嵌套的php页面显示一个基于父页面下拉列表中引用的数据库的网站。

在控制台内部,我收到ReferenceError:showUser未定义。适用于rss.php

rss.php

<html>
<head>
<script>
function showRSS(str) {
  if (str.length==0) {
    document.getElementById("rssOutput").innerHTML="";
    return;
  }
  if (window.XMLHttpRequest) {
    // code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp=new XMLHttpRequest();
  } else {  // code for IE6, IE5
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
  xmlhttp.onreadystatechange=function() {
    if (xmlhttp.readyState==4 && xmlhttp.status==200) {
      document.getElementById("rssOutput").innerHTML=xmlhttp.responseText;
    }
  }
  xmlhttp.open("GET","getrss.php?q="+str,true);
  xmlhttp.send();
}
</script>
</head>
<body>

<form method="post" action="rss.php">
<select name="users" onchange="showUser(this.value)">
<option value="">Select a person:</option>
<?php
$con=mysqli_connect("localhost","user","password","table");
// Check connection
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM users");
while($row = mysqli_fetch_array($result)) {
    echo "<option value='" . $row['id'] . "'>" . $row['uname'] . "</option>";
mysqli_close($con);
}
?>
</select>
</form>
<br>
<div id="rssOutput">RSS Display</div>
</body>
</html> 

getrs.php

<?php
ini_set("display_errors",1); error_reporting(E_ALL);
//get the q parameter from URL
$q = intval($_GET['q']);
$con=mysqli_connect("localhost","user","password","table");
// Check connection
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$query = "SELECT * FROM users WHERE id ='".$q."'";
$result = mysqli_query($con,$query);

while($row = mysqli_fetch_array($result))
            {
$id = $row['id'];
$xml = $row['info2'];

$xmlDoc = new DOMDocument();
$xmlDoc->load($xml);
//get elements from "<channel>"
$channel=$xmlDoc->getElementsByTagName('channel')->item(0);
$channel_title = $channel->getElementsByTagName('title')
->item(0)->childNodes->item(0)->nodeValue;
$channel_link = $channel->getElementsByTagName('link')
->item(0)->childNodes->item(0)->nodeValue;
$channel_desc = $channel->getElementsByTagName('description')
->item(0)->childNodes->item(0)->nodeValue;
//output elements from "<channel>"
echo("<p><a href='" . $channel_link
  . "'>" . $channel_title . "</a>");
echo("<br>");
echo($channel_desc . "</p>");
//get and output "<item>" elements
$x=$xmlDoc->getElementsByTagName('item');
for ($i=0; $i<=2; $i++) {
  $item_title=$x->item($i)->getElementsByTagName('title')
  ->item(0)->childNodes->item(0)->nodeValue;
  $item_link=$x->item($i)->getElementsByTagName('link')
  ->item(0)->childNodes->item(0)->nodeValue;
  $item_desc=$x->item($i)->getElementsByTagName('description')
  ->item(0)->childNodes->item(0)->nodeValue;
  echo ("<p><a href='" . $item_link
  . "'>" . $item_title . "</a>");
  echo ("<br>");
  echo ($item_desc . "</p>");
}
}
?> 

这可能是从数据库表中提取的url如何显示的问题吗?我认为这并不是我想象中的那样。我使用的是通用的wamp服务器。

您的users下拉列表在更改时绑定到showUser,但您的帖子中只有showRSS函数。除非是这样,否则将代码更改为使用showRSS应该是可行的。

<select name="users" onchange="showRSS(this.value)">