0

首先我想为这个标题道歉,找不到更好的标题。

我是 ajax 新手,我想将我的 jquery 更改为 ajax。我想在同一个 jquery 中执行此操作,但我正在使用的 jquery 已经有一个url,我认为您不能同时使用两个url

我当前的 jquery 这工作,如果你理解这一点,你应该能够理解我想要什么

$(document).ready(function(){
    $('#selected').hide();
    $('#button').click(function(){
        var pid = $('#pid').val();
        var length = $('#length').val();
        var Category = $('#Category').val();
        var Qty = $('#Qty').val();
        var qty = $('#Qty').val();
        var price = '\u00A3' + parseInt($('#pricetag').text().replace(/^\D/, ''), 10) * qty;
        var category = $('#Category').text();
        var length = $('#length').val();
        if (!/^[1-9]\d?$/.test(Qty)){
            alert('Quantity should not be below 1 or null');
            return false; // don't continue
        }
        else {
        $('#sprice').text(price);
        $('#scategory').text(category);
        $('#slength').text(length);
        $('#selected').slideDown();
        }
        $.ajax({
            url: 'cart.php',
            type: 'POST',
            data: { pid:pid, 
            length:length, 
            Qty:Qty, 
            Category:Category },
            success: function(data)
            {

            }
        });
    });
});

如果你注意到我需要隐藏我的divcall selected。这不是正确的做法。

我选择的 div

         <div class="slidingDiv" id='selected'>
    <table class="tableclass">
        <tr>
            <td>Price:</td>
            <td id='sprice'></td>
        </tr>
        <tr>
            <td>Category:</td>
            <td id='scategory'></td>
        </tr>
        <tr>
            <td>Length:</td>
            <td id='slength'></td>
        </tr>
    </table>
</div>

我尝试在 w3school 上找到的这个 ajax 代码

function showitem(str)
{
var xmlhttp;
if (str.length==0)
  { 
  document.getElementById("showtxt").innerHTML="";
  return;
  }
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("showtxt").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","showitem.php?pid="+str,true);
xmlhttp.send();
}

但它不起作用,因为我对它没有经验。

想要我想做的

我不想隐藏它,selected因为如果你禁用它会显示的 javascript。

我打算做什么。如果当您onclick使用 ajax 单击添加按钮时,它会将长度、pid、类别和数量发送showitem.php到此 showitem.php 中,它将计算乘以价格 * 数量,然后回显到

<table class="tableclass">
        <tr>
            <td>Price:</td>
            <td id='sprice'></td>
        </tr>
        <tr>
            <td>Category:</td>
            <td id='scategory'></td>
        </tr>
        <tr>
            <td>Length:</td>
            <td id='slength'></td>
        </tr>
    </table>

显示项目.php

<?php
include('global.php');
    dbconnect();
$id=$_POST["pid"];
$Category=$_POST["Category"];
$length=$_POST["length"];
$qty=$_POST["Qty"];
$stmt2 = $conn->prepare("
SELECT Product.Name as ProductName, Category.Name, Category.CatID, Length, Price
FROM Itemised_Product, Product, Category
WHERE Itemised_Product.ppid =:item_id
AND Hair_Length = :length  AND Category.Name = :Category Limit  1");
$stmt2->bindParam('id',$id);
$stmt2->bindParam('length',$length);  
$stmt2->bindParam('Category',$length);  
$stmt2->bindParam('length',$length);  
$stmt2->execute();
$price = $row["Price"];
foreach ($_SESSION["item_price"]as $each_item) {
$pricetotal = $price * $each_item['Qty'];
$i = 0;
$rows2 = $stmt2->fetchAll(PDO::FETCH_ASSOC); 
        foreach ($rows2 as $row2) {
            if ($i == 0) {  
        echo '<table class="tableclass">
        <tr>
            <td>Price:</td>
            <td id='sprice'>' . $totalprice . '</td>
        </tr>
        <tr>
            <td>Category:</td>
            <td id='scategory'>' . $Category . '</td>
        </tr>
        <tr>
            <td>Length:</td>
            <td id='slength'>'.$length.'</td>
        </tr>
    </table>';
            }
        }
        }
?>

请帮我。如果您不理解问题,请发表评论,我会尝试更详细地解释它

4

1 回答 1

0

您应该使用 Jquery 为您处理所有已完成的 Ajax 内容,这是示例 Jquery Ajax 代码

var price = $('#sprice').html();
var length = $('#scategory').html();
var category = $('#slength').html();
$.ajax({
            //this is the php file that processes the data
            url: "showitem.php",

            //GET method is used
            type: "GET",

            //pass the data
            data: '&price='+price+'&category'+category+'&length='+length,
            //Do not cache the page
            cache: false,

             beforeSend: function(){
            //do something before data is sent  

            },
             complete: function(html){
             //do soemthing after data has been sent
             },


            //success
            success: function (html) {
            //do something with the result
            },

            error : function ()
            {
            //something went wrong
            }


        });
于 2013-07-28T12:40:59.743 回答