簡體   English   中英

使用PHP MySQL使用數據庫中的一個重置選項標簽值

[英]Reset option tag value with one from database using PHP MySQL

最初,我用庫存數量填充我的期權標簽。

<select name="quantity" class="form-control input-sm" id="quantity">
    <?php for ($i=1; $i<=$Quantity; $i++) { ?>
    <option value="<?php echo $i;?>"><?php echo $i;?></option>
    <?php } ?>
</select>

我正在使用Javascript從html表單更新數量:

function update_id(id)
{
    if (confirm('Update quantity for this Item?')) {
        uid = id;
        quantity = $('select[name=quantity]').val()
        window.location.href="Cart.php?update_id="+uid+"&quantity="+quantity;//+'?quantity='+quantity;
    }   
}

並將其針對該特定產品存儲在購物車表格中。 重新加載或刷新頁面時,選項標簽將重置為默認值“ 1”。 我想為該特定產品分配帶有數據庫更新數量的選項標簽值。

購物車的屏幕截圖

提前致謝

我認為HTML部分來自“ Cart.php”。 如果是這樣,您可以將其更改為:

<select name="quantity" class="form-control input-sm" id="quantity">
    <?php 
        $selectedQuantity = (isset($_GET["quantity"]) ? $_GET["quantity"] : 1);
        for ($i=1; $i<=$Quantity; $i++) { 
    ?>
    <option value="<?php echo $i;?>"<?=($selectedQuantity == $i ? " selected" : "")?>><?php echo $i;?></option>
    <?php } ?>
</select>

因此,向選項標簽添加“ selected”屬性將對所選屬性進行評估。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM