PDA

Click to See Complete Forum and Search --> : [RESOLVED] how to check if at least one checkboxes is checked


jimot
Nov 28th, 2005, 07:15 PM
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:

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:


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:

Warning: Variable passed to each() is not an array or object in /home/www/dl.jimotman.com/index.php on line 100

visualAd
Nov 29th, 2005, 12:03 AM
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:

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:

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

jimot
Nov 29th, 2005, 05:02 AM
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!

visualAd
Nov 29th, 2005, 05:13 AM
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. :)

jimot
Nov 29th, 2005, 11:29 AM
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 :p