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 16
  1. #1
    New Coder
    Join Date
    Aug 2007
    Posts
    16
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Multiple Checkboxes

    Hello

    I hope someone can give me some advice on the following code.

    The script is called register_ops.php. The script is nothing more than a register to assign the date that soemone is in work within a database table. The script display's a list of names with a checkbox next to each. The user can then check the checkbox(s) (to confirm that someone is in work that day) and then submit the entire form which then updates each record that has been checked with the current date. I have stripped out all of the mysql connect "type stuff" to keep the code shorter and everything else works fine except that I cannot get the table column to update those records that have been checked.

    Currently the table from which the list of names is generated and the column updated (to record the date) is the same. Called opsv10.

    If anyone can give me some help, I would really appreciate it as I have been trying to work this one out for the last 5 days!


    <?php

    if (isset($_POST['submitted'])) {

    foreach ($_POST as $key) {
    $id=$key[ops_id];

    $query = "UPDATE opsv10 SET on_site= NOW() where ops_id = $id";

    }

    } else {

    // Number of records to show per page:
    $display = 10;

    // Determine how many pages there are.
    if (isset($_GET['np'])) { // Already been determined.

    $num_pages = $_GET['np'];

    } else { // Need to determine.

    $c = addslashes($_SESSION['company']);

    // Count the number of records
    $query = "SELECT COUNT(*) AS Num FROM opsv10 WHERE company = '$c'";
    $result = mysql_query ($query);
    $row = mysql_fetch_array ($result, MYSQL_NUM);
    $num_records = $row[0];

    // Calculate the number of pages.
    if ($num_records > $display) { // More than 1 page.
    $num_pages = ceil ($num_records/$display);
    } else {
    $num_pages = 1;
    }

    } // End of np IF.

    // Determine where in the database to start returning results.
    if (isset($_GET['s'])) {
    $start = $_GET['s'];
    } else {
    $start = 0;
    }

    // Make the query.
    $query = "SELECT ops_id, first_name, last_name, position FROM opsv10 WHERE company = '$c' ORDER BY last_name ASC LIMIT $start, $display";
    $result = mysql_query ($query); // Run the query.
    ?>



    <div id="contractor_table_holder">

    <form action="register_ops.php" method="post">
    <?php
    // Table header.
    echo '
    <table align="center" cellspacing="0" cellpadding="8" bgcolor="">
    <tr>
    <td align="center"><font face="Geneva, Arial, Helvetica, sans-serif, Century Gothic" size="2"><b></b></font></td>
    <td align="center"><img src="program_images/spacer_gif.gif" border="0" /></td>
    <td align="center"><font face="Geneva, Arial, Helvetica, sans-serif, Century Gothic" size="2"><b>Name</b></font></td>
    <td align="center"><font face="Geneva, Arial, Helvetica, sans-serif, Century Gothic" size="2"><b>Position</b></font></td>
    </tr>
    ';
    // Fetch and print all the records.
    $bg = ''; // Set the background color.
    while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
    $id = $row['ops_id'];
    $bg = ($bg=='' ? '' : ''); // Switch the background color.
    echo '<tr bgcolor="' . $bg . '">
    <td style="border-top: 1px solid #000000;" align="center"><input name="checkbox[]" type="checkbox" value="$id" /></td>
    <td style="border-top: 1px solid #000000;" align="center"><img src="program_images/spacer_gif.gif" border="0" /></td>
    <td style="border-top: 1px solid #000000;" align="left"><font face="Geneva, Arial, Helvetica, sans-serif, Century Gothic" size="2" color="#FFFFFF"><strong>' . $row['first_name'] . ' ' . $row['last_name'] . '</strong></font></td>
    <td style="border-top: 1px solid #000000;" align="left"><font face="Geneva, Arial, Helvetica, sans-serif, Century Gothic" size="2" color="#FFFFFF"><strong>' . $row['position'] . '</strong></font></td>
    <td><td>

    </tr>
    ';
    }

    echo '</table>';
    ?>
    <input name="submit" type="button" value="send"/>
    <input type="hidden" name="submitted" value="TRUE" />
    <input type="hidden" name =".$id.[ops_id]" value =".$id.">
    </form>



    </div>
    <?php

    mysql_free_result ($result); // Free up the resources.
    mysql_close(); // Close the database connection.
    ?>


    <div id="cont_list_pages">
    <?php
    // Make the links to other pages, if necessary.
    if ($num_pages > 1) {

    echo '<p>';
    // Determine what page the script is on.
    $current_page = ($start/$display) + 1;

    // If it's not the first page, make a Previous button.
    if ($current_page != 1) {
    echo '<a href="register_ops.php?s=' . ($start - $display) . '&np=' . $num_pages . '"><font face="Geneva, Arial, Helvetica, sans-serif, Century Gothic" size="2"><<<&nbsp;&nbsp;</font></a> ';
    }

    // Make all the numbered pages.
    for ($i = 1; $i <= $num_pages; $i++) {
    if ($i != $current_page) {
    echo '<font face="Geneva, Arial, Helvetica, sans-serif, Century Gothic" size="2"><a href="register_ops.php?s=' . (($display * ($i - 1))) . '&np=' . $num_pages . '"><font face="Geneva, Arial, Helvetica, sans-serif, Century Gothic" size="2">' . $i . '</font></a> ';
    } else {
    echo $i . ' ';
    }
    }

    // If it's not the last page, make a Next button.
    if ($current_page != $num_pages) {
    echo '<a href="register_ops.php?s=' . ($start + $display) . '&np=' . $num_pages . '"><font face="Geneva, Arial, Helvetica, sans-serif, Century Gothic" size="2">&nbsp;&nbsp;>>></a></font>';
    }

    echo '</p>';
    }
    } // End of links section.
    ?>
    </div>
    </div>
    </div>
    </div>
    </div>
    <?php // Include the HTML footer.
    include ('./includes/footer.html');
    ?>
    Last edited by JBLC; 08-18-2007 at 10:50 PM. Reason: Spelling mistake in title.

  • #2
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,642
    Thanks
    2
    Thanked 405 Times in 397 Posts
    Change value="$id" to value="' . $id . '", then change the code to update it to this:
    PHP Code:
    foreach ($_POST['checkbox'] as $key => $value) {
    $query "UPDATE opsv10 SET on_site= NOW() where ops_id = $value"

    You could make this all one query if you want to by imploding the array with ' OR `ops_id` = ', or something similar.

  • #3
    New Coder
    Join Date
    Aug 2007
    Posts
    16
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Hello Inigoesdr

    Tried your changes but still no luck. It appears that the submit button is not "working". Could there be another error in the code?

    Thanks
    JC

  • #4
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,642
    Thanks
    2
    Thanked 405 Times in 397 Posts
    It's possible. I really didn't look that much. Edit your post and put the code inside of [php][/php] tags so we can see it formatted.


    Edit: Your submit button is called "submit" and the $_POST variable you're using is called "submitted", change the $_POST variable.
    Last edited by Inigoesdr; 08-19-2007 at 01:08 AM.

  • #5
    New Coder
    Join Date
    Aug 2007
    Posts
    16
    Thanks
    3
    Thanked 0 Times in 0 Posts
    OK code tags added.

    PHP Code:
    <?php 

    if (isset($_POST['submitted'])) { 

    foreach (
    $_POST as $key) { 
    $id=$key[ops_id]; 

    $query "UPDATE opsv10 SET on_site= NOW() where ops_id = $id"



    } else { 

    // Number of records to show per page: 
    $display 10

    // Determine how many pages there are. 
    if (isset($_GET['np'])) { // Already been determined. 

    $num_pages $_GET['np']; 

    } else { 
    // Need to determine. 

    $c addslashes($_SESSION['company']); 

    // Count the number of records 
    $query "SELECT COUNT(*) AS Num FROM opsv10 WHERE company = '$c'"
    $result mysql_query ($query); 
    $row mysql_fetch_array ($resultMYSQL_NUM); 
    $num_records $row[0]; 

    // Calculate the number of pages. 
    if ($num_records $display) { // More than 1 page. 
    $num_pages ceil ($num_records/$display); 
    } else { 
    $num_pages 1


    // End of np IF. 

    // Determine where in the database to start returning results. 
    if (isset($_GET['s'])) { 
    $start $_GET['s']; 
    } else { 
    $start 0


    // Make the query. 
    $query "SELECT ops_id, first_name, last_name, position FROM opsv10 WHERE company = '$c' ORDER BY last_name ASC LIMIT $start, $display"
    $result mysql_query ($query); // Run the query. 
    ?>

    <div id="contractor_table_holder">

    <form action="register_ops.php" method="post">
    PHP Code:
    <?php 
    // Table header. 
    echo 
    <table align="center" cellspacing="0" cellpadding="8" bgcolor=""> 
    <tr> 
    <td align="center"><font face="Geneva, Arial, Helvetica, sans-serif, Century Gothic" size="2"><b></b></font></td> 
    <td align="center"><img src="program_images/spacer_gif.gif" border="0" /></td> 
    <td align="center"><font face="Geneva, Arial, Helvetica, sans-serif, Century Gothic" size="2"><b>Name</b></font></td> 
    <td align="center"><font face="Geneva, Arial, Helvetica, sans-serif, Century Gothic" size="2"><b>Position</b></font></td> 
    </tr> 
    '

    // Fetch and print all the records. 
    $bg ''// Set the background color. 
    while ($row mysql_fetch_array($resultMYSQL_ASSOC)) { 
    $id $row['ops_id']; 
    $bg = ($bg=='' '' ''); // Switch the background color. 
    echo '<tr bgcolor="' $bg '"> 
    <td style="border-top: 1px solid #000000;" align="center"><input name="checkbox[]" type="checkbox" value="$id" /></td> 
    <td style="border-top: 1px solid #000000;" align="center"><img src="program_images/spacer_gif.gif" border="0" /></td> 
    <td style="border-top: 1px solid #000000;" align="left"><font face="Geneva, Arial, Helvetica, sans-serif, Century Gothic" size="2" color="#FFFFFF"><strong>' 
    $row['first_name'] . ' ' $row['last_name'] . '</strong></font></td> 
    <td style="border-top: 1px solid #000000;" align="left"><font face="Geneva, Arial, Helvetica, sans-serif, Century Gothic" size="2" color="#FFFFFF"><strong>' 
    $row['position'] . '</strong></font></td> 
    <td><td> 

    </tr> 
    '



    echo 
    '</table>'
    ?>
    <input name="submit" type="button" value="send"/>
    <input type="hidden" name="submitted" value="TRUE" />
    <input type="hidden" name =".$id.[ops_id]" value =".$id.">
    </form>



    </div>
    PHP Code:
    <?php 

    mysql_free_result 
    ($result); // Free up the resources. 
    mysql_close(); // Close the database connection. 
    ?> [/CODE]

    <div id="cont_list_pages"> 
    [CODE]<?php 
    // Make the links to other pages, if necessary. 
    if ($num_pages 1) { 

    echo 
    '<p>'
    // Determine what page the script is on. 
    $current_page = ($start/$display) + 1

    // If it's not the first page, make a Previous button. 
    if ($current_page != 1) { 
    echo 
    '<a href="register_ops.php?s=' . ($start $display) . '&np=' $num_pages '"><font face="Geneva, Arial, Helvetica, sans-serif, Century Gothic" size="2"><<<&nbsp;&nbsp;</font></a> '


    // Make all the numbered pages. 
    for ($i 1$i <= $num_pages$i++) { 
    if (
    $i != $current_page) { 
    echo 
    '<font face="Geneva, Arial, Helvetica, sans-serif, Century Gothic" size="2"><a href="register_ops.php?s=' . (($display * ($i 1))) . '&np=' $num_pages '"><font face="Geneva, Arial, Helvetica, sans-serif, Century Gothic" size="2">' $i '</font></a> '
    } else { 
    echo 
    $i ' '



    // If it's not the last page, make a Next button. 
    if ($current_page != $num_pages) { 
    echo 
    '<a href="register_ops.php?s=' . ($start $display) . '&np=' $num_pages '"><font face="Geneva, Arial, Helvetica, sans-serif, Century Gothic" size="2">&nbsp;&nbsp;>>></a></font>'


    echo 
    '</p>'

    // End of links section. 
    ?>
    </div>
    </div>
    </div>
    </div>
    </div>
    PHP Code:
    <?php // Include the HTML footer. 
    include ('./includes/footer.html'); 
    ?>
    Last edited by JBLC; 08-19-2007 at 01:11 AM.

  • #6
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,642
    Thanks
    2
    Thanked 405 Times in 397 Posts
    Please re-read my previous post, and the sticky threads for this forum.

  • #7
    New Coder
    Join Date
    Aug 2007
    Posts
    16
    Thanks
    3
    Thanked 0 Times in 0 Posts
    I have corrected the post - sorry but I doubled up on the [code] and [php]. It makes some sense now.

  • #8
    New Coder
    Join Date
    Aug 2007
    Posts
    16
    Thanks
    3
    Thanked 0 Times in 0 Posts
    The $_POST submitted refers to the hidden input.

  • #9
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,642
    Thanks
    2
    Thanked 405 Times in 397 Posts
    You can put the [php][/php] tags around everything, including the HTML.

    Edit: You didn't make those changes I posted in my first post either
    Last edited by Inigoesdr; 08-19-2007 at 01:22 AM.

  • #10
    New Coder
    Join Date
    Aug 2007
    Posts
    16
    Thanks
    3
    Thanked 0 Times in 0 Posts
    OK, the original script with the changes you recommended.

    PHP Code:
    <?php    
         
        
    if (isset($_POST['submitted']))  {
       
       foreach (
    $_POST['checkbox'] as $key => $value) {
    $query "UPDATE opsv10 SET on_site= NOW() where ops_id = $value"

    } else { 
        
          
    // Number of records to show per page:
    $display 10;

    // Determine how many pages there are. 
    if (isset($_GET['np'])) { // Already been determined.

        
    $num_pages $_GET['np'];

    } else { 
    // Need to determine.

        
    $c addslashes($_SESSION['company']);

         
    // Count the number of records
        
    $query "SELECT COUNT(*) AS Num FROM opsv10 WHERE company = '$c'";
        
    $result mysql_query ($query);
        
    $row mysql_fetch_array ($resultMYSQL_NUM);
        
    $num_records $row[0];

        
    // Calculate the number of pages.
        
    if ($num_records $display) { // More than 1 page.
            
    $num_pages ceil ($num_records/$display);
        } else {
            
    $num_pages 1;
        }
        
    // End of np IF.

    // Determine where in the database to start returning results.
    if (isset($_GET['s'])) {
        
    $start $_GET['s'];
    } else {
        
    $start 0;
    }
            
    // Make the query.
    $query "SELECT ops_id, first_name, last_name, position FROM opsv10 WHERE company = '$c' ORDER BY last_name ASC LIMIT $start, $display";        
    $result mysql_query ($query); // Run the query.
    ?>



    <div id="contractor_table_holder">

    <form action="register_ops.php" method="post">
    <?php
    // Table header.
    echo '

    <table align="center" cellspacing="0" cellpadding="8" bgcolor="">
    <tr>
        <td align="center"><font face="Geneva, Arial, Helvetica, sans-serif, Century Gothic" size="2"><b></b></font></td>
        <td align="center"><img src="program_images/spacer_gif.gif" border="0" /></td>
        <td align="center"><font face="Geneva, Arial, Helvetica, sans-serif, Century Gothic" size="2"><b>Name</b></font></td>    
        <td align="center"><font face="Geneva, Arial, Helvetica, sans-serif, Century Gothic" size="2"><b>Position</b></font></td>
        </tr>
    '
    ;
    // Fetch and print all the records.
    $bg ''// Set the background color.
    while ($row mysql_fetch_array($resultMYSQL_ASSOC)) {
    $id $row['ops_id'];
        
    $bg = ($bg=='' '' ''); // Switch the background color.
        
    echo '<tr bgcolor="' $bg '">
            <td style="border-top: 1px solid #000000;" align="center"><input name="checkbox[]" type="checkbox" value="' 
    $id '" /></td>
            <td style="border-top: 1px solid #000000;" align="center"><img src="program_images/spacer_gif.gif" border="0" /></td>
            <td style="border-top: 1px solid #000000;" align="left"><font face="Geneva, Arial, Helvetica, sans-serif, Century Gothic" size="2" color="#FFFFFF"><strong>' 
    $row['first_name'] . ' ' $row['last_name'] . '</strong></font></td>
            <td style="border-top: 1px solid #000000;" align="left"><font face="Geneva, Arial, Helvetica, sans-serif, Century Gothic" size="2" color="#FFFFFF"><strong>' 
    $row['position'] . '</strong></font></td>
            <td><td>
            
            </tr>
        '
    ;
    }

    echo 
    '</table>';
    ?>
    <input name="submit" type="button" value="send" />
    //<input type="hidden" name="submitted" value="TRUE" />
    <input type="hidden" name ="<?php $id ?>" value ="<?php $id ?>" />
    </form>



    </div>
    <?php

    mysql_free_result 
    ($result); // Free up the resources.    
    mysql_close(); // Close the database connection.
    ?>


    <div id="cont_list_pages">
    <?php
    // Make the links to other pages, if necessary.
    if ($num_pages 1) {
        
        echo 
    '<p>';
        
    // Determine what page the script is on.    
        
    $current_page = ($start/$display) + 1;
        
        
    // If it's not the first page, make a Previous button.
        
    if ($current_page != 1) {
            echo 
    '<a href="register_ops.php?s=' . ($start $display) . '&np=' $num_pages '"><font face="Geneva, Arial, Helvetica, sans-serif, Century Gothic" size="2"><<<&nbsp;&nbsp;</font></a> ';
        }
        
        
    // Make all the numbered pages.
        
    for ($i 1$i <= $num_pages$i++) {
            if (
    $i != $current_page) {
                echo 
    '<font face="Geneva, Arial, Helvetica, sans-serif, Century Gothic" size="2"><a href="register_ops.php?s=' . (($display * ($i 1))) . '&np=' $num_pages '"><font face="Geneva, Arial, Helvetica, sans-serif, Century Gothic" size="2">' $i '</font></a> ';
            } else {
                echo 
    $i ' ';
            }
        }
        
        
    // If it's not the last page, make a Next button.
        
    if ($current_page != $num_pages) {
            echo 
    '<a href="register_ops.php?s=' . ($start $display) . '&np=' $num_pages '"><font face="Geneva, Arial, Helvetica, sans-serif, Century Gothic" size="2">&nbsp;&nbsp;>>></a></font>';
        }
        
        echo 
    '</p>';
    }    
    // End of links section.
    ?>
    </div>
    </div>
    </div>
    </div>
    </div>
    <?php // Include the HTML footer.
    include ('./includes/footer.html');
    ?>

  • #11
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,642
    Thanks
    2
    Thanked 405 Times in 397 Posts
    Add
    PHP Code:
    mysql_query($query) or die('Query: ' $query '<br />Error: ' mysql_error()); 
    after the $query at the top and post any errors you get.

  • Users who have thanked Inigoesdr for this post:

    JBLC (08-20-2007)

  • #12
    New Coder
    Join Date
    Aug 2007
    Posts
    16
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Inigoesdr

    Used the above code but no errors are reported. I think the problem is that the submit button is not posting anything. I tried changing submitted to submit (as you suggested earlier) but still nothing!

    There must be a more general error in the actual form as the button isn't sending - what I can't get my head around is the format of the script is no different to countless other forms I have done with no problems at all????

    JBLC

  • #13
    Senior Coder Len Whistler's Avatar
    Join Date
    Jul 2002
    Location
    Vancouver, BC Canada
    Posts
    1,323
    Thanks
    26
    Thanked 100 Times in 100 Posts
    PHP Code:
    <?php    
         
        
    if (isset($_POST['submitted']))  { 

    -------------------------------

    <
    form action="register_ops.php" method="post"

    ------------------------------

    <
    input name="submit" type="button" value="send" />
    //<input type="hidden" name="submitted" value="TRUE" />
    <input type="hidden" name ="<?php $id ?>" value ="<?php $id ?>" />
    </
    form>
    This is the code you have so far, one of the lines is commented out and it matches the syntax of if (isset($_POST['submitted'])). Use the common word of submit instead of submitted to keep it simple, make these changes in the register_ops.php file also.

    Also consider using <form method="post" action="<?php echo $PHP_SELF;?>"> instead of <form action="register_ops.php" method="post"> to do the processing. I find one page is better than using two pages to do the POST processing in most cases.
    Leonard Whistler

  • #14
    New Coder
    Join Date
    Aug 2007
    Posts
    16
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Inigoesdr

    Just to let you know, I have found the solution to the problem. The original query was changed to:

    PHP Code:
    $query 'UPDATE opsv10 SET on_site= NOW() where ops_id IN (' implode(','$_POST['checkbox']) . ')'
    and all works fine. Thank you very much for your help and please refer to the private message that I sent you.

    Regards
    JBLC

  • #15
    New Coder
    Join Date
    Aug 2007
    Posts
    16
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Lenoard

    Thanks for the input. The last post from Larry Ullman solved the problem.

    Appreciate your input though.
    JBLC (JC)


  •  
    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
    •