无法从第一个文本字段中获取其他文本字段的值

时间:2012-12-28 05:35:53

标签: php

我使用phpwebcommerce的购物车教程中的代码,我正在尝试为用户添加文本字段以插入数量。由于单个产品的选择数量不同,因此我使用while语句查询子项,并在每个子项旁边放置一个文本字段和一个添加按钮。但它只需要第一项的数量,而不是其余的数量。如果我为第一个项目插入一个数量,然后点击任何其他项目的“添加”,它仍会将第一个存储在我的购物车中,它不应该......我被卡住了...有人可以帮助我?

<?php
function displayprodDetail($pdId)
{
    $query = mysql_query("SELECT * FROM product_image WHERE p_id ='$pdId'");
    WHILE($datarows = mysql_fetch_array($query))
    {
        $p_num = $datarows['p_number'];
        echo '<h1><span>'.$p_num.' -- $'.$datarows['price'].'</span></h1>';
        echo '<div id="prodPic"><a href ="'.$datarows['image']. '" rel="ibox"    title="'.$p_num.'"><img src ="'.$datarows['image'].'"/></a><br /></div>';
        echo '<div id="items">';
        $sql = mysql_query("SELECT * FROM items WHERE item_number LIKE '$p_num/%' ORDER BY item_number ASC");
        $numProduct = dbNumRows($sql);
        if($numProduct > 1)
        {
            echo '<table border="0" cellspacing="0">';
            WHILE($data = mysql_fetch_array($sql))
            {
                $itemId = $data['item_id'];
                $p_num = $data['item_number']; 
                echo '<tr>';
                echo '<td><INPUT TYPE="HIDDEN" NAME="'.$itemId.'" VALUE="'.$itemId.'">'.$p_num.'</td>';
                echo '<td>&nbsp;&nbsp;Qty: <input type="number" name="qty" id="qty" style="width:30px"></td>';
                $cart_url = "cart.php?action=add&i=$itemId&qty=";
                ?>
                <td><input type="Submit" name="Submit" value="Add" onClick="window.location.href='<?php echo $cart_url; ?>' +document.getElementById('qty').value;" ></td>
                <?php
                echo '</tr>';
            }
            echo '</table>';
        }
        else
        {
            echo 'Items are currently not available.<br>Contact us for more detail.';
        }

        echo '</div>';
    }
}
?>

这是添加到购物车功能:

function addToCart()
{
    // make sure the product id exist
    if (isset($_GET['i']) && (int)$_GET['i'] > 0) 
    {
        $itemId = (int)$_GET['i'];
    } 
    else 
    {
        header('Location: home.php');
    }
    if (isset($_GET['qty']) && (int)$_GET['qty'] > 0) 
    {
        $qty = (int)$_GET['qty'];
    } 
    // current session id
    $sid = session_id();

    // check if the product is already
    // in cart table for this session
    $sql = mysql_query("SELECT item_id FROM cart_table WHERE item_id = $itemId AND ct_session_id = '$sid'");

    if (dbNumRows($sql) == 0) 
    {
        // put the product in cart table
        $sql = mysql_query("INSERT INTO cart_table (item_id, ct_qty, ct_session_id, ct_date) VALUES ($itemId, $qty, '$sid', now())");
    } 
    else 
    {
        // update product quantity in cart table
        $sql = mysql_query("UPDATE cart_table SET ct_qty = ct_qty + $qty WHERE ct_session_id = '$sid' AND item_id = $itemId");                  
    }   

    // an extra job for us here is to remove abandoned carts.
    // right now the best option is to call this function here
    deleteAbandonedCart();

    header('Location: ' . $_SESSION['shop_return_url']);                
}

1 个答案:

答案 0 :(得分:0)

因为您将输入框命名为相同,这就是为什么它没有按预期工作,尝试类似:

<input type="number" name="qty_<?php echo $itemId; ?>" id="qty_<?php echo $itemId; ?>" style="width:30px">...
...
<td><input type="Submit" name="Submit" value="Add" onClick="window.location.href='<?php echo $cart_url; ?>' +document.getElementById('qty_<?php echo $itemId; ?>').value;" ></td>
相关问题