Hi everybody!
Please,help!
My problem is as following:
in a file XXX1.php we have a form with checkboxes:
{
echo '<form action ="XXX2.php" method= "post">
What you are looking for: <br />
<p><input type="checkbox" name = "wordprop[]" value ="Option1" >Option1<br/>
<p><input type="checkbox" name = "wordprop[]" value ="Option2" >Option2<br/>
<p><input type="checkbox" name = "wordprop[]" value ="Option3" >Option3<br/>
<input type= "submit" name ="s" value="Submit">
</form>';
}

What I whould like to have is:
After user presses the button <Submit>, the results should be processed in file XXX2.php. The program should:
1.check which checkboxes were chosen
2.then connect to the database and retrieve the stored values from the database
3.display the values

I can connect to the database, the problem is how to verify which checkboxes were chosen and then query the database.

Recommended Answers

All 4 Replies

You can use the isset() function in XXX2.php to check which boxes were or were not selected.

in XXX2.php

//first check the submit was pressed
if (isset($_POST['s']))
{
   //then check if a checkbox was selected.
   if (isset($_POST['name-of-checkbox1']))
   {
      echo "checkbox 1 was selected";
      // execute database action here
   }
   if (isset($_POST['name-of-checkbox2']))
   {
      echo "checkbox 2 was selected";
      // execute database action here
   }
   if (isset($_POST['name-of-checkbox3']))
   {
      echo "checkbox 3 was selected";
      // execute database action here
   }
}

You will need to give your checkboxes unique names for this to work properly.

Here is another way of doing the same thing as the code above. The only difference is that you don't need multiple if-statements to do your processing. To make it easier for you to understand what is going on, do the following.
Create a table and name it fruits. This table should have the two columns, fruit and price. Now populate you table with the following: orange, 10; apple, 5; banana, 15; pear, 20; guava, 2.

Now try this code. I have only one page instead of two.

<?php
if(!isset($_POST['send']))
{
?>
<form method = "post" action = "ppp.php">
<input type = "checkbox" name = "mycheck[]" value = "orange">ORANGE<br>
<input type = "checkbox" name = "mycheck[]" value = "apple">APPLE<br>
<input type = "checkbox" name = "mycheck[]" value = "banana">BANANA<br>
<input type = "checkbox" name = "mycheck[]" value = "pear">PEAR<br>
<input type = "checkbox" name = "mycheck[]" value = "gauva">GUAVA<br>
<input type = "submit" name = "send" value = "SEND">
</form>
<?php
}
else
{
	if(sizeof($_POST['mycheck']) == 0)
	{
		//User hasn't made any selection, display error message
		echo "You must select an item";
	}
	else
	{	
		//User has selected at least one item, use the selected item(s) in your query
		
		///////////////////////////////////////////////////
		// foreach ($_POST['mycheck'] as $index => $value)
		//{
		//	echo "$index => $value<br>";
		//}
		//If you use the code enclosed here, you will realise that the array
		//<B>mycheck</B> has numeric index, however you cannot rely on the indices
		//but only the values. This is because the index of the elements will be 
		//different based on the number of items the user selects.
		//////////////////////////////////////////////////
		
		$con = mysql_pconnect("localhost", "root", "password") or die (mysql_error());
		mysql_select_db("database_name") or die(mysql_error());
		$total = 0; //Variable to store total cost of selected items
		
		echo "<table width = '200' border = '1'>";
		echo "<tr><td>FRUIT</td><td>PRICE (GHC)</td></tr>";
		foreach($_POST['mycheck'] as $value)
		{
			//echo "$key = $value <br>";
			$query = "SELECT * FROM fruits WHERE fruit = '".$value."'";
			$result = mysql_query($query) or die(mysql_error());
			$fruit = mysql_fetch_object($result);
			echo "<tr><td>".ucfirst($fruit->fruit)."</td><td>".$fruit->price."</td></tr>";
			$total += $fruit->price;
		}
		echo "<tr><td>Total cost : </td><td><b> $total</b></td></tr>";
		echo "</table>";
	}
}
?>

Here is another way of doing the same thing as the code above. The only difference is that you don't need multiple if-statements to do your processing. To make it easier for you to understand what is going on, do the following.
Create a table and name it fruits. This table should have the two columns, fruit and price. Now populate you table with the following: orange, 10; apple, 5; banana, 15; pear, 20; guava, 2.

Now try this code. I have only one page instead of two.

<?php
if(!isset($_POST['send']))
{
?>
<form method = "post" action = "ppp.php">
<input type = "checkbox" name = "mycheck[]" value = "orange">ORANGE<br>
<input type = "checkbox" name = "mycheck[]" value = "apple">APPLE<br>
<input type = "checkbox" name = "mycheck[]" value = "banana">BANANA<br>
<input type = "checkbox" name = "mycheck[]" value = "pear">PEAR<br>
<input type = "checkbox" name = "mycheck[]" value = "gauva">GUAVA<br>
<input type = "submit" name = "send" value = "SEND">
</form>
<?php
}
else
{
	if(sizeof($_POST['mycheck']) == 0)
	{
		//User hasn't made any selection, display error message
		echo "You must select an item";
	}
	else
	{	
		//User has selected at least one item, use the selected item(s) in your query
		
		///////////////////////////////////////////////////
		// foreach ($_POST['mycheck'] as $index => $value)
		//{
		//	echo "$index => $value<br>";
		//}
		//If you use the code enclosed here, you will realise that the array
		//<B>mycheck</B> has numeric index, however you cannot rely on the indices
		//but only the values. This is because the index of the elements will be 
		//different based on the number of items the user selects.
		//////////////////////////////////////////////////
		
		$con = mysql_pconnect("localhost", "root", "password") or die (mysql_error());
		mysql_select_db("database_name") or die(mysql_error());
		$total = 0; //Variable to store total cost of selected items
		
		echo "<table width = '200' border = '1'>";
		echo "<tr><td>FRUIT</td><td>PRICE (GHC)</td></tr>";
		foreach($_POST['mycheck'] as $value)
		{
			//echo "$key = $value <br>";
			$query = "SELECT * FROM fruits WHERE fruit = '".$value."'";
			$result = mysql_query($query) or die(mysql_error());
			$fruit = mysql_fetch_object($result);
			echo "<tr><td>".ucfirst($fruit->fruit)."</td><td>".$fruit->price."</td></tr>";
			$total += $fruit->price;
		}
		echo "<tr><td>Total cost : </td><td><b> $total</b></td></tr>";
		echo "</table>";
	}
}
?>

Thank you very much!I'll try, though, if-statements are fine for me as I don't have many checkboxes anyways.

You can use the isset() function in XXX2.php to check which boxes were or were not selected.

in XXX2.php

//first check the submit was pressed
if (isset($_POST['s']))
{
   //then check if a checkbox was selected.
   if (isset($_POST['name-of-checkbox1']))
   {
      echo "checkbox 1 was selected";
      // execute database action here
   }
   if (isset($_POST['name-of-checkbox2']))
   {
      echo "checkbox 2 was selected";
      // execute database action here
   }
   if (isset($_POST['name-of-checkbox3']))
   {
      echo "checkbox 3 was selected";
      // execute database action here
   }
}

You will need to give your checkboxes unique names for this to work properly.

Nonshatter, thank you so much!! I got it to work:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.