为什么运行此代码后出现Ajax错误?

时间:2013-02-20 15:13:37

标签: php zend-framework jquery cakephp-1.3

我收到了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>

1 个答案:

答案 0 :(得分:0)

AJAX列表的属性列表缺少括号。基本上成功功能未正确关闭导致JavaScript中断。

请注意下面代码段中的额外}

  $.ajax({
    type: 'GET',
    url: 'product.php',
    data: value,
    dataType: 'html',
    success: function( msg ) {
      $('#products').html(msg);
    }
  });