Chaitanya Krish
Chaitanya Krish

Reputation: 65

How to access values of checkboxes in php mysql

I have a table with pizza name, pizza type and price. I used a loop to print all the items in a table.

<form method="Cart.php" method="post">
<table border="1" cellpadding="10">
<tr>
<th>Pizza name</th>
<th>Pizza type</th>
<th>Price</th>
</tr>
<?php
while($row = mysql_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['subcat_name'] . "</td>";
echo "<td>" . $row['cat_name'] . "</td>";
echo "<td>" . $row['price']. "</td>";
echo "<td><input type=\"checkbox\" name=\"checkbox\" value=\"\"   id=\"checkbox\"></td>";
echo "</tr>";
}

echo "</table>";
?>
<input type="submit" name="addToCart" id="addToCart"/>
</form>

Now I want to access the pizza names and prices as I click the submit button.

Upvotes: 0

Views: 78

Answers (3)

Dhamu
Dhamu

Reputation: 11

Here is another way, Hope this helps.

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

       $check_list = $_POST['checkbox'];

       foreach ($check_list as $checked=>$value) {
          //Here you got all checked values in "$checked"
          //Eg: to move checked values to array
          array_push($pizzaOrdered, $value);
        }
    }

Upvotes: 1

VIDesignz
VIDesignz

Reputation: 4783

You can access them by first checking if it exists...

if(isset($_POST['checkbox']){
    $checkbox_value = $_POST['checkbox'];
}else{
    $checkbox_value = ""; // set a default value here
}

I should add that checkboxes that are NOT "checked" will not pass along in a POST, so you need to explicitly check if it has been "checked" by calling

if(isset($_POST['checkbox'])){}

Which at that point you can decide to set a value yourself or use the value you set in the form.

Upvotes: 2

The Beast
The Beast

Reputation: 1

Try this just use the name of the check box $_POST['checkbox']

Upvotes: 0

Related Questions