Advice on getting POST to pull from checkboxes

I have a basic form that I need to run PHP from, I am not sure how to get the PHP to acknowledge the checkboxes aren’t checked, it returns a “invalid index”. Thanks in advance.

<!DOCTYPE html>
<!-- batteries.php -->

<html lang = "en">
	<head>
		<title>Execute the batteries.html </title>
		<meta charset = "utf-8" />
		<style type = 'text/css"
			td, th, table {border: thin solid black;}
		</style>
	</head>
	
	<body>
		<?php
		
	//get form values
		$aa4 = $_POST["aa4"];
		$aa8 = $_POST["aa8"];
		$d4 = $_POST["d4"];
		$d8 = $_POST["d8"];
		$fname = $_POST["fname"];
		$lname = $_POST["lname"];
		$payment = $_POST[payment];
	
		
	//compute costs
		$aa4_cost = 4.39 * $aa4;
		$aa8_cost = 7.29 * $aa8;
		$d4_cost = 5.95 * $d4;
		$d8_cost = 9.49 * $d8;
		$tax_cost = .063;
		$subtotal = $aa4_cost + $aa8_cost + $d4_cost + $d8_cost;
		$include_tax = $subtotal * .063;
		$total_cost = $subtotal + $include_tax;
		$total_items = $aa4 + $aa8 + $d4 + $d8;
		
	//Return results to browser
		?>
		<h4> Customer: </h4>
		<?php
			print ("$fname <br /> $lname <br />");
		?>
		<p /> <p />
		<table>
			<caption> Order Information </caption>
				<tr>
					<th> Product </th>
					<th> Price </th>
				</tr>
				<tr>
					<td> Four AA Batteries </td>
					<td> $4.39 </td>
					<td> <?php print ("aa4"); ?> </td>
					<td> <?php printf ("$ %4.2f", $aa4_cost); ?> </td>
				</tr>
				<tr>
					<td> Eight AA Batteries </td>

well can’t say for sure without the html for your form, but my guess is this line:
[php]$payment = $_POST[payment];[/php]
which would return invalid index because you don’t have the form field/array key wrapped in single quotes
[php]$payment = $_POST[‘payment’];[/php]

Okay, I will update that, I have also included my HTML in case that needs work as well. I don’t think I have the submit button right in order to call the PHP… thanks again.

<!DOCTYPE html>
<!--Chapter 9 part 2-->
<html lang = "en">
	<head>
		<title> Batteries </title>
		<meta charset = "utf - 8" />
		<style type = "text/css">
			td, th, table {border: thin solid black;}
		</style>
	</head>
	
	<body>
		<form action = "batteries.php" method = "post">
			<h2> Welcome to Battery Depot </h2>
			
				<table>
<!-- Text widgets for the customer's name  -->
					<tr>
						<td> First Name: </td>
						<td> <input type = "text" name = "fname" size = "30" /></td>
					</tr>
					<tr>					
						<td> Last name: </td>
						<td> <input type = "text" name = "lname" size = "30" /></td>
					</tr>
					
				</table>
				<p />
				<table>
				
<!-- Column Headings -->
					<tr> 
						<th> </th>
						<th> Product </th>
						<th> Price </th>
					</tr>
<!-- Table Data -->
					<tr>
						<td> <input type = "checkbox" name = "aa4" value = "4aa" /></td>
						<td> Four AA Batteries </td>
						<td> $4.39 </td>
					</tr>
					<tr>
						<td> <input type = "checkbox" name = "aa8" value = "8aa" /></td>
						<td> Eight AA Batteries </td>
						<td> $7.29 </td>
					</tr>
					<tr>
						<td> <input type = "checkbox" name = "d4" value = "4d" /></td>
						<td> Four D Batteries </td>
						<td> $5.95 </td>
					</tr>
					<tr>
						<td> <input type = "checkbox" name = "d8" value = "8d" /></td>
						<td> Eight D Batteries </td>
						<td> $9.49 </td>
					</tr>
					
				</table>
				<p />
				<table>
				
<!-- Radio Buttons for Payment -->

				<h3> Payment Method </h3>
				<p>
					<input type = "radio" name = "payment" value = "visa"  />Visa<br />
					<input type = "radio" name = "payment" value = "mastercard" />Mastercard<br />
					<input type = "radio" name = "payment" value = "discover" />Discover<br />
					
<!-- Submit & Reset Buttons -->

					<input type = "submit" value = "Submit Order" /> <!--onclick function to call PHP file-->
					<input type = "reset" = "Clear Order" /> <!--function to clear the form?-->
				</p>
		</form>
	</body>
</html>

You need to give the submit button a name. Unless you’re using javascript or jquery, you’re not doing anything with the button.

You pull info from a checkbox like you do any other field - $_POST[‘aa4’], $_POST[‘aa8’], etc.

You’re costs are going to give you errors since you’re trying to do math with strings. like with aa4, you’re multiplying 4.39 * 4aa. The value just needs to be a number, not a string. But even then, its still going to be wrong. You’re essentially double charging. The 4aa is already 4.39 and then you’re charging another 4.39 on top of that.

$total_items = $aa4 + $aa8 + $d4 + $d8; will not give you how many items were ordered, its going to give you the subtotal again.

I think you need to include a quantity field in there for each checkbox, and then multiply that value times whatever the charge is.

Sponsor our Newsletter | Privacy Policy | Terms of Service