在下拉菜单中添加选定的字段

时间:2018-08-16 12:06:52

标签: javascript php jquery arrays

对编码来说是新手,并创建了以下内容:

<option value="Select your magazine"> Select your magazine</option>
<option value="100 mag| 78.99"> 100 mag | 78.99</option>
<option value="250 mag | 136.99"> 250 mag| 136.99</option>
<option value="500 mag | 284.99"> 500 mag  | 284.99</option>

我希望此人从此菜单中选择一个选项,并从其他与此菜单类似的菜单中选择两个选项。

我希望能够获得页面底部的总数。

我该怎么做?

第二,一旦他们从下拉菜单中选择了多个选项,如何使贝宝按钮正常工作?

最后,我该如何在他们的订单中添加带有秘密字样的优惠券(折扣)?

我搜索过高低,我有点困惑。需要帮助:-)。

3 个答案:

答案 0 :(得分:0)

不确定菜单到底要什么,但是您需要将选项以选择形式和表格形式包含起来。

<form method='post' action='yourphpfile.php'>
<select name='variablename' id='variablename'>
     <option value="Select your magazine"> Select your magazine</option>
     <option value="100 mag| 78.99"> 100 mag | 78.99</option>
     <option value="250 mag | 136.99"> 250 mag| 136.99</option>
     <option value="500 mag | 284.99"> 500 mag  | 284.99</option>
</select>
<button type='submit' name='submit'>Submit</button>
</form>

名称字段将允许您使用php获取内容。 根据折扣代码,您可以通过以下方式创建表格

<form method='post' action='yourphpfile.php'>
    <input type='text' name='discountcode'>
    <button type='submit' name='submit'>Use Code</button>
</form>

然后在php部分的顶部使用'discountcode'名称来捕获输入内容,并设置所需的代码。

$codeaccepted = 'ABC123';
$codesubmitted=isset($_POST['discountcode'])? $_POST['discountcode'] : '';

然后,如果代码相似,则可以执行简单的if语句,并在if语句中设置折扣。

答案 1 :(得分:0)

我为您准备了一些东西。它并不完美,但可以接受并尝试一下。

<?php
//this should give an array all the prices you marked
$magazines = isset($_POST['magazine']) ? $_POST['magazine'] : '';

$pricing = [78.99, 136.99, 284.99];
$totalprice = 0;
$discount = 0;
foreach($magazine as $i){
//loop over all the marked magazines and add the prices
    $price += $i;
}
$codeaccepted = 'ABC123';
$codesubmitted=isset($_POST['discountcode'])? $_POST['discountcode'] : '';

if($codesubmitted == $codeaccepted){
    $discount = .20;//only set the discounts if the codes match
}
$discount = 1 - $discount;
$price = $price * $discount;//apply the discount
?>

<form method='post' action='yourphpfile.php'>
    <p>Select Your magazine</p>
    <select name='variablename' id='variablename' onchange='updatePrice()'>
        <?php foreach($pricing as $i){?>
            <option value= <?php echo $i?> ><?php echo $i?></option><?php
        }?>
    </select>
    <button type='submit' name='submit' id='button' 
           style='display:none;'>Submit</button>
</form>

<form method='post' action='yourphpfile.php'>
    <input type='text' name='discountcode'>
    <button type='submit' name='submit'>Use Code</button>
</form>

<script>
    function updatePrice(){
        button = document.getElementById('button');
        button.click();
    }
</script>

答案 2 :(得分:0)

这应该完全按照您希望的某个选定选项的方式工作。查看它,尝试找出如何添加更多选项。祝你好运!

<?php
$magazines = isset($_POST['magazine']) ? $_POST['magazine'] : 0;//returns the price 
//of the selected magazine

//add more select options here

$totalprice = 0;//initiate price variable
$discount = 0;//initiate discount variable

$price += $magazines;//Increment the price
$pricing['prices'] = ['78.99', '136.99', '284.99']; // set the prices
$pricing['selected'] = 'None';//use this to determine which one is selected

foreach($pricing['prices'] as $i){
    if($magazines === $i){//compare
        //set the selected price
        $pricing['selected'] = $i;
    }
}

$codeaccepted = 'ABC123';//accepted discount code
$codesubmitted=isset($_POST['discountcode'])? $_POST['discountcode'] : '';//submitted 
//code

if($codesubmitted == $codeaccepted){
    $discount = .20;//only set the discounts if the codes match
}
$discount = 1 - $discount;//math
$price = $price * $discount;//apply the discount
?>

<form method='post' action='stuff.php'>
<p>Select Your magazine</p>
<select name='magazine' id='magazine' onchange='updatePrice()'>
        <option value=0>Select</option>
    <?php foreach($pricing['prices'] as $i){
        if($i === $pricing['selected']){?>
            <!--display the selected option price-->
            <option selected value=<?php echo $i?> ><?php echo $i?></option><?php
        }else{?>
            <!--display all other prices-->
            <option value=<?php echo $i?> ><?php echo $i?></option><?php
        }
    }?>
</select>
<button type='submit' name='submit' id='button' style='display:none;'>Submit</button>
<input type='text' name='discountcode'>
<button type='submit' name='submit'>Use Code</button>
</form>

<p><?php echo $price?></p>

<script>
function updatePrice(){
    button = document.getElementById('button');
    button.click();//submit the form
}
</script>