Page 1 of 3
Check the BOX?
Posted: Tue Aug 07, 2007 5:51 pm
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
Posted: Tue Aug 07, 2007 7:17 pm
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.")";
Posted: Tue Aug 07, 2007 7:18 pm
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.
Posted: Tue Aug 07, 2007 7:20 pm
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.
Posted: Tue Aug 07, 2007 7:24 pm
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'?
Posted: Tue Aug 07, 2007 7:29 pm
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.

Posted: Tue Aug 07, 2007 7:34 pm
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".
Posted: Tue Aug 07, 2007 7:41 pm
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.
Posted: Tue Aug 07, 2007 7:50 pm
by xterra
feyd | Please use 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
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]
Posted: Tue Aug 07, 2007 7:57 pm
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:
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.
Posted: Tue Aug 07, 2007 7:58 pm
by phpretard
feyd | Please use 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
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]
Posted: Tue Aug 07, 2007 8:05 pm
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.")";
Posted: Tue Aug 07, 2007 8:14 pm
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;
Posted: Tue Aug 07, 2007 8:21 pm
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

Posted: Tue Aug 07, 2007 8:26 pm
by phpretard
feyd | Please use 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
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]