0

我很陌生,jquery但我想知道它们是否可以在mysql下面使用我的 jquery 运行。

在我的cart.php我有一个query检查是否发布的任何内容都在数据库中,如果没有,则不会添加该项目。

在我的 view.php 页面中,它位于下面jqueryhidden div位置。works当您单击add button隐藏的 div 时,它会显示您添加的内容。

<button type="submit" name="submit" class="show_hide" id="button" name="submit">Add</button>

jQuery

$(document).ready(function(){
    $('#selected').hide();
    $('#button').click(function(){
        var pid = $('#pid').val();
        var length = $('#length option:selected').text();
        var Category = $('#Category').val();
        var cat = $('#Category option:selected').text();
        var Qty = $('#Qty').val();
        var qty = parseInt($('#Qty').val());
        var price =  parseFloat($('#pricetag').text().replace(/^\D/, ''), 10) * qty;
        price = '\u00A3' + price.toFixed(2);
        var category = $('#Category').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(cat);
        $('#slength').text(length);
        $('#selected').slideDown();
        }
        $.ajax({
            url: '../cart.php',
            type: 'POST',
            data: { pid:pid, 
            length:length, 
            Qty:Qty, 
            Category:Category },
            success: function(data)
            {
             location.reload();  
            }
        });
    });
  });

隐藏的 div

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

编辑 添加查询代码

在 cart.php 页面我有这个query

$statement = $conn->prepare("SELECT * FROM Category ct, Itemised ip, Product pd
                         WHERE ct.CatID = ? AND ip.Length = ? AND pd.pid = ?
                         ");
     $statement->execute(array($Category, $length, $pid));
     if (!$statement->fetch()) {
         exit();
     }

我用它来检查发布的内容是否存在于数据库中。这或多或少与我想在 jquery 中运行以检查是否var存在以及不url:./cart.php应该运行的查询相同。

更新以显示我的 cart.php 代码

if(isset($_POST['pid']) && isset($_POST['length']) && isset($_POST['Qty']) && isset($_POST['Category'])){   
    $pid = $_POST['pid'];
    $length = $_POST['length'];
    $qty = $_POST['Qty'];
    $Category = $_POST['Category'];;
    $wasFound = false;

     dbconnect();
     $statement = $conn->prepare("SELECT * FROM Category ct, Itemised_Product ip, Product pd
                         WHERE ct.CatID = ? AND ip.ItemID = ? AND pd.Sef = ?
                         ");
     $statement->execute(array($Category, $length, $pid));
     if (!$statement->fetch()) {
         exit();
     }

     $i = 0;
    // If the cart session variable is not set or cart array is empty
    if (!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1) { 
        // RUN IF THE CART IS EMPTY OR NOT SET
        $_SESSION["cart_array"] = array(0 => array("item_id" => $pid, "length" => $length, "Category" => $Category, "quantity" => $qty));
    } else {
        // RUN IF THE CART HAS AT LEAST ONE ITEM IN IT
        foreach ($_SESSION["cart_array"]as $array_key=>$each_item) { 
              if ($each_item['item_id'] == $pid  && $each_item['length'] == $length && $each_item['Category'] == $Category) {
                      $_SESSION["cart_array"][$array_key]['quantity']+=$qty;
                      $wasFound = true;

                  } 
              } 
           if ($wasFound == false) {
               array_push($_SESSION["cart_array"], array("item_id" => $pid, "length" => $length, "Category" => $Category, "quantity" => $qty));

           }
    }
    header('Location: '.fetchinline($bpages).$currentFile);
    exit();
}

我的问题 在购物车页面中,我有一个查询,它检查发布的内容是否未被用户检查。我想对上面的 jquery 做同样的事情,如果我想停止用户have,我想停止hidden div显示。

我希望我已经解释给你理解。感谢您的时间。

4

2 回答 2

0

我不确定我是否完全理解你的问题。是吗:

  1. 按添加按钮
  2. AJAX 请求检查产品是否存在
  3. 如果是,那么显示隐藏的 div?

或者

  1. 按添加按钮
  2. 显示更多信息的隐藏字段
  3. 将产品添加到购物车

无论如何,在上述两种情况下,您都可以向服务器发出 AJAX 请求并检查产品是否存在。如果您查看 $.ajax 成功函数,那么“数据”实际上是来自服务器的响应,您可以包含您需要的任何内容并使用 javascript/jquery 处理数据。

于 2013-08-09T13:27:41.327 回答
0

JS

    $(document).ready(function(){
    $('#selected').hide();
    $('#button').click(function(){
        var pid = $('#pid').val();
        var length = $('#length option:selected').text();
        var Category = $('#Category').val();
        var cat = $('#Category option:selected').text();
        var Qty = $('#Qty').val();
        var qty = parseInt($('#Qty').val());
        var price =  parseFloat($('#pricetag').text().replace(/^\D/, ''), 10) * qty;
        price = '\u00A3' + price.toFixed(2);
        var category = $('#Category').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(cat);
        $('#slength').text(length);
        $('#selected').slideDown();
        }
        $.ajax({
            url: '../cart.php',
            type: 'POST',
            data: { pid:pid, 
            length:length, 
            Qty:Qty, 
            Category:Category },
            dataType:json,
            success: function(data)
            {
                if(data.response=='failed')
                {
                    alert('error');
                }
                else if(data.response=='success')
                {
                    alert('success');
                    //location.reload();  
                }
                else if(data.response == 'already_in_database') 
                {
                    alert('Already exists in database');
                }   
            },
            error: function()
            {
                alert('error');
            }
        });
    });
  });

购物车.php

if (isset($_POST['pid']) && isset($_POST['length']) && isset($_POST['Qty']) && isset($_POST['Category']))
{
    $pid = $_POST['pid'];
    $length = $_POST['length'];
    $qty = $_POST['Qty'];
    $Category = $_POST['Category'];
    $wasFound = false;

    dbconnect();
    $statement = $conn->prepare("SELECT * FROM Category ct, Itemised_Product ip, Product pd
                         WHERE ct.CatID = ? AND ip.ItemID = ? AND pd.Sef = ?
                         ");
    $statement->execute(array($Category, $length, $pid));
    $result=array();
    if (!$statement->fetch())
    {
        $result['response'] = 'already_in_database';
        echo jsone_encode($result);
        die();
    }

    $i = 0;
    // If the cart session variable is not set or cart array is empty
    if (!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1)
    {
        // RUN IF THE CART IS EMPTY OR NOT SET
        $_SESSION["cart_array"] = array(0 => array("item_id" => $pid, "length" => $length, "Category" => $Category, "quantity" => $qty));
    }
    else
    {
        // RUN IF THE CART HAS AT LEAST ONE ITEM IN IT
        foreach ($_SESSION["cart_array"]as $array_key => $each_item)
        {
            if ($each_item['item_id'] == $pid && $each_item['length'] == $length && $each_item['Category'] == $Category)
            {
                $_SESSION["cart_array"][$array_key]['quantity']+=$qty;
                $wasFound = true;
            }
        }
        if ($wasFound == false)
        {
            array_push($_SESSION["cart_array"], array("item_id" => $pid, "length" => $length, "Category" => $Category, "quantity" => $qty));
        }
    }
    header('Location: ' . fetchinline($bpages) . $currentFile);
    exit();
}
于 2013-08-09T15:49:26.490 回答