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
    83

    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
    83
    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
    19,220
    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)?
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  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
    83
    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
    862
    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
    19,220
    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...

    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

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



Recent Articles