|
-
Nov 28th, 2005, 08:15 PM
#1
Thread Starter
Addicted Member
[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:
Warning: Variable passed to each() is not an array or object in /home/www/dl.jimotman.com/index.php on line 100
-
Nov 29th, 2005, 01:03 AM
#2
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>
-
Nov 29th, 2005, 06:02 AM
#3
Thread Starter
Addicted Member
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!
-
Nov 29th, 2005, 06:13 AM
#4
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.

-
Nov 29th, 2005, 12:29 PM
#5
Thread Starter
Addicted Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|