简体   繁体   English

单击类型提交按钮时如何传递隐藏值? PHP

[英]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. (我知道,这样做的一种方法是将相同的名称设置为type =提交按钮,但将它们的值设置为彼此不同,但是这些值会出现在按钮本身上,这是我所不希望的。)预先。

 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: 避免SQL注入,准备使用更多的信息语句和参数化查询 这里 为了简便起见,下面的代码仍然是脆弱的:

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);

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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