0

こんにちは、GETリクエストの後にjquerys ajaxを使用してフォームを更新しようとしています。単にテーブルを更新したいのですが、ページヘッダーも送信します。

最初にこれが私のスクリプトです。小さいので、すべて含めます。

var wishorder = {
    init: function(config){
        this.config = config;
        this.bindEvents();
    },
    bindEvents: function(){
        this.config.itemSelection.on('click',this.addWish);
    },
    addWish: function(e){
        console.log('working');

        var self = wishorder;
        $.ajax({
            url: $(this).attr('href'),
            type: 'GET',
            data: {
                sku: $(this).data('sku'),
                action: $(this).data('action'),
                qty: $(this).data('qty')
            },
            success: function(results){
                //here is where is would like to repopulate the div
                //but its outputting the whole page
                console.log(results);
            }
        });
        e.preventDefault();
    }


};
wishorder.init({
    itemSelection: $('#carttable a'),
    form: $('#cartfrm')
});

必要な結果が返されていることがわかりますが、ページヘッダーのようにhtmlでラップされています。

呼び出し元のphp関数は次のとおりです。

if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_GET['action'] == 'move'){

  list($sp_type, $pid) = $this->parse_sku($_GET['sku']);
  $qty =  (filter_var($_GET['qty'], FILTER_VALIDATE_INT, array('min_range' >= 1)))?$_GET['qty']:1;
  $q = "CALL add_to_wish_list('$this->uid', '$sp_type', $pid, $qty)";
  $this->query($q);
  $q = "CALL remove_from_cart('$this->uid', '$sp_type', $pid)";
  $this->query($q);
  $q = "CALL get_shopping_cart_contents('$this->uid')";
  $this->query($q);
  $this->read();
  $this->outcart();
  exit();
}

そして、divのhtmlを生成するoutcart関数

function outcart(){
  echo "<div id=\"cartcont\" class=\"cartitems\"><form action=\"/cart.php\" method=\"post\"><table id=\"carttable\"><tr class=\"theads\"> <th class=\"titm\" >Item</th> <th >Quantity</th> <th >Price</th> <th>Subtotal</th> <th class=\"topt\" >Options</th>";
  $total = 0;
  while($row = $this->_result->fetch_object()){
    $price = $this->get_just_price($row->price, $row->sale_price);
    $subtotal = $price * $row->quantity;
    $total += $subtotal;
    echo "<tr class=\"tdata\">
            <td>".$row->category." :: ".$row->name."</td>
            <td><input type=\"text\" name=\"quantity[".$row->sku."]\" value=\"$row->quantity\" size=\"2\">
            <td>$".$price."</td>
            <td>$".number_format($subtotal, 2)."</td> 
            <td><a href=\"/wishlist.php?sku=".$row->sku."&amp;action=move&amp;qty=".$row->quantity."\" class=\"buttoncart black\">&nbsp;&nbsp; Move To Wishlist &nbsp;&nbsp;</a><br><a href=\"/cart.php?sku=".$row->sku."&amp;action=remove\" class=\"buttoncart black\">&nbsp;Remove From Cart &nbsp;</a></td>";
  }

  echo "</tr><tr class=\"ttotals\"><td colspan=\"3\" ><strong id=\"carttotal\">TOTAL:</strong></td>
                <td colspan=\"2\" >&nbsp;&nbsp; $".number_format($total,2) ." </td>
                </tr>";
  echo "</table>";

  echo "<div id=\"cartmnbtns\"><p><input type=\"submit\" value=\"UPDATE QUANTITIES\" class=\"buttoncart1 green\"><br><a href=\"https://".BASE_URL."/checkout.php?session=".$this->uid."\" class=\"buttoncart1 green \">Checkout</a></p> </div></form>";
}

だから私は本当にテーブルまたはcartcont divに結果を再入力したいのですが、どこがポイントを逃していますか?

4

1 に答える 1

1

あなたが呼び出しているurl: $(this).attr('href')ページは、すべてのヘッダーなどを含む Web ページです... Web ページ フレーム全体を含まない別の .php ファイルに関数を配置する必要があります。必要なテキストのみを出力します。

それが不可能な場合は、成功のコールバック関数を で作成し、document.createDocumentFragment()レスポンス全体をフラグメント内に渡し、必要なものを次のように引き出してmyHTML = docFrag.getElementsByTagName('div')[0]から、フォームに追加してこれを渡します。

contentType: "text/plain; charset=utf-8"ajax 呼び出しのようなものを設定することもできます。

ところで、文字列内の変数を解析する便利さを使用していない場合は、文字列を記述する別の方法 (他の引用符) を使用することをお勧めします。

echo '<tr class="tdata">...'
于 2013-09-14T10:34:01.040 に答える