0

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.

2

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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

1 Comment

Are you sure the check box will come in as an array if it's just name='checkbox' . I think it only works if you set the name like this name='checkbox[]'
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.