Results 1 to 5 of 5

Thread: [RESOLVED] how to check if at least one checkboxes is checked

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2001
    Posts
    237

    Resolved [RESOLVED] how to check if at least one checkboxes is checked

    hi all.. how can we check if at least one of the checkboxes in a form is checked?

    Here is the problem, I have a form with checkboxes. If at least one of the boxes is checked, then there's no problem after I press the submit button (delete), but if no boxes are checked, then it gives me an error.

    here's my code in the index.php:
    PHP Code:
    print "<form action=delete.php><table>";

    while (list(
    $code$url$categori$description) = mysql_fetch_row($result)):
       print 
    "<tr><td><input type=checkbox name=item[$code] value=y>$description
       </td></tr>"
    ;
    endwhile;

    print 
    "<tr><td><input type=submit value=Delete></td></tr>";
    print 
    "</table></form>"
    and here's the code in delete.php:

    PHP Code:
    while (list($key$val) = each ($item)) :
       if (
    $val == "y"):
          
    $query "DELETE FROM items WHERE code = $key";
          
    $result mysql_query($query);
       endif;
    endwhile; 
    if none of the checkboxes is checked, then it gives this error:
    PHP Code:
    WarningVariable passed to each() is not an array or object in /home/www/dl.jimotman.com/index.php on line 100 

  2. #2
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: how to check if at least one checkboxes is checked

    If I were you, I would turn off register globals. Anyhow, to check that an item was selected you just need to check that it is an array:
    PHP Code:
    if (is_array($item)) {
        while (list(
    $key$val) = each ($item)) {
            if (
    $val == "y") {
                
    $query "DELETE FROM items WHERE code = $key";
                
    $result mysql_query($query);
            }
        }

    Also, you don't need print to output HTML:
    PHP Code:
    ?>
    <form action="delete.php">
        <table>
            <? php while (list($code, $url, $categori, $description) = mysql_fetch_row($result)): ?>
            <tr>
                <td><input type="checkbox" name="item[$code]" value="y"><?php print($description?></td>
            </tr>
            <?php endwhile; ?>
            <tr>
                <td><input type="submit" value="Delete" /></td>
            </tr>
        </table>
    </form>
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    May 2001
    Posts
    237

    thanks

    thank you very much visualAd, it works fine now.. but I have a question, why do you prefer turn off register globals? sorry if it's newbie question, coz I'm learning the language..

    and about the print statement, I don't really like to type "?>" and "<?" often, so I used the print statement.

    thanks again!

  4. #4
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: [RESOLVED] how to check if at least one checkboxes is checked

    Firstly, register globals makes your scripts more vulnerable to hijacking, as PHP will overwrite any variable in the global name space with those supplied by the user. If this is done before after cookie or session variables a registered, then this can lead to security holes in your application.

    Even worse, some versions of PHP are open to complete GLOBALS overwrite, in which an attack can overwrite the entire global name space by sending a form variable with the name GLOBALS[test] for example. Have a look at the below link for a more detailed explanation.

    http://www.hardened-php.net/advisory_202005.79.html
    http://www.hardened-php.net/globals-problem

    Secondly, it is best not to output HTML using print or echo statements, because it gives the
    • PHP interpreter and thus the server un-necessary processing
    • Even more unnecessary processing is required if you put the strings inside double quotes because PHP needs to check for an parse any embedded variables.
    • The resulting HTML output is messy and hard to debug.
    • The script is messy and hard to read, and any syntax errors in the HTML will not be highlighted by most syntax highlighters.
    • Lastly, PHP is an HTML Embedded scripting language. If you want to use print to output all the HTML, then you may as well learn perl, as it is both faster, more powerful and, more secure and more efficient.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    May 2001
    Posts
    237

    Re: [RESOLVED] how to check if at least one checkboxes is checked

    oh..so..thank you very much visualAd for your explanation. I understand now and I'll use print as little as I can from now on. thanks

Posting Permissions

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



Click Here to Expand Forum to Full Width