2 Replies - 362 Views - Last Post: 23 July 2012 - 11:08 AM Rate Topic: -----

#1 brandido  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 23-July 12

PHP checkbox problem

Posted 23 July 2012 - 08:44 AM

hello,

need help here. im learning php and mysql. i am creating a system, with php and mysql. this is like a ordering system.

i have one table for ingredients from my database, two type of ingredients, meat and vegetables.

ingredient table looks like these:

| ingredientid | in_name | in_type | in_price |


below is my code to display list of ingredients with checkboxes, because i wanted the user to pick any ingredients he wanted to choose.

include ('dbconnect.php');

<form action="index.php" method="post">
	<table>
	  <tbody>
	<tr>
	   <th>Meat:</th>
	   <th>Vegetables:</th>
	</tr>
	<tr>
	 <td>
	<?php
	$meat =mysql_query("Select * FROM ingredients WHERE intype LIKE 'meat' ");
	while ($row_meat = mysql_fetch_array($meat)) {
		echo '<input type="checkbox" name="meat[]" value="'.$row_meat[3].'">'.$row_meat[1].'<br/>'; 
		
	}

	?>
	</td>
	<td>
	<?php
	$vegetable = mysql_query("Select * FROM ingredients WHERE intype LIKE 'vegetable'");
	
	while ($row_veg = mysql_fetch_array($vegetable)) {
		echo '<input type="checkbox" name="veg[]" value="'.$row_veg[3].'">'.$row_veg[1].'<br/>';
	}

	?>
	</td>
	</tbody>
	</table>
	<p>
	   <input type="submit" name="submit_order" value="Add to Order">
	</p>

<form>



from the above code i am able to separate the two type of ingredients into two columns, namely meat and vegetables. letting the user to choose any ingredients he likes for his order and then submit it. when he submits his order, the name of the ingredients he choose and the price of the ingredients and the total of all the ingredients he choose will appear on the right side of the page.

so this is my second code

<div id="right">
	
	<?php if (isset($_POST['submit_order']))  {
			
			if (!empty($_POST['meat'])) {
				echo '<h3>Meat:</h3><span class="right">';
				
			foreach ($_POST['meat'] AS $key_meat => $element_meat)  {
				$meats = mysql_query("Select name FROM ingredients WHERE price ='".$element_meat."' ");
					$row_meats = mysql_fetch_array($meats);
						echo $row_meats[name].' - P'.$element_meat.'.00<br/>';
						$total_meat += $element_meat;
			}
			echo '</span>';
		}
			if (!empty($_POST['veg'])) {
			echo '<h3>Vegetables:</h3><span class="right">';
			
			foreach ($_POST['veg'] AS $key_veg => $element_veg)  {
				$vegs = mysql_query("Select name FROM ingredients WHERE price ='".$element_veg."' ");
					$row_vegs = mysql_fetch_array($vegs);
						echo $row_vegs[name].' - P'.$element_veg.'.00<br/>';
						$total_vegs += $element_veg;
			}
		
		}
			echo '</span>';
	}
	?>
 </div>
	<div id="total">
	<?php 
	$total1 = $total_meat+$total_vegs;
	if (isset($_POST['submit_order']))
			if (($total_meat+$total_vegs) != 0)	{ 
				echo 'P'.($total_meat+$total_vegs); 
			} ?>
	</div>
 </div>




what i want is when he submits his order, the choosen ingredients remains checked! and when he adds another ingredients and hit 'add to order' button again, the new ingredients he checked will be added to the right side along with the previous ingredients displayed on the right side. any idea please?

Is This A Good Question/Topic? 0
  • +

Replies To: PHP checkbox problem

#2 Duckington  Icon User is offline

  • D.I.C Addict

Reputation: 162
  • View blog
  • Posts: 588
  • Joined: 12-October 09

Re: PHP checkbox problem

Posted 23 July 2012 - 10:39 AM

You'll want to check if (during your loop through the ingredients) that particular ingredient exists within the $_POST array, set by your submit button

E.g.

while ($row_meat = mysql_fetch_array($meat)) {
     $checked = "";
     if(isset($_POST['meat'][$row_meat[3]])) $checked = "checked";
     echo '<input type="checkbox" name="meat[]" value="'.$row_meat[3].'" '.$checked.'>'.$row_meat[1].'<br/>'; 
		
}



So you set a variable $checked to an empty string on each loop round. Then if that key exists within the $_POST['meat'] array, it'#ll set the value to "checked" which will make the checkbox..checked.

You could also use array_key_exists() rather than isset() if you wanted to.
Was This Post Helpful? 0
  • +
  • -

#3 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2486
  • View blog
  • Posts: 8,528
  • Joined: 08-August 08

Re: PHP checkbox problem

Posted 23 July 2012 - 11:08 AM

If you're learning PHP and MySQL, then learn these two facts and their implications:

  • There is no such thing as a PHP checkbox. Checkboxes are done in HTML, which is processed on a completely different computer after PHP is done being processed on the server.
  • MySQL_* functions are insecure, outdated, and by the time you're comfortable with them they'll probably be gone. Learn to use prepared statements like those in PDO.


Then learn an indent style and stick with it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1