简体   繁体   中英

Retrieving Post value of <select> tag for updating shopping cart quantity?

I'm trying to create a simple shopping cart. That allows users to select a quantity from a drop-down menu using <select> tag. By default it shows "1" unless a quantity is selected , see below:

在此输入图像描述

Problem: I am getting a "<" symbol once I've click update and updated the cart.

在此输入图像描述

I cannot figure out why I am getting the "<" symbol, my <select> tag and value attributes look correct?!

Here is my code:

// If the update button is clicked and the if an option has been selected for the <select> tag.

    if(isset($_POST['updated_value'])) {

        if(isset($_POST['selectDropDown'])) {
            $updatedValue =  $_POST['selectDropDown']; 
        }
    } else {
        $updatedValue = '1';
    }

?>

    <table>
        <tr>
            <th>Description </th>
            <th>Quantity</th> 
        </tr>
    </table>

<?php while($cart = mysqli_fetch_assoc($result)) { ?> 


    <form action="cart.php" method="POST">

     <table>
        <tr>
            <td>
                 <?php echo $cart["prod_desc"]; ?>
            </td>
            <td> 
                <select name= "selectDropDown" >

            <option value=< <?php echo $updatedValue; ?> > <?php echo $updatedValue; ?> </option> 
                    <option value=<2> 2 </option> 
                    <option value=<3> 3 </option>
                    <option value=<4> 4 </option>
                    <option value=<5> 5 </option>
                    <option value=<6> 6 </option>
                    <option value=<7> 7 </option>
                    <option value=<8> 8 </option>
                    <option value=<9> 9 </option>
                    <option value=<10> 10 </option>
                </select>
            </td>
            <td>
                <input type="submit" name="updated_value" value="update"/>
            </td>   

        </tr>
    </table>

    </form>


<?php 
} 

?>

Any ideas? Please help!

 <option value=< <?php echo $updatedValue; ?> > <?php echo $updatedValue; ?> </option> 
                <option value=<2> 2 </option> 
                <option value=<3> 3 </option>
                <option value=<4> 4 </option>
                <option value=<5> 5 </option>
                <option value=<6> 6 </option>
                <option value=<7> 7 </option>
                <option value=<8> 8 </option>
                <option value=<9> 9 </option>
                <option value=<10> 10 </option>

It's not correct, you should remove the "<" symbol in "value =", it's should look like this:

<option value= <?php echo $updatedValue; ?> > <?php echo $updatedValue; ?> </option> 
                <option value=2> 2 </option> 
                <option value=3> 3 </option>
                <option value=4> 4 </option>
                <option value=5> 5 </option>
                <option value=6> 6 </option>
                <option value=7> 7 </option>
                <option value=8> 8 </option>
                <option value=9> 9 </option>
                <option value=10> 10 </option>

Try this,

// If the update button is clicked and the if an option has been selected for the <select> tag.

    if(isset($_POST['updated_value'])) {

        if(isset($_POST['selectDropDown'])) {
            $updatedValue =  $_POST['selectDropDown']; 
        }
    } else {
        $updatedValue = '1';
    }

?>

    <table>
        <tr>
            <th>Description </th>
            <th>Quantity</th> 
        </tr>
    </table>

<?php while($cart = mysqli_fetch_assoc($result)) { ?> 


    <form action="cart.php" method="POST">

     <table>
        <tr>
            <td>
                 <?php echo $cart["prod_desc"]; ?>
            </td>
            <td> 
                <select name= "selectDropDown" >

            <option value="<?php echo $updatedValue; ?>" > <?php echo $updatedValue; ?> </option> 
                    <option value=2> 2 </option> 
                    <option value=3> 3 </option>
                    <option value=4> 4 </option>
                    <option value=5> 5 </option>
                    <option value=6> 6 </option>
                    <option value=7> 7 </option>
                    <option value=8> 8 </option>
                    <option value=9> 9 </option>
                    <option value=10> 10 </option>
                </select>
            </td>
            <td>
                <input type="submit" name="updated_value" value="update"/>
            </td>   

        </tr>
    </table>

    </form>


<?php 
} 

?>

Add for example <option value="<2"> 2 </option>

value of option value should be in quotes...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM