1

ユーザーがページを離れることなく、アイテムをカートに追加するために ajax 呼び出しを実行しようとしています。私は次のコードを書くことができましたが、何も機能していません。addtocart.php で、手動で入力しましたがProdID、エコーアウトしませんでしたsizeCategory誰かが私のajaxとaddtocart.phpの両方を見てください

AJAX

<script>
    $(document).ready(function(){
$('.ajax').click(function(){
$.ajax({ 
url: '../main/php/addtocart.php',
         type: 'post',
         data:{
            length:$('#length').val(),
                Category:$('#Category').val(),
                id:$('#id').val(),
                Qty:$('#Qty').val()

                  },
         success: function(data) {

                  }
});
});
});
</script>

PHP

<?php
    include('dbconnect.php');
    $id = $_POST['id'];
    $length = $_POST["size"];
    $qty = $_POST['Qty'];
    $Category = $_POST['Category'];

$stmt = $conn->prepare("
SELECT  ProductName, Category.Name, size, Price
FROM itembag, Product, Category
WHERE Product.ProdID =:id
AND size= :length  AND Category.Name = :Category Limit  1");
$stmt->bindParam('id',$id);
$stmt->bindParam('length',$length); 
$stmt->bindParam('Category',$Category); 
$stmt->execute();
$i=0;
 foreach ($stmt->fetchAll(PDO::FETCH_ASSOC) as $row) {
             if ($i == 0) {  
echo 'status:1,id:'.$row['ProdID'].',Price:'.$row['Price'].',txt:\'\
\
<table width="100%" id="table_'.$row['ProdID'].'">\
  <tr>\
    <td width="60%">'.$row['ProductName'].'</td>\
    <td width="40%">$'.$row['Price'].'</td>\
    <td width="10%">$'.$row['Category'].'</td>\
  </tr>\
</table>\'';
}
}
?>

追加情報: アイテムには ID が 1 つあるかもしれませんが、さまざまなサイズと多くのカテゴリがあります。

4

3 に答える 3

1

あなたに基づいて、HTMLあなたAJAXは次のようになります:

注: を使用している場合はAJAX、セレクターを参照する必要がありidます。name

AJAX

$(document).ready(function()
{
    $('#button').click(function()
    {
        var id = $('#id').val();
        var length = $('#length').val();
        var qty = $('#Qty').val();
        var cat = $('#Category').val();

        $.ajax({
            url: '../main/php/addtocart.php',
            type: 'POST',
            data: { id:id, length:length, qty:qty, cat:cat },
            success: function(data)
            {
                //whatever you want to do
            }
        })
    });
});

あなたがあなたの最初にこのようにするなら、それも良いかもしれませんPHP

if(isset($_POST['id']) && isset($_POST['length']) && isset($_POST['Qty']) && isset($_POST['cat']))
{    
    $id = $_POST['id'];
    $length = $_POST['length'];
    $qty = $_POST['Qty'];
    $Category = $_POST['Category'];
于 2013-06-25T01:59:49.653 に答える
1

POSTするデータがありません

data: {
//Here you have to put your data
},

たとえば、POST する値を取得する必要があります。

('.ajax').click(function(){
 pid = $('#pid').val();//#pid->is the id of your input, the same bellow
length = $('#length').val();
Qty = $('#Qty').val();
Category = $('#Category').val();
$.ajax({ 
  url: '../main/php/addtocart.php',
  data:{
    pdi:pdi,
    length:length,
    Qty :Qty ,
    Category : Category},
    type: 'post',
}).done(function(data) {
 $(data).html('#containerShoppingCart');//Callback Replace the html of your shoppingCart Containe with the response of addtocart.php
}).fail(function(){ alert("failed!"); });//Some action to indicate is Failing 

詳細については、 http://api.jquery.com/jQuery.ajax/を参照してください。

于 2013-06-25T01:49:19.787 に答える
0

jQuery load を使用すると、カートのロード/更新に役立つ場合があります。 http://api.jquery.com/load/

于 2013-06-25T01:14:12.523 に答える