简体   繁体   中英

how to pass hidden value when type submit button is clicked? PHP

in this code I retrieve each product added to the cart by the user, and under each product is a 'remove' button. The problem is I want to set a hidden value for each one, and be able to get the value when the button is clicked. (I know that one way of doing this is setting the same name to the type= submit buttons but set their values different from each other, but the values would appear on the button itself, which i don't want.) Thank you in advance.

 while ($isready= mysqli_fetch_array($getinfo))
        {
            echo "<br>$isready[1] $isready[2] BD $isready[4]";
            echo '<br><input name="removefromcart" type="submit" value="remove">';
            echo '<input type=hidden name="removed" value="'.$isready[0].'"">';
            $i++;
        }`enter code here`
        $mult= $isready[2]* $isready[4];
        $total= $total +$mult;
    echo "<br><br>$total BD";
    if (isset($_POST['removefromcart']))
    {
        $removebutton=$_POST['removefromcart'];
        $conn->query("DELETE FROM `addedtocart` WHERE ID=$removebutton");
        $conn->query("ALTER TABLE addedtocart AUTO_INCREMENT=$removebutton");

    }

Avoid SQL Injection, Use prepared statements and parameterized queries for more info HERE For expediency sake, below code is still vulnerable:

This Code I altered little bit i added the post removed value into your query you can adjust like how you want:

while ($isready= mysqli_fetch_array($getinfo))
        {
            echo "<br>$isready[1] $isready[2] BD $isready[4]";
            echo '<br><input name="removefromcart" type="submit" value="remove">';
            echo '<input type=hidden name="removed" value="'.$isready[0].'"">';
            $i++;
        }`enter code here`
        $mult= $isready[2]* $isready[4];
        $total= $total +$mult;
    echo "<br><br>$total BD";
    if (isset($_POST['removefromcart']))
    {
        $removebutton=$_POST['removefromcart'];
        $removeValue=$_POST['removed'];
        $conn->query("DELETE FROM `addedtocart` WHERE ID=".$removeValue);
        $conn->query("ALTER TABLE addedtocart AUTO_INCREMENT=".$removeValue);

    }

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