www.webdeveloper.com
Results 1 to 7 of 7

Thread: Pesky checkboxes with PHP

  1. #1
    Join Date
    Mar 2014
    Location
    Corpus Christi, TX
    Posts
    91

    Pesky checkboxes with PHP

    All,

    I have data from MySQL displayed in a table. I am trying to include checkboxes on each line and then a single "Submit" button that will update the database.

    I have successfully displayed the checkboxes in my table and created the PHP file that will initiate the update.

    Here is where I have trouble:

    1. The submit button is on each row, instead of one single button
    2. The PHP file to which I am trying to post only shows the first checked value, not all of them

    I've never dealt with checkboxes and PHP before, so I'm hoping the geniuses here can help.

    OpenIssues.php:

    PHP Code:
    <?php
    mysql_connect
    ('localhost','****','****'); $query=("SELECT * FROM speedtest.data WHERE issuecreated = 'New' ORDER by ID DESC");$result=mysql_query($query);$num=mysql_numrows($result);$i=0; while ($i $num) {
    $ComputerName=mysql_result($result,$i,"ComputerName");$ExpectedRig=mysql_result($result,$i,"ExpectedRig");$Timestamp=mysql_result($result,$i,"Timestamp");$download=mysql_result($result,$i,"download");$upload=mysql_result($result,$i,"upload");$latency=mysql_result($result,$i,"latency");$jitter=mysql_result($result,$i,"jitter");$testServer=mysql_result($result,$i,"testServer");$ip_address=mysql_result($result,$i,"ip_address");$hostname=mysql_result($result,$i,"hostname");$issuecreated=mysql_result($result,$i,"issuecreated");$id=mysql_result($result,$i,"ID");
    ?><tr>          <td><a href='updatecomputer.php?compname=<?php echo $ComputerName;?>' target='_self'><?php echo $ComputerName?></a></td>          <td><?php echo $ExpectedRig?></td>          <td><?php echo $Timestamp?></td>          <td><?php echo $download?></td>          <td><?php echo $upload?></td>          <td><?php echo $latency?></td>          <td><?php echo $jitter?></td>          <td><?php echo $testServer?></td>          <td><?php echo $ip_address?></td>          <td><?php echo $hostname?></td>          <td><?php echo $issuecreated?></td>          <td><form action="massreview.php" method="post" align="center" target="_blank">                <input type="checkbox" name="check_list[]" value="<?php echo $id;?>">    <input type="submit" /></form></td>          <td><form action="datareviewed.php?id=<?php echo $id;?>" method="post" name="form6" target="_self" id="form6"><input type="submit" style="background-color:#F7981B;color:#000000" name="datareviewed" id="datareviewed" value="Mark Reviewed" />    </form><form action="dataresolved.php?id=<?php echo $id;?>" method="post" name="form6" target="_self" id="form6"><input type="submit" style="background-color:#00FF00;color:#000000" name="dataresolved" id="dataresolved" value="Mark Resolved" />  </form>  </td></tr>
    <?php  $i++;}// end whileecho "</table>";
    massreview.php:

    PHP Code:
    <title>mass review test</title><?phpif(!empty($_POST['check_list'])) {    foreach($_POST['check_list'] as $check) {            echo $check;    }}?>

  2. #2
    Join Date
    Mar 2014
    Location
    Corpus Christi, TX
    Posts
    91
    I have learned that the way I have this set up will not work. Only one button per form, which means the table needs to be encompassed in the form.

    I have removed the two buttons located at the end of my table, and made the table the form. I have also changed from checkboxes to two radio buttons with one main submit bottom at the end of the form.

    The problem I am having is sending the information to the page to execute the update. I need to know if Reviewed or Resolved was selected for any line item in my table.

    Where am I going wrong?

    Code:
     
    <form id="statusupdate" name="statusupdate" method="post" align="center" action="massreview.php">  
    <table class="inet" border="1" cellspacing="0" cellpadding="3" align="center"><tr><td><strong>computerName</strong></td><td><strong>ExpectedRig</strong></td><td><strong>timestamp</strong></td><td><strong>download</strong></td><td><strong>upload</strong></td><td><strong>latency</strong></td><td><strong>jitter</strong></td><td><strong>testServer</strong></td><td><strong>ip_address</strong></td><td><strong>hostname</strong></td><td><strong>Issue Created</strong></td><td><strong>Update Status</strong></td><?phpmysql_connect('localhost','root','admin'); $query=("SELECT * FROM speedtest.data WHERE issuecreated = 'New' ORDER by ID DESC");$result=mysql_query($query);$num=mysql_numrows($result);$i=0; while ($i < $num) {
    $ComputerName=mysql_result($result,$i,"ComputerName");$ExpectedRig=mysql_result($result,$i,"ExpectedRig");$Timestamp=mysql_result($result,$i,"Timestamp");$download=mysql_result($result,$i,"download");$upload=mysql_result($result,$i,"upload");$latency=mysql_result($result,$i,"latency");$jitter=mysql_result($result,$i,"jitter");$testServer=mysql_result($result,$i,"testServer");$ip_address=mysql_result($result,$i,"ip_address");$hostname=mysql_result($result,$i,"hostname");$issuecreated=mysql_result($result,$i,"issuecreated");$id=mysql_result($result,$i,"ID");
    ?><tr>          <td><a href='updatecomputer.php?compname=<?php echo $ComputerName;?>' target='_self'><?php echo $ComputerName; ?></a></td>          <td><?php echo $ExpectedRig; ?></td>          <td><?php echo $Timestamp; ?></td>          <td><?php echo $download; ?></td>          <td><?php echo $upload; ?></td>          <td><?php echo $latency; ?></td>          <td><?php echo $jitter; ?></td>          <td><?php echo $testServer; ?></td>          <td><?php echo $ip_address; ?></td>          <td><?php echo $hostname; ?></td>          <td><?php echo $issuecreated; ?></td>          <td><label>  <input type="radio" name="update <?php echo $id; ?>" id="<?php echo $id; ?>" value="Reviewed" />    Reviewed     <input type="radio" name="update <?php echo $id; ?>" id="<?php echo $id; ?>" value="Resolved" />    Resolved              </label></td>  </tr>
    <?php  $i++;}// end whileecho "</table>"; ?>
    When I try and pull the information from POST, nothing copies over because it is looking for the name of the radio button to be a static value, and I need the value to be a variable.

    Thoughts?

  3. #3
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    22,288
    It's hard to read that code without line breaks (maybe a copy/paste issue between your code editor and the browser here?). Any chance you could re-post that within [php]...[/php] tags and making sure it has proper line breaks (and hopefully indentation)?
    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

  4. #4
    Join Date
    Dec 2002
    Location
    Seattle, WA
    Posts
    1,843
    You have a form element wrapping each input[type="checkbox"] instead of wrapping all the input elements.
    This is why you are getting only one item (in $_POST['check_list']) on the server-side.

    Example:
    PHP Code:
    <?php
    // dbms initialization...
    if (($result mysqli_query($link'SELECT * FROM table')) !== false) {
        echo 
    '<form action="./" method="post">',
            
    '<table>';
        while (
    $row mysqli_fetch_array($resultMYSQLI_ASSOC)) {
            echo 
    '<tr>',
                
    '<td><input type="checkbox" value="' .$row['id']. '"><td>',
                
    '<td>' .$row['name']. '</td>',
                
    '</tr>';
        }
        
    mysqli_free_result($result);
        echo 
    '</table>',
            
    '<div><button type="submit">Submit</button></div>',
            
    '</form>';
    }
    ?>
    Last edited by ShrineDesigns; 04-26-2014 at 09:08 PM.

  5. #5
    Join Date
    Mar 2014
    Location
    Corpus Christi, TX
    Posts
    91
    Here is some cleaner looking code for my php page with the radio buttons. I'm trying to allow a user to select "Reviewed" or "Resolved" and mass update. I tried using the code blocks, but it screwed up my line breaks.

    <form id="statusupdate" name="statusupdate" method="post" align="center" action="massreview.php">



    <table class="inet" border="1" cellspacing="0" cellpadding="3" align="center">
    <tr>
    <td><strong>
    computerName</strong></td>
    <td><strong>
    ExpectedRig</strong></td>
    <td><strong>
    timestamp</strong></td>
    <td><strong>
    download</strong></td>
    <td><strong>
    upload</strong></td>
    <td><strong>
    latency</strong></td>
    <td><strong>
    jitter</strong></td>
    <td><strong>
    testServer</strong></td>
    <td><strong>
    ip_address</strong></td>
    <td><strong>
    hostname</strong></td>
    <td><strong>
    Issue Created</strong></td>
    <td><strong>
    Update Status</strong></td>
    <?php
    mysql_connect('localhost','root','admin');
    $query=("SELECT * FROM speedtest.data WHERE issuecreated = 'New' ORDER by ID DESC");
    $result=mysql_query($query);
    $num=mysql_numrows($result);
    $i=0;
    while ($i < $num) {


    $ComputerName=mysql_result($result,$i,"ComputerName");
    $ExpectedRig=mysql_result($result,$i,"ExpectedRig");
    $Timestamp=mysql_result($result,$i,"Timestamp");
    $download=mysql_result($result,$i,"download");
    $upload=mysql_result($result,$i,"upload");
    $latency=mysql_result($result,$i,"latency");
    $jitter=mysql_result($result,$i,"jitter");
    $testServer=mysql_result($result,$i,"testServer");
    $ip_address=mysql_result($result,$i,"ip_address");
    $hostname=mysql_result($result,$i,"hostname");
    $issuecreated=mysql_result($result,$i,"issuecreated");
    $id=mysql_result($result,$i,"ID");


    ?>
    <tr>
    <td><a href='updatecomputer.php?compname=<?php echo $ComputerName;?>' target='_self'><?php echo $ComputerName; ?></a></td>
    <td><?php echo $ExpectedRig; ?></td>
    <td><?php echo $Timestamp; ?></td>
    <td><?php echo $download; ?></td>
    <td><?php echo $upload; ?></td>
    <td><?php echo $latency; ?></td>
    <td><?php echo $jitter; ?></td>
    <td><?php echo $testServer; ?></td>
    <td><?php echo $ip_address; ?></td>
    <td><?php echo $hostname; ?></td>
    <td><?php echo $issuecreated; ?></td>
    <td><label>
    <input type="radio" name="update" id="<?php echo $id; ?>" value="Reviewed<?php echo $id ?>" />
    Reviewed
    <input type="radio" name="update" id="<?php echo $id; ?>" value="Resolved<?php echo $id?>" />
    Resolved

    </label></td>

  6. #6
    Join Date
    Mar 2005
    Location
    Behind you...
    Posts
    1,159
    I notice a difference in your original updated code (your second post) and this most recent one without the [ CODE] blocks around it. Intially your radio inputs use name="update <?php echo $id; ?>" but this plain text code uses name="update" id="<?php echo $id; ?>".

    I was going to note that since you had a space in the 'name' attribute, it was likely causing some problems. Though since that seems to be updated I'm not sure I can say what problems you would still be having.

    As far as having a dynamic field name that includes this $id variable from PHP, not having a space in the 'name' attribute should be just fine
    PHP Code:
    <input type="radio" name="update<?php echo $id?>" value="Reviewed<?php echo $id ?>" />
    When you check this in PHP you'll have to do 1 of 2 things though. You can either add a type="hidden" input field in the form that holds the ID value, and then use this ID value to dynamically check your radio inputs. Or you can simply loop through the $_POST object in PHP and load them this way.
    "Given billions of tries, could a spilled bottle of ink ever fall into the words of Shakespeare?"

  7. #7
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    22,288
    Also note that you can use square braces to create array-style names for your form elements, which then become sub-arrays within $_GET or $_POST, allowing you to loop on them like any other PHP array, e.g.:
    PHP Code:
    <input type="checkbox" name="update[<?php echo $id?>]" value="Reviewid" />
    Then in your PHP form-handler code, you could do something like:
    PHP Code:
    foreach($_POST['update'] as $id => $value) {
        
    // do something to review ID $id based on $value...

    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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

"

"

X vBulletin 4.2.2 Debug Information

  • Page Generation 0.19141 seconds
  • Memory Usage 2,953KB
  • Queries Executed 13 (?)
More Information
Template Usage (34):
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_global_above_footer
  • (1)ad_global_below_navbar
  • (1)ad_global_header1
  • (1)ad_global_header2
  • (1)ad_navbar_below
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)ad_thread_first_post_content
  • (1)ad_thread_last_post_content
  • (1)bbcode_code
  • (6)bbcode_php
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (7)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (7)postbit
  • (7)postbit_onlinestatus
  • (7)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available (6):
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files (26):
  • ./showthread.php
  • ./global.php
  • ./includes/class_bootstrap.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/functions_navigation.php
  • ./includes/class_friendly_url.php
  • ./includes/class_hook.php
  • ./includes/class_bootstrap_framework.php
  • ./vb/vb.php
  • ./vb/phrase.php
  • ./includes/functions_facebook.php
  • ./includes/functions_calendar.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_notice.php
  • ./packages/vbattach/attach.php
  • ./vb/types.php
  • ./vb/cache.php
  • ./vb/cache/db.php
  • ./vb/cache/observer/db.php
  • ./vb/cache/observer.php 

Hooks Called (70):
  • init_startup
  • friendlyurl_resolve_class
  • init_startup_session_setup_start
  • database_pre_fetch_array
  • database_post_fetch_array
  • init_startup_session_setup_complete
  • global_bootstrap_init_start
  • global_bootstrap_init_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • load_show_variables
  • load_forum_show_variables
  • global_state_check
  • global_bootstrap_complete
  • global_start
  • style_fetch
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • strip_bbcode
  • friendlyurl_clean_fragment
  • friendlyurl_geturl
  • forumjump
  • cache_templates
  • cache_templates_process
  • template_register_var
  • template_render_output
  • fetch_template_start
  • fetch_template_complete
  • parse_templates
  • fetch_musername
  • notices_check_start
  • notices_noticebit
  • process_templates_complete
  • friendlyurl_redirect_canonical
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • memberaction_dropdown
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • build_navigation_data
  • build_navigation_array
  • check_navigation_permission
  • process_navigation_links_start
  • process_navigation_links_complete
  • set_navigation_menu_element
  • build_navigation_menudata
  • build_navigation_listdata
  • build_navigation_list
  • set_navigation_tab_main
  • set_navigation_tab_fallback
  • navigation_tab_complete
  • fb_like_button
  • showthread_complete
  • page_templates