Check the BOX?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

phpretard
Forum Commoner
Posts: 26
Joined: Wed Aug 01, 2007 8:03 pm
Location: Orlando Florida

Check the BOX?

Post by phpretard »

Is it possible to select a row or multiple rows based on a checkboxes in a form?

Here is the wannabe code:

Code: Select all

$query = "SELECT email FROM testemail WHERE $checkbox1 '$checkbox2', '$checkbox3', '$and_so_on' = yes";

<form action= nothing_yet.php  name=don't_work>

<input type=checkbox name=instructional id=instructional value=yes>

</form>
Don't let all the yeses concern you.

I want to select the rows that have the value "yes".

Possible???

Thank you in advance.

Respectfully,

Anthony
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Post by VladSun »

I usually do this like that:

Code: Select all

<form action="" method="post">
       <input type="checkbox" name="cb[1]">
       <input type="checkbox" name="cb[2]">
       <input type="checkbox" name="cb[6]">
       <input type="checkbox" name="cb[120]">
       <input type="checkbox" name="cb[34]">
</form>

Code: Select all

$cbs = $_POST['cb'];
$query = "0";

foreach ($cb as $id => $checked)
{
     if ($checked == 'on')
     {
           $query .= ", ".$id;
     }
}

$query = "select data from table where id in (".$query.")";
Last edited by VladSun on Tue Aug 07, 2007 7:25 pm, edited 1 time in total.
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Yes, you could do that. You'd likely want to make sure that you validate all of the data, or you'll be prone to SQL injection.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

VladSun wrote:I usually do this like that:

Code: Select all

<form action="" method="post">
       <input type="checkbox" name="cb[1]">
       <input type="checkbox" name="cb[2]">
       <input type="checkbox" name="cb[6]">
       <input type="checkbox" name="cb[120]">
       <input type="checkbox" name="cb[34]">
</form>

Code: Select all

$cbs = $_POST['cb'];
$query = "0"

foreach ($cb as $id => $checked)
{
     if ($checked == 'on')
     {
           $query .= ", ".$id;
     }
}

$query = "select data from table where id in (".$query.")";
You could do that by giving a 'value' attribute instead.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Post by VladSun »

I always validate and sanitize user data. But ... this is another answer to another question ;)

I think that using 'value' will just add some redundant HTML code. What are the advantages of using 'value'?
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

VladSun wrote:I always validate and sanitize user data. But ... this is another answer to another question ;)
I figured you do. That comment was in response to the OP. I didn't see your post till afterwards.
VladSun wrote:I think that using 'value' will just add some redundant HTML code. What are the advantages of using 'value'?
You could actually use the table names. Then again, on second thought, I can't think of a reason to want to put your table names into the HTML. :?
xterra
Forum Commoner
Posts: 69
Joined: Mon Mar 06, 2006 12:52 pm

Post by xterra »

How can I loop through a var_dump? The user selects a certain amount of checkboxes, and I have it stored in an array. The above doesn't make sense to me, I do not have any variables named "$checked" or "$on".
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Post by VladSun »

1. Why do you use "var_dump"?
2. variable $checked is defined in foreach loop ...
3. variable $on doesn't exist in my code - it is a value...

you have to addapt my/your code to your/my code ;)

PS:

4. If you use names like "checkbox_name[checbox_id]" you will already have an array in $_POST['checkbox_name']. No need to parse etc.
There are 10 types of people in this world, those who understand binary and those who don't
xterra
Forum Commoner
Posts: 69
Joined: Mon Mar 06, 2006 12:52 pm

Post by xterra »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Because feyd told me to  

Here's my code:

[syntax="html"]<form name=form1 method=post action=exe_send_mails.php> 
<input type="checkbox" name="contact[]" value="1">1</td> 
<input type="checkbox" name="contact[]" value="2">2</td> 
<input type="checkbox" name="contact[]" value="3">3</td> 
<input type="checkbox" name="contact[]" value="4">4</td> 
<input type="checkbox" name="contact[]" value="5">5</td> 
<input type=submit name=Submit value=Continue> 
</form>
I need to see which ones were checked. Feyd said that on the exe_send_mails.php file, I should use POST data to get the contact[] array, then use var_dump(contact[]).


feyd | Please use[/syntax]

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Post by VladSun »

I think that feyd hadn't this in mind...

I would write:

Code: Select all

<form name=form1 method=post action=exe_send_mails.php>
 <input type="checkbox" name="contact[1]">1</td>
 <input type="checkbox" name="contact[2]">2</td>
 <input type="checkbox" name="contact[3]">3</td>
 <input type="checkbox" name="contact[4]">4</td>
 <input type="checkbox" name="contact[5]">5</td>
 <input type=submit name=Submit value=Continue>
</form> 
Then in your exe_send_mails.php:

Code: Select all

$contact = $_POST['contact']
and there you have the array $contact with these keys/values:

Code: Select all

$contact[1] = 'on';
$contact[2] = 'on';
$contact[5] = 'on';
assuming that onlu checkboxes 1,2,5 have been checked.
There are 10 types of people in this world, those who understand binary and those who don't
phpretard
Forum Commoner
Posts: 26
Joined: Wed Aug 01, 2007 8:03 pm
Location: Orlando Florida

Post by phpretard »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Code: Select all

<?php

$to ="email@email.com";

$subject =$_POST['subject']; 

$message = $_POST['body'];



$username = '';
$password = ''; 
$database = '';


$con=mysql_connect($database,$username,$password);
@mysql_select_db($database) or die("<b>Unable to specified database</b>");



$query = "SELECT email FROM testemail WHERE ////((((((((((checked checkboxes only)))))))))\\\\ = yes";
$result=mysql_query($query) or die('Error, query failed');

mysql_close($con);

$row=1;
$numrows=mysql_num_rows($result);
$bccfield="Bcc: ". mysql_result($result,0,"email");
while($row<$numrows)
{
$email=mysql_result($result,$row,'email');
$bccfield .= "," . $email; //seperate by comma 
$row++;
}
$bccfield .= "\r\n"; 


$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= $bccfield;

$headers .= 'From: email>' . "\r\n";

// Mail it
mail($to, $subject, $message,$headers);



?>
Here are the checkboxes:

Code: Select all

<form action=code.php>

<input type=checkbox name=instructional>

<input type=checkbox name="tball">

<input type=checkbox name="rookie">

<input type=checkbox name="minor">

<input type=checkbox name="major">

<input type=checkbox name="prep">

<input type=checkbox name="mom">

</form>
I have a checkbox named after each sql row name.

Each of the rows have a "yes" or nothing as the value.

So...I was hoping to select (with checkboxs) each row with "yes" as a value to send an email to.


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Post by VladSun »

... it is still the same ... interpolate, pls ...

Code: Select all

<input type=checkbox name="cb['tball']">
<input type=checkbox name="cb['rookie']">
<input type=checkbox name="cb['minor']">
<input type=checkbox name="cb['major']">
<input type=checkbox name="cb['prep']">
<input type=checkbox name="cb['mom']"> 

Code: Select all

$cbs = $_POST['cb'];
$query = "1=1 "

foreach ($cb as $id => $checked)
{
     if ($checked == 'on')
     {
           $query .= "and ".$id."='yes' ";
     }
}

$query = "select data from table where (".$query.")";
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Only checkboxes that have been checked will be submitted. Therefore, there is no need to loop to verify it's value.

Code: Select all

$where = '';
if (!empty($_POST['cb']) && count($_POST['cb')) {
   $where = 'WHERE `id` IN ('. implode(', ', $_POST['cb']).')';
}

$query = 'SELECT `data` FROM `table` '. $where;
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Post by VladSun »

Jcart wrote:Only checkboxes that have been checked will be submitted. Therefore, there is no need to loop to verify it's value.

Code: Select all

$where = '';
if (!empty($_POST['cb']) && count($_POST['cb')) {
   $where = 'WHERE `id` IN ('. implode(', ', $_POST['cb']).')';
}

$query = 'SELECT `data` FROM `table` '. $where;
Beautiful :)
There are 10 types of people in this world, those who understand binary and those who don't
phpretard
Forum Commoner
Posts: 26
Joined: Wed Aug 01, 2007 8:03 pm
Location: Orlando Florida

Post by phpretard »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


You lost me on this one...

here is what I have now:

Code: Select all

@mysql_select_db($database) or die("<b>Unable to specified database</b>");

$cbs = $_POST['cb']; 
$query = "1=1 " 

$where = ''; 

if (!empty($_POST['cb']) && count($_POST['cb')) { 

   $where = 'WHERE `id` IN ('. implode(', ', $_POST['cb']).')'; 
} 

$query = 'SELECT email FROM contacts . $where;

$result=mysql_query($query) or die('Error, query failed');


mysql_close($con);

$row=1;
$numrows=mysql_num_rows($result);
$bccfield="Bcc: ". mysql_result($result,0,"email");
while($row<$numrows)
{
$email=mysql_result($result,$row,'email');
$bccfield .= "," . $email;  
$row++;
}
$bccfield .= "\r\n"; 

// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= $bccfield;

$headers .= 'From: LHBRB <ami@mail.com>' . "\r\n";

// Mail it
mail($to, $subject, $message,$headers);


?>


form--------

Code: Select all

<input type=checkbox name="cb['tball']"> 
<input type=checkbox name="cb['rookie']"> 
<input type=checkbox name="cb['minor']"> 
<input type=checkbox name="cb['major']"> 
<input type=checkbox name="cb['prep']"> 
<input type=checkbox name="cb['mom']"> 
Is this right?


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Post Reply