在一个PHP文件中,我解析数据库(“ Chat”)中的数据,并以XML格式回显所有数据。然后,在我的index.php文件中,如果单击一个按钮,我将执行ajax请求以从XML检索数据。为什么我失败了?将数据从query.php文件回显到index.php文件是否正确?
query.php
#connect to database
$servername = "localhost";
$username = "root";
$password = "root";
$db_name = "Concerts";
try
{
$db = new PDO("mysql: host=$servername; dbname=$db_name", $username, $password);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e)
{
echo "Connection failed: " . $e->getMessage();
die;
}
$result = $db->query("SELECT * FROM events");
echo '<?xml version="1.0" encoding="UTF-8"?>
<event>';
foreach ($result as $row)
{
echo "<title>" . $row['title'] . "</title>";
echo "<details>" . $row['details'] . "</details>";
echo "<date>" . $row['date'] . "</date>";
echo "<place>" . $row['place'] . "</place>";
}
echo "</event>";
?>
index.php
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<button onclick="myFunc()"> Click </button>
<script type="text/javascript" src="prototype.js"></script>
<script>
function myFunc()
{
new Ajax.Request('query.php',
{
method: "get",
onSuccess: function(ajax)
{
ajax.getElementsByTagName("title");
},
onFailure: function() { alert("failure"); },
onException: function() { alert("exception"); }
}
);
}
</script
</body>
</html>