I have 4 checkbox.
1)name 2)phone 3)address 4)dob 5)all
What i am trying to do suppose name,address,& dob is checked by user,then query is:
Select name,address,dob from table;
Similarly if phone,address
select phone,address from table;
& for all
Select * from table;

I am getting value of checkboxes.
Need suggestions

Recommended Answers

All 7 Replies

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Untitled 1</title>
</head>

<body>
<table>
 <form name="myform" action="<?php $_SERVER['PHP_SELF'];?>" method="post">
  <tr>
      <td><input type="checkbox" name="name_check" value="name" />Name</td>
      <td><input type="checkbox" name="address_check" value="address" />Address</td>
      <td><input type="checkbox" name="contact_check" value="mobile" />Contact No</td>
      <td><input type="checkbox" name="dob_check" value="dob" />DOB</td>
      <td><input type="checkbox" name="namdan_check" value="namdan_date"/>namdan date</td>
  </tr>
  <tr><td><input type="submit" name="submit" value="submit"/></td></tr>
   </form>
</table>
</body>

</html>
<?php
include("db_hindi.php");
if (isset($_POST['name_check']) && isset($_POST['address_check']) && isset($_POST['dob_check']) && isset($_POST['contact_check']) && isset($_POST['namdan_check']) )
 {
  $result=mysql_query("select * from tbl_hindi");
  // do work here
  
} 
elseif()
{
//do work
}
else
{
//do work
}
?>

In upper code i have to put no. of conditions.but
if there are no of checkbox cases this is not a good method.
need suggestion for any other method.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
 
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Untitled 1</title>
</head>
 
<body>
<table>
 <form name="myform" action="page2.php" method="post">
  <tr>
      <td><input type="checkbox" name="a[]" value="name" />Name</td>
      <td><input type="checkbox" name="a[]" value="address" />Address</td>
      <td><input type="checkbox" name="a[]" value="mobile" />Contact No</td>
      <td><input type="checkbox" name="a[]" value="dob" />DOB</td>
      <td><input type="checkbox" name="a[]" value="namdan_date"/>namdan date</td>
  </tr>
  <tr><td><input type="submit" name="submit" value="submit"/></td></tr>
   </form>
</table>
</body>
 
</html>

page2.php

<?php
include("db_hindi.php");
 $chk = $_POST['a'];
  if(empty($chk))
  {
    echo("Sorry wrong query");
  }
  else
  {
    $N = count($aDoor);
 if($N==5)
     {
      $result=mysql_query("select * from tbl_hindi");
     }
else{ $b="";
    for($i=0; $i < $N; $i++)
      {
      $b=$b + $i + " "; 
      }
$result=mysql_query("select ".$b."from tbl_hindi");
     }
   }

?>

Although i haven't checked the script but it will work...make suitable arrangement according to your convenience...
The concept here is that you use array to store all the checked boxes and then just using it make your query....

Group the checkbox with the unique name like @IIM method. But, you don't need many query within the loop. It is unnecessarily extra process. Try this:

if(isset($_POST['submit'])){
	$sql = "SELECT";
	$check = (array) $_POST['a'];
	for($i = 0; $i < count($check); $i++) {
		//assign the query string
		$sql .= ($i <= count($check)) ? " `$check[$i]`" : " `$check[$i]`, ";
	}
	//concatenate the require strign for query
	$sql .= " from tbl_hindi";
	//execute the query
	$query = mysql_query($sql);
}

As you see, the query string will concatenate within the loop with the amount of the check-box which are already checked by the users. There is no problem even if you don't have 'SELECT *', when all check-boxes was checked. In this condition, the query string will be

SELECT `name`, `address`, `mobile`, `dob`, `namdan_date`

. It's similar to 'SELECT *' if your table was built with these fields.
Also, line 11, you've this code.

<form name="myform" action="<?php $_SERVER;?>" method="post">

<?php $_SERVER['PHP_SELF'];?> should be <?php echo $_SERVER['PHP_SELF'];?>

commented: Nice work...i was thinking something similar but can't conclude to exact point.... +2
$sql .= ($i <= count($check)) ? " `$check[$i]`" : " `$check[$i]`, ";

output:SELECT `name` `address` `mobile` `dob` from tbl_hindi
query error

$sql .= ($i <= count($check)) ? " `$check[$i]`," : " `$check[$i]`,";

SELECT `name`, `address`, `mobile`, from tbl_hindi
extra comma query failed

Group the checkbox with the unique name like @IIM method. But, you don't need many query within the loop. It is unnecessarily extra process. Try this:

if(isset($_POST['submit'])){
	$sql = "SELECT";
	$check = (array) $_POST['a'];
	for($i = 0; $i < count($check); $i++) {
		//assign the query string
		$sql .= ($i <= count($check)) ? " `$check[$i]`" : " `$check[$i]`, ";
	}
	//concatenate the require strign for query
	$sql .= " from tbl_hindi";
	//execute the query
	$query = mysql_query($sql);
}

As you see, the query string will concatenate within the loop with the amount of the check-box which are already checked by the users. There is no problem even if you don't have 'SELECT *', when all check-boxes was checked. In this condition, the query string will be

SELECT `name`, `address`, `mobile`, `dob`, `namdan_date`

. It's similar to 'SELECT *' if your table was built with these fields.
Also, line 11, you've this code. <?php $_SERVER['PHP_SELF'];?> should be <?php echo $_SERVER['PHP_SELF'];?>

Sorry! It is wrong. Here is correct one.

$sql .= ($i == (count($check)-1)) ? " `$check[$i]`" : " `$check[$i]`,";

Echoing the query string on the browser, before executing is good practice. You'll see your query is properly collect or not. And you can copy and paste this string into your phpMyAdmin and can see how the query shows.
Hope this help!

try this

<?php
    include("db_hindi.php");
    if($_POST["submit"])
	{
		echo $qry="select ".implode(",",$_POST["check_qry"])." from tbl_hindi";
                mysql_query($qry);
	}
    ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
     
    <head>
    <meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
    <title>Untitled 1</title>
    </head>
     
    <body>
    <table>
    <form name="myform" action="" method="post">
    <tr>
    <td><input type="checkbox" name="check_qry[]" value="name" />Name</td>
    <td><input type="checkbox" name="check_qry[]" value="address" />Address</td>
    <td><input type="checkbox" name="check_qry[]" value="mobile" />Contact No</td>
    <td><input type="checkbox" name="check_qry[]" value="dob" />DOB</td>
    <td><input type="checkbox" name="check_qry[]" value="namdan_date"/>namdan date</td>
    </tr>
    <tr><td><input type="submit" name="submit" value="submit"/></td></tr>
    </form>
    </table>
    </body>
     
    </html>
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.