我收到一个 ajax 错误。这是我的 buy.php 页面,用户可以在其中导航下拉菜单来选择产品。一旦他们选择它(通过 ajax),数据库中的所有内容都将被加载:
<?php
session_start();
$Name = $_POST['name'];
$Pass = $_POST['password'];
//STEP 1 Connect To Database
$host= "localhost";
$dbname= "register";
$user = "root";
$pass = "";
try {
# MySQL with PDO_MYSQL
$DBH = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);
//$DBH->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
$STH = $DBH->query("SELECT username, password from tbl_users");
$STH->execute();
//STEP 2 Declare Variables
$Query = $DBH->query("SELECT * FROM tbl_users WHERE username='$Name' AND password='$Pass'");
$Query->execute();
$Query->setFetchMode(PDO::FETCH_NUM);
$NumRows = $Query->fetch();
$_SESSION['name'] = $Name;
$_SESSION['password'] = $Pass;
//STEP 3 Check to See If User Entered All Of The Information
if(empty($_SESSION['name']) || empty($_SESSION['password']))
{
die("could not connect");
}
if($Name && $Pass == "")
{
die("Please enter a name and password!");
}
if($Name == "")
{
die("Please enter your name!" . "</br>");
}
if($Pass == "")
{
die("Please enter a password!");
echo "</br>";
}
//STEP 4 Check Username And Password With The MySQL Database
if($NumRows != 0)
{
$STH->setFetchMode(PDO::FETCH_ASSOC);
while($Row = $STH->fetch())
{
$dname = $Row['username'];
$dpass = $Row['password'];
}
}
else
{
if( $_SESSION['name']!= $dname || $_SESSION['password'] != $dpass)
{
header("location: login.php");
}
}
if($Name == $dname && $Pass == $dpass)
{
// If The User Makes It Here Then That Means He Logged In Successfully
echo "Hello " . $Name . "!";
}
}
catch(PDOException $e) {
echo "I'm sorry, database connection is wrong.";
$e->getMessage();
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" type="text/javascript"></script>
<script>
$(document).ready(function(){
$('#pro').bind('change', function (e){
var value = $(this).val();
e.preventDefault(e)
$.ajax({
type: 'GET',
url: 'product.php',
data: value,
dataType: 'html',
success: function( msg ){
$('#products').html(msg);
});
});
});
</script>
</head>
<body>
<p>
<select id="pro">
<?php
$STH = $DBH->query("SELECT productID, productName, productImage, productPrice from tbl_products");
$STH->setFetchMode(PDO::FETCH_ASSOC);
while($PRow = $STH->fetch())
{
$Pid = $PRow['productID'];
$Pname = $PRow['productName'];
$Pimage = $PRow['productImage'];
$Pimage = $PRow['productPrice'];
echo "<option value='$Pid'>".$Pname. "</option>";
}
?>
</select>
</p>
<div id="products"></div>
</body>
</html>
这是我的产品页面,其中查询字符串通过 ajax 传递并在 buy.php 页面中加载内容:
<?php
session_start();
$Name = $_POST['name'];
$Pass = $_POST['password'];
//STEP 1 Connect To Database
$host= "localhost";
$dbname= "register";
$user = "root";
$pass = "";
try {
# MySQL with PDO_MYSQL
$DBH = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);
//$DBH->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
$STH = $DBH->query("SELECT username, password from tbl_users");
$STH->execute();
//STEP 2 Declare Variables
$Query = $DBH->query("SELECT * FROM tbl_users WHERE username='$Name' AND password='$Pass'");
$Query->execute();
$Query->setFetchMode(PDO::FETCH_NUM);
$NumRows = $Query->fetch();
$_SESSION['name'] = $Name;
$_SESSION['password'] = $Pass;
//STEP 3 Check to See If User Entered All Of The Information
if(empty($_SESSION['name']) || empty($_SESSION['password']))
{
die("could not connect");
}
if($Name && $Pass == "")
{
die("Please enter a name and password!");
}
if($Name == "")
{
die("Please enter your name!" . "</br>");
}
if($Pass == "")
{
die("Please enter a password!");
echo "</br>";
}
//STEP 4 Check Username And Password With The MySQL Database
if($NumRows != 0)
{
$STH->setFetchMode(PDO::FETCH_ASSOC);
while($Row = $STH->fetch())
{
$dname = $Row['username'];
$dpass = $Row['password'];
}
}
else
{
if( $_SESSION['name']!= $dname || $_SESSION['password'] != $dpass)
{
header("location: login.php");
}
}
if($Name == $dname && $Pass == $dpass)
{
// If The User Makes It Here Then That Means He Logged In Successfully
echo "Hello " . $Name . "!";
}
}
catch(PDOException $e) {
echo "I'm sorry, database connection is wrong.";
$e->getMessage();
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" type="text/javascript"></script>
<script>
$(document).ready(function(){
$('#pro').bind('change', function (e){
var value = $(this).val();
e.preventDefault(e)
$.ajax({
type: 'GET',
url: 'product.php',
data: value,
dataType: 'html',
success: function( msg ){
$('#products').html(msg);
});
});
});
</script>
</head>
<body>
<p>
<select id="pro">
<?php
$STH = $DBH->query("SELECT productID, productName, productImage, productPrice from tbl_products");
$STH->setFetchMode(PDO::FETCH_ASSOC);
while($PRow = $STH->fetch())
{
$Pid = $PRow['productID'];
$Pname = $PRow['productName'];
$Pimage = $PRow['productImage'];
$Pimage = $PRow['productPrice'];
echo "<option value='$Pid'>".$Pname. "</option>";
}
?>
</select>
</p>
<div id="products"></div>
</body>
</html>