从父页面到嵌套页面的调用数据

时间:2014-09-13 02:38:43

标签: php ajax xml rss

我正在使用ajax从php页面上的下拉菜单调用数据到嵌套的php页面。我可以从数据库填充下拉菜单,但我无法使用相应的数据库数据,而不是回显它们。我试图让嵌套的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> 

getrss.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>");
}
}
?> 

这可能是一个问题,如何显示从数据库表中绘制的网址,我认为不会像我想象的那样显示它的字面意思。我正在使用一般的wamp服务器。

1 个答案:

答案 0 :(得分:0)

您的users下拉列表与showUser次更改绑定,但您的帖子中只有showRSS个功能。除非是这样,否则更改代码以使用showRSS应该有效。

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