-2

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>
4

1 に答える 1

0

AJAX リストのプロパティ リストに括弧がありません。基本的に、success関数が適切に閉じられていないため、JavaScript が壊れます。

}以下のスニペットの余分な部分に注目してください。

  $.ajax({
    type: 'GET',
    url: 'product.php',
    data: value,
    dataType: 'html',
    success: function( msg ) {
      $('#products').html(msg);
    }
  });
于 2013-02-21T12:58:53.020 に答える