...

View Full Version : Retrieve values of checked checkboxes



UD2006
09-15-2009, 12:38 PM
I have a page on which I can make selections and display the values of those selection (selected checkboxes).

For example, on the page I have checkboxes 1 till 6, and 2, 4, 5 are checked, the records in mysql with the values 1 on fields or checkboxes 2, 4 and 5 should be displayed. If no checkbox is checked no records are being displayed(exported).

This is the code from the export page: (the records are being exported to an excel file, that works, but no records are in that file, when I know a record has a value of 1 from a checked checkbox).


<?php

function exportMysqlToCsv($table,$filename = 'selecties contact database.csv')
{

if (!empty($_GET['nbs_vmb_lid_vmb'])) ? 1 : 0;

if (!empty($_GET['nbs_vmb_selfburns'])) ? 1 : 0;

if (!empty($_GET['nbs_vmb_profrelatie'])) ? 1 : 0;

if (!empty($_GET['nbs_vmb_psychonet'])) ? 1 : 0;

if (!empty($_GET['nbs_vmb_revalinet'])) ? 1 : 0;

if (!empty($_GET['nbs_vmb_reintegranet'])) ? 1 : 0;

if (!empty($_GET['nbs_vmb_juridischehulp'])) ? 1 : 0;

if (!empty($_GET['nbs_vmb_nomail'])) ? 1 : 0;

$csv_terminated = "\n";
$csv_separator = ";";
$csv_enclosed = '"';
$csv_escaped = "\\";
$sql_query = "SELECT nbs_vmb_contact_aanhef, nbs_vmb_contact_prechar, nbs_vmb_contact_name, nbs_vmb_contact_tussenv, nbs_vmb_contact_sur, nbs_vmb_address, nbs_vmb_number, nbs_vmb_numberadd, nbs_vmb_zip, nbs_vmb_postbusnr, nbs_vmb_city, nbs_vmb_country, nbs_vmb_emailadres FROM $table where nbs_vmb_group_id = '2' AND nbs_vmb_lid_vmb = $nbs_vmb_lid_vmb";

// Gets the data from the database
$result = mysql_query($sql_query);
$fields_cnt = mysql_num_fields($result);
?>


This is the code from the selection page:


<form id="form_ins_contact" name="form_ins_contact" method="get" action="test.php">
<table width="680" height="350" border="0" align="center" cellpadding="0" cellspacing="2">
<tr>
<td width="160" align="right"><strong><?php echo $tcm_label[36]; ?></strong></td>
<td class="td_no_border"><input name="nbs_vmb_lid_vmb" type="checkbox" value="1" <?php if ($nbs_vmb_lid_vmb == "1") echo "checked"; ?> /></td>
<td class="td_no_border2"></td>
<td class="td_no_border2"></td>
</tr>
<tr>
<td width="160" align="right"><strong><?php echo $tcm_label[37]; ?></strong></td>
<td class="td_no_border"><input value="1" name="nbs_vmb_selfburns" type="checkbox" <?php if ($nbs_vmb_selfburns == "1") echo "checked"; ?> /></td>
<td align="right"><strong><?php echo $tcm_label[29]; ?></strong></td>
<td class="td_no_border"><input name="nbs_vmb_zip" type="text" id="nbs_vmb_zip" size="15" /></td>
</tr>
<tr>
<td width="160" align="right"><strong><?php echo $tcm_label[40]; ?></strong></td>
<td class="td_no_border"><input value="1" name="nbs_vmb_profrelatie" type="checkbox" <?php if ($nbs_vmb_profrelatie == "1") echo "checked"; ?> /></td>
<td align="right"><strong><?php echo $tcm_label[35]; ?></strong></td>
<td class="td_no_border"><input name="nbs_vmb_contact_day" type="text" id="nbs_vmb_contact_day" maxlength="2" size="1" />
<input name="nbs_vmb_contact_month" type="text" id="nbs_vmb_contact_month" maxlength="2" size="1" />
<input name="nbs_vmb_contact_year" type="text" id="nbs_vmb_contact_year" maxlength="4" size="3" /></td>
</tr>
<tr>
<td width="160" align="right"><strong><?php echo $tcm_label[42]; ?></strong></td>
<td class="td_no_border"><input value="1" name="nbs_vmb_revalinet" type="checkbox" <?php if ($nbs_vmb_revalinet == "1") echo "checked"; ?> /></td>
<td align="right"><strong><?php echo $tcm_label[38]; ?></strong></td>
<td class="td_no_border" width="194"><input name="nbs_vmb_year" type="text" id="nbs_vmb_year" size="15" /></td>
</tr>
<tr>
<td align="right"><strong><?php echo $tcm_label[44]; ?></strong></td>
<td class="td_no_border"><input value="1" name="nbs_vmb_juridischehulp" type="checkbox" <?php if ($nbs_vmb_juridischehulp == "1") echo "checked"; ?> /></td>
<td align="right"><strong><?php echo $tcm_label[41]; ?></strong></td>
<td class="td_no_border" width="194"><input value="1" name="nbs_vmb_psychonet" type="checkbox" <?php if ($nbs_vmb_psychonet == "1") echo "checked"; ?> /></td>
</tr>
<tr>
<td align="right"><strong><?php echo $tcm_label[45]; ?></strong></td>
<td class="td_no_border"><input value="1" name="nbs_vmb_nomail" type="checkbox" <?php if ($nbs_vmb_nomail == "1") echo "checked"; ?> /></td>
<td align="right"><strong><?php echo $tcm_label[43]; ?></strong></td>
<td class="td_no_border"><input value="1" name="nbs_vmb_reintegranet" type="checkbox" <?php if ($nbs_vmb_reintegranet == "1") echo "checked"; ?> /></td>
</tr>
<input type="submit" class="buttons" value="Export Selecties" />
</form>


Hope someone can help me with this.

djm0219
09-15-2009, 01:17 PM
You aren't setting a variable if the boxes are checked. You are just setting a 1 or zero. I can't quite tell what you are trying to accomplish but set a variable to 1 or zero based on the checks something along these lines.



if (!empty($_GET['nbs_vmb_lid_vmb'])) ? $nbs_vmb_lid_vmb = 1 : $nbs_vmb_lid_vmb = 0;

abduraooft
09-15-2009, 01:20 PM
if (!empty($_GET['nbs_vmb_lid_vmb'])) ? $nbs_vmb_lid_vmb = 1 : $nbs_vmb_lid_vmb = 0; Mixed if with conditional operator?

$nbs_vmb_lid_vmb=(isset($_GET['nbs_vmb_lid_vmb'])? 1:0);

UD2006
09-15-2009, 02:23 PM
Mixed if with conditional operator?

$nbs_vmb_lid_vmb=(isset($_GET['nbs_vmb_lid_vmb'])? 1:0);

Thanks I've tried your code but no results.

This is the code and query as I have them now:


$nbs_vmb_lid_vmb = (isset($_GET['nbs_vmb_lid_vmb'])? 1 : 0);
$nbs_vmb_selfburns = (isset($_GET['nbs_vmb_selfburns']) ? 1 : 0);
$nbs_vmb_profrelatie = (isset($_GET['nbs_vmb_profrelatie']) ? 1 : 0);
$nbs_vmb_psychonet = (isset($_GET['nbs_vmb_psychonet']) ? 1 : 0);
$nbs_vmb_revalinet = (isset($_GET['nbs_vmb_revalinet']) ? 1 : 0);
$nbs_vmb_reintegranet = (isset($_GET['nbs_vmb_reintegranet']) ? 1 : 0);
$nbs_vmb_juridischehulp = (isset($_GET['nbs_vmb_juridischehulp']) ? 1 : 0);
$nbs_vmb_nomail = (isset($_GET['nbs_vmb_nomail']) ? 1 : 0);

$csv_terminated = "\n";
$csv_separator = ";";
$csv_enclosed = '"';
$csv_escaped = "\\";
$sql_query = "SELECT nbs_vmb_contact_aanhef, nbs_vmb_contact_prechar, nbs_vmb_contact_name, nbs_vmb_contact_tussenv, nbs_vmb_contact_sur, nbs_vmb_address, nbs_vmb_number, nbs_vmb_numberadd, nbs_vmb_zip, nbs_vmb_postbusnr, nbs_vmb_city, nbs_vmb_country, nbs_vmb_emailadres FROM $table where nbs_vmb_group_id = '2' AND nbs_vmb_lid_vmb = $nbs_vmb_lid_vmb AND nbs_vmb_selfburns = $nbs_vmb_selfburns AND nbs_vmb_profrelatie = $nbs_vmb_profrelatie AND nbs_vmb_psychonet = $nbs_vmb_psychonet AND nbs_vmb_revalinet = $nbs_vmb_revalinet AND nbs_vmb_reintegranet = $nbs_vmb_reintegranet AND nbs_vmb_juridischehulp = $nbs_vmb_juridischehulp AND nbs_vmb_nomail = $nbs_vmb_nomail";


Any idea?

funnymoney
09-15-2009, 02:45 PM
try this approach, less writing

<?php
if (isset($_POST['checkoption'])) {
foreach ($_POST['checkoption'] as $option) {
print "Choosed $option<br>";
}
}
?>
<form action="" method="post">
<input type="checkbox" name="checkoption[]" value="num1" />1
<input type="checkbox" name="checkoption[]" value="num2" />2
<input type="checkbox" name="checkoption[]" value="num3" />3
<input type="checkbox" name="checkoption[]" value="num4" />4
<input type="submit">
</form>

UD2006
09-15-2009, 02:53 PM
Thanks, I've read about this option, but I thought this was only for entries in 1 field of mysql, I use different columns (check my last post),

Don't know if that applies also for me.

funnymoney
09-15-2009, 03:08 PM
maybe like this you can get simpler $sql query


<?php
if (isset($_POST['checkoption'])) {
$prefix = "nbs_vmb_";
$fields = "";
#for last sql ,
$numfields = count($_POST['checkoption']);
print $numfields;
$i=1;
foreach ($_POST['checkoption'] as $option) {
if ($i < $numfields) {
$fields .= $prefix.$option.", ";
}
else {
$fields .= $prefix.$option;
}
$i++;
}

$sql_query = 'SELECT' .$fields.' FROM $table where group_id = \'2\' AND lid_vmb = $lid_vmb AND selfburns = $selfburns AND profrelatie = $profrelatie AND psychonet = $psychonet AND revalinet = $revalinet AND reintegranet = $reintegranet AND juridischehulp = $juridischehulp AND nomail = $nomail';

print $sql_query;
}

?>
<form action="" method="post">
<input type="checkbox" name="checkoption[]" value="contact_aanhef" />1
<input type="checkbox" name="checkoption[]" value="contact_prechar" />2
<input type="checkbox" name="checkoption[]" value="contact_name" />3
<input type="checkbox" name="checkoption[]" value="contact_tussenv" />4
<input type="submit">
</form>

UD2006
09-16-2009, 06:16 AM
Thanks.

Where do you get the $fields from? Where there is (in a earlier post of mine) Select ........ from there are columns (of records I want to export), but I see in your last post that you gave the checkboxes the value as those columns.

But it should be: nbs_vmb_lid_vmb etc.

So the select should be something like: select nbs_vmb_contact_aanhef, nbs_vmb_contact_prechar, ...... From

I hope that this makes more sence or did I misunderstood your comment?

funnymoney
09-16-2009, 07:24 AM
i was just trying to show you another way of using checkboxes. to me it's logical that if checkbox is checked that it's value is one, no?
so if you already have that "true" statement, why don't use value property of checkbox to transport more useful data to script, like maybe field names, or something even more useful, like checkbox ID value if any...

UD2006
09-16-2009, 08:27 AM
i was just trying to show you another way of using checkboxes. to me it's logical that if checkbox is checked that it's value is one, no?
so if you already have that "true" statement, why don't use value property of checkbox to transport more useful data to script, like maybe field names, or something even more useful, like checkbox ID value if any...

Sorry I'm not trying to go against you, but I'm on a tight schedule (need to find the solution today), so hope you can help me.

Btw I've echoed the $sql_query


$sql_query = "SELECT nbs_vmb_contact_aanhef, nbs_vmb_contact_prechar, nbs_vmb_contact_name, nbs_vmb_contact_tussenv, nbs_vmb_contact_sur, nbs_vmb_address, nbs_vmb_number, nbs_vmb_numberadd, nbs_vmb_zip, nbs_vmb_postbusnr, nbs_vmb_city, nbs_vmb_country, nbs_vmb_emailadres FROM $table where nbs_vmb_group_id = '2' AND nbs_vmb_lid_vmb = $nbs_vmb_lid_vmb AND nbs_vmb_selfburns = $nbs_vmb_selfburns AND nbs_vmb_profrelatie = $nbs_vmb_profrelatie AND nbs_vmb_psychonet = $nbs_vmb_psychonet AND nbs_vmb_revalinet = $nbs_vmb_revalinet AND nbs_vmb_reintegranet = $nbs_vmb_reintegranet AND nbs_vmb_juridischehulp = $nbs_vmb_juridischehulp AND nbs_vmb_nomail = $nbs_vmb_nomail";


And got the following results back:


SELECT nbs_vmb_contact_aanhef, nbs_vmb_contact_prechar, nbs_vmb_contact_name, nbs_vmb_contact_tussenv, nbs_vmb_contact_sur, nbs_vmb_address, nbs_vmb_number, nbs_vmb_numberadd, nbs_vmb_zip, nbs_vmb_postbusnr, nbs_vmb_city, nbs_vmb_country, nbs_vmb_emailadres FROM nbs_vmb_contacts where nbs_vmb_group_id = '2' AND nbs_vmb_lid_vmb = AND nbs_vmb_selfburns = AND nbs_vmb_profrelatie = AND nbs_vmb_psychonet = AND nbs_vmb_revalinet = AND nbs_vmb_reintegranet = AND nbs_vmb_juridischehulp = AND nbs_vmb_nomail =


The test that I made was I only had the nbs_vmb_lid_vmb checkbox selected so it should not have put all the others in the query and the checkbox I selected should have had '1' after the = sign.

abduraooft
09-16-2009, 08:32 AM
$result = mysql_query($sql_query) or die(mysql_error()); Make it as a habit while development.

UD2006
09-16-2009, 08:46 AM
$result = mysql_query($sql_query) or die(mysql_error()); Make it as a habit while development.

Thanks that was stupid of me, (I'll try to make it a habit :thumbsup: )
I've changed the query as followed, but when I have the nbs_vmb_lid_vmb checkbox checked, I still don't get any records.


$sql_query = "SELECT nbs_vmb_contact_aanhef, nbs_vmb_contact_prechar, nbs_vmb_contact_name, nbs_vmb_contact_tussenv, nbs_vmb_contact_sur, nbs_vmb_address, nbs_vmb_number, nbs_vmb_numberadd, nbs_vmb_zip, nbs_vmb_postbusnr, nbs_vmb_city, nbs_vmb_country, nbs_vmb_emailadres FROM `$table` WHERE `nbs_vmb_group_id`='2' AND `nbs_vmb_lid_vmb`='$nbs_vmb_lid_vmb' AND `nbs_vmb_selfburns`='$nbs_vmb_selfburns' AND `nbs_vmb_profrelatie`='$nbs_vmb_profrelatie' AND `nbs_vmb_psychonet`='$nbs_vmb_psychonet' AND `nbs_vmb_revalinet`='$nbs_vmb_revalinet' AND `nbs_vmb_reintegranet`='$nbs_vmb_reintegranet' AND `nbs_vmb_juridischehulp`='$nbs_vmb_juridischehulp' AND `nbs_vmb_nomail`='$nbs_vmb_nomail'";


The problem of the error were the ` and ' I had to add.

UD2006
09-16-2009, 11:24 AM
I've made some changed and no when I select a checkbox I get a record, but when I select 2 checkboxes ( seperate columns, checkbox 1 has 1 in the column and checkbox 3 has 0 in the column), I don't get any result again.

It looks like they neutralize, because I still should get a record, (when I select checkbox 3 only, I get no records, but is suppose to happen).

Here is the changed code:


$query .= !empty($_GET['nbs_vmb_lid_vmb']) ? ' AND `nbs_vmb_lid_vmb` = 1' : '';
$query .= !empty($_GET['nbs_vmb_selfburns']) ? ' AND `nbs_vmb_selfburns` = 1' : '';
$query .= !empty($_GET['nbs_vmb_profrelatie']) ? ' AND `nbs_vmb_profrelatie` = 1' : '';
$query .= !empty($_GET['nbs_vmb_psychonet']) ? ' AND `nbs_vmb_psychonet` = 1' : '';
$query .= !empty($_GET['nbs_vmb_revalinet']) ? ' AND `nbs_vmb_revalinet` = 1' : '';
$query .= !empty($_GET['nbs_vmb_reintegranet']) ? ' AND `nbs_vmb_reintegranet` = 1' : '';
$query .= !empty($_GET['nbs_vmb_juridischehulp']) ? ' AND `nbs_vmb_juridischehulp` = 1' : '';
$query .= !empty($_GET['nbs_vmb_nomail']) ? ' AND `nbs_vmb_nomail` = 1' : '';

$csv_terminated = "\n";
$csv_separator = ";";
$csv_enclosed = '"';
$csv_escaped = "\\";
$sql_query = "SELECT nbs_vmb_contact_aanhef, nbs_vmb_contact_prechar, nbs_vmb_contact_name, nbs_vmb_contact_tussenv, nbs_vmb_contact_sur, nbs_vmb_address, nbs_vmb_number, nbs_vmb_numberadd, nbs_vmb_zip, nbs_vmb_postbusnr, nbs_vmb_city, nbs_vmb_country, nbs_vmb_emailadres FROM `$table` WHERE `nbs_vmb_group_id`='2' $query";

funnymoney
09-16-2009, 04:51 PM
you can try on something like this


<?php
$sqlAND = NULL; //edited to initiate before isset (if null doesnt work try $sqlAND ="";
if (isset($_POST['nbs_vbm'])) {
foreach ($_POST['nbs_vbm'] as $option) {
$sqlAND .="AND $option = '1' ";
}
print $sqlAND;
}



$csv_terminated = "\n";
$csv_separator = ";";
$csv_enclosed = '"';
$csv_escaped = "\\";
$sql_query = "SELECT nbs_vmb_contact_aanhef, nbs_vmb_contact_prechar, nbs_vmb_contact_name, nbs_vmb_contact_tussenv, nbs_vmb_contact_sur, nbs_vmb_address, nbs_vmb_number, nbs_vmb_numberadd, nbs_vmb_zip, nbs_vmb_postbusnr, nbs_vmb_city, nbs_vmb_country, nbs_vmb_emailadres FROM `$table` WHERE `nbs_vmb_group_id`='2' $sqlAND";
?>
<form action="" method="post">
<input type="checkbox" name="nbs_vbm[]" value="nbs_vmb_lid_vmb" />
<input type="checkbox" name="nbs_vbm[]" value="nbs_vmb_selfburns" />
<input type="checkbox" name="nbs_vbm[]" value="nbs_vmb_profrelatie" />
<input type="checkbox" name="nbs_vbm[]" value="nbs_vmb_psychonet" />
<input type="checkbox" name="nbs_vbm[]" value="nbs_vmb_revalinet" />
<input type="checkbox" name="nbs_vbm[]" value="nbs_vmb_reintegranet" />
<input type="checkbox" name="nbs_vbm[]" value="nbs_vmb_juridischehulp" />
<input type="checkbox" name="nbs_vbm[]" value="nbs_vmb_nomail" />
<input type="submit">
</form>

UD2006
09-17-2009, 10:02 AM
Thanks funnymonkey, it works.

There is only a small matter I hope to solve with this query and that is the following:

If I'm logged in, in group 2, and I make a selection; i've checked checkbox: <input type="checkbox" name="nbs_vbm[]" value="nbs_vmb_profrelatie" />

The query is as followed:


$sql_query = "SELECT nbs_vmb_contact_aanhef, nbs_vmb_contact_prechar, nbs_vmb_contact_name, nbs_vmb_contact_tussenv, nbs_vmb_contact_sur, nbs_vmb_address, nbs_vmb_number, nbs_vmb_numberadd, nbs_vmb_zip, nbs_vmb_postbusnr, nbs_vmb_city, nbs_vmb_country, nbs_vmb_emailadres FROM `$table` WHERE `nbs_vmb_group_id`='2' OR `nbs_vmb_shared`='1' $sqlAND";

In the database a record (in group 2) has a 0 in the column of nbs_vmb_profrelatie, but because nbs_vmb_shared is set to 1, a record with a 1 in the column nbs_vmb_shared is displayed, this is correct. Only the record from group 2 has a 0 in column nbs_vmb_profrelatie (as I told), so this record shouldn't be displayed, only the record with shared should be displayed.

I think the query is not totally correct, any ideas on that.

funnymoney
09-17-2009, 01:58 PM
well that is more of a SQL question, there is good SQL forum here, so you might ask about it there.

i'm personally using different database organization than you are now. more tables with more unique id's, tables are i believe in relation (i'm sql noob), and when i need to connect more tables, i use left joint, or right joint :)

UD2006
09-17-2009, 02:37 PM
i'm personally using different database organization than you are now. more tables with more unique id's, tables are i believe in relation (i'm sql noob), and when i need to connect more tables, i use left joint, or right joint :)

Thanks, that was my exact thought also, started to work on the left joint and right joint this morning.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum