如何检查是否在PHP中单击了按钮标记

时间:2016-03-01 07:18:20

标签: php

我尝试检查按钮标签是否被点击然后执行声明,但似乎不起作用。我错过了什么或做错了吗?非常感谢。

if (isset($_POST['appetizer_button'])) {
    $results = $mysqli->query("SELECT * FROM products ORDER BY id ASC");

    if ($results) { 
        //fetch results set as object and output HTML
        while($obj = $results->fetch_object()) {
            echo '<div class="product">'; 
            echo '<form method="post" action="cart_update.php">';
            // echo '<div class="product-thumb"><img src="images/'.$obj->product_img_name.'"></div>';
            echo '<div class="product-content"><h3>'.$obj->product_name.'</h3>';
            echo '<div class="product-desc">'.$obj->product_desc.'</div>';
            echo '<div class="product-info">';
            echo 'Price '.$currency.$obj->price.' | ';
            echo 'Qty <input type="text" name="product_qty" value="1" size="3" />';
            echo '<button class="add_to_cart">Add To Cart</button>';
            echo '</div></div>';
            echo '<input type="hidden" name="product_code" value="'.$obj->product_code.'" />';
            echo '<input type="hidden" name="type" value="add" />';
            echo '<input type="hidden" name="return_url" value="'.$current_url.'" />';
            echo '</form>';
            echo '</div>';
        }
    // }
    }
}

<td>
    <button name="appetizer_button" onclick="location.href='index.php'">Appetizers & Soup</a></button> |
    <button name="house_button" onclick="location.href='index.php'">House Specials</a></button>
</td>

3 个答案:

答案 0 :(得分:4)

必须是这样的:

if (isset($_POST['appetizer_button'])) {
    // Your code that you want to execute
}

这将生成post事件,然后执行您的代码。

回应:

    let sndButtonClick = SKAction.playSoundFileNamed("button_click.wav", waitForCompletion: false)

答案 1 :(得分:2)

form在哪里?它应该是这样的:

<form action="something.php" method="post">
    <button name="appetizer_button" onclick="location.href='index.php'">Appetizers & Soup</a></button> |
    <button name="house_button" onclick="location.href='index.php'">House Specials</a></button>
</form>'

答案 2 :(得分:0)

根据您的评论,您只需要将<form>添加到按钮中:

<td>
    <form method="post" action="index.php">
        <button type="submit" name="appetizer_button">Appetizers & Soup</a></button> |
        <button type="submit" name="house_button">House Specials</a></button>
    </form>
</td>