Rick Ng
Rick Ng

Reputation: 69

How to check if the button tag is clicked in PHP

I am try to check if the button tag is clicked then do the statement but it seems doesn't work. Am I missing anything or doing something wrong? Thanks so much.

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>

Upvotes: 4

Views: 32755

Answers (4)

Favour okechukwu
Favour okechukwu

Reputation: 77

This should work fine

 if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // Something posted

    if (isset($_POST['appetizerbutton'])) {
       // btnDelete
    } else {
       // Assume btnSubmit
    }
}

Upvotes: -1

Fakhruddin Ujjainwala
Fakhruddin Ujjainwala

Reputation: 2553

It has to be like this:

<form action="index.php" method="post">
<input type="submit" name="appetizer_button" value="Appetizers & Soup">
</form>

This will generate the post event that will in-turn execute your code.

To echo:

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

Upvotes: 7

Tajmin
Tajmin

Reputation: 403

Where is your form ? It should be something like:

<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>'

Upvotes: 0

devpro
devpro

Reputation: 16117

As per you comment, you just need to add <form> form your buttons as:

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

Upvotes: 0

Related Questions