Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    Regular Coder
    Join Date
    Jul 2007
    Location
    Velsen Noord, Netherlands
    Posts
    207
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Question Retrieve values of checked checkboxes

    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 Code:
    <?php 

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


    if (!empty(
    $_GET['nbs_vmb_lid_vmb'])) ? 0
      
    if (!empty(
    $_GET['nbs_vmb_selfburns'])) ? 0
      
    if (!empty(
    $_GET['nbs_vmb_profrelatie'])) ? 0
         
    if (!empty(
    $_GET['nbs_vmb_psychonet'])) ? 0
         
    if (!empty(
    $_GET['nbs_vmb_revalinet'])) ? 0
         
    if (!empty(
    $_GET['nbs_vmb_reintegranet'])) ? 0
         
    if (!empty(
    $_GET['nbs_vmb_juridischehulp'])) ? 0
         
    if (!empty(
    $_GET['nbs_vmb_nomail'])) ? 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:
    PHP Code:
    <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.

  • #2
    Senior Coder djm0219's Avatar
    Join Date
    Aug 2003
    Location
    Wake Forest, North Carolina
    Posts
    1,294
    Thanks
    4
    Thanked 203 Times in 200 Posts
    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.

    PHP Code:
    if (!empty($_GET['nbs_vmb_lid_vmb'])) ? $nbs_vmb_lid_vmb $nbs_vmb_lid_vmb 0
    Dave .... HostMonster for all of your hosting needs

  • #3
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,852
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    PHP Code:
    if (!empty($_GET['nbs_vmb_lid_vmb'])) ? $nbs_vmb_lid_vmb $nbs_vmb_lid_vmb 0
    Mixed if with conditional operator?
    PHP Code:
    $nbs_vmb_lid_vmb=(isset($_GET['nbs_vmb_lid_vmb'])? 1:0); 
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #4
    Regular Coder
    Join Date
    Jul 2007
    Location
    Velsen Noord, Netherlands
    Posts
    207
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by abduraooft View Post
    Mixed if with conditional operator?
    PHP Code:
    $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:
    PHP Code:
    $nbs_vmb_lid_vmb         = (isset($_GET['nbs_vmb_lid_vmb'])? 0);
    $nbs_vmb_selfburns         = (isset($_GET['nbs_vmb_selfburns']) ? 0);
    $nbs_vmb_profrelatie     = (isset($_GET['nbs_vmb_profrelatie']) ? 0);
    $nbs_vmb_psychonet         = (isset($_GET['nbs_vmb_psychonet']) ? 0);
    $nbs_vmb_revalinet         = (isset($_GET['nbs_vmb_revalinet']) ? 0);
    $nbs_vmb_reintegranet     = (isset($_GET['nbs_vmb_reintegranet']) ? 0);
    $nbs_vmb_juridischehulp = (isset($_GET['nbs_vmb_juridischehulp']) ? 0);
    $nbs_vmb_nomail         = (isset($_GET['nbs_vmb_nomail']) ? 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?

  • #5
    Regular Coder funnymoney's Avatar
    Join Date
    Aug 2007
    Posts
    364
    Thanks
    17
    Thanked 24 Times in 24 Posts
    try this approach, less writing
    PHP Code:
    <?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>

  • #6
    Regular Coder
    Join Date
    Jul 2007
    Location
    Velsen Noord, Netherlands
    Posts
    207
    Thanks
    6
    Thanked 0 Times in 0 Posts
    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.

  • #7
    Regular Coder funnymoney's Avatar
    Join Date
    Aug 2007
    Posts
    364
    Thanks
    17
    Thanked 24 Times in 24 Posts
    maybe like this you can get simpler $sql query

    PHP Code:
    <?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>

  • #8
    Regular Coder
    Join Date
    Jul 2007
    Location
    Velsen Noord, Netherlands
    Posts
    207
    Thanks
    6
    Thanked 0 Times in 0 Posts
    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?

  • #9
    Regular Coder funnymoney's Avatar
    Join Date
    Aug 2007
    Posts
    364
    Thanks
    17
    Thanked 24 Times in 24 Posts
    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...

  • #10
    Regular Coder
    Join Date
    Jul 2007
    Location
    Velsen Noord, Netherlands
    Posts
    207
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by funnymoney View Post
    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
    PHP Code:
    $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:
    PHP Code:
    SELECT nbs_vmb_contact_aanhefnbs_vmb_contact_precharnbs_vmb_contact_namenbs_vmb_contact_tussenvnbs_vmb_contact_surnbs_vmb_addressnbs_vmb_numbernbs_vmb_numberaddnbs_vmb_zipnbs_vmb_postbusnrnbs_vmb_citynbs_vmb_countrynbs_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.

  • #11
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,852
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    Code:
     $result = mysql_query($sql_query) or die(mysql_error());
    Make it as a habit while development.
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #12
    Regular Coder
    Join Date
    Jul 2007
    Location
    Velsen Noord, Netherlands
    Posts
    207
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by abduraooft View Post
    Code:
     $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 )
    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.
    PHP Code:
    $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.

  • #13
    Regular Coder
    Join Date
    Jul 2007
    Location
    Velsen Noord, Netherlands
    Posts
    207
    Thanks
    6
    Thanked 0 Times in 0 Posts
    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:
    PHP 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"

  • #14
    Regular Coder funnymoney's Avatar
    Join Date
    Aug 2007
    Posts
    364
    Thanks
    17
    Thanked 24 Times in 24 Posts
    you can try on something like this

    PHP Code:
    <?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>
    Last edited by funnymoney; 09-16-2009 at 05:05 PM.

  • #15
    Regular Coder
    Join Date
    Jul 2007
    Location
    Velsen Noord, Netherlands
    Posts
    207
    Thanks
    6
    Thanked 0 Times in 0 Posts
    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:
    PHP Code:
    $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.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •