A community in which webmasters can ask for help with topics such as PHP coding , MySQL , IT jobs, web design, IT security.
Current location:homephp forumphp talk in 2009 yearPHP form checkbox and undefined index - page 1
User InfoPosts
PHP form checkbox and undefined index#1
I am getting an "Undefined index" error when submitting a form with an un-checked checkbox. Is there any other way besides running an "isset" or "empty" check on each individual posted value?

I have looked at this Question and am having trouble believing that this is the only solution.

Below is some example code:
EDIT: please not that these are not the actual names of the tables columns; they are named uniquely (like "postAddress, displayPhone, student, etc.)

posted date: 2009-04-13 21:00:00


Re: PHP form checkbox and undefined index#2
I had made out the solution of this problem. click to view my topic...

hope that hepls.

posted date: 2009-04-13 21:00:01


Re: PHP form checkbox and undefined index#3
try the following<?php//first part of the query$query = "UPDATE table SET ";$howManyCheckboxes = 5;//for every checkbox , see if it exist , if it is, add it to the queryfor($i=1;$i<=$howManyCheckboxes;$i++){ if(isset($_POST[(checkbox(.$i])) { $query = $query . " checkbox".$i."=(".escape($_POST[(checkbox(.$i])."(,"; }}//lets remove the last coma$query = substr($query,0,-1);//add the rest of the query$query = $query . " LIMIT 1";$result = mysql_query( $query );if( ! $result ) echo "um, not everything went as expected.";?>

posted date: 2009-04-13 21:05:00


Re: PHP form checkbox and undefined index#4
You could write a function that checks whether a checkbox was checked:function checkbox_value($name) { return (isset($_POST[$name]) ? 1 : 0);}Now call that function in your query like this:$sql = (UPDATE table SET (. (checkbox1 = (. checkbox_value((checkbox1() .(,(. (checkbox2 = (. checkbox_value((checkbox2() .(,(. (checkbox3 = (. checkbox_value((checkbox3() .(,(. (checkbox4 = (. checkbox_value((checkbox4() .(,(. (checkbox5 = (. checkbox_value((checkbox5() .(,(. "LIMIT 1";

posted date: 2009-04-13 21:10:00


Re: PHP form checkbox and undefined index#5
thanks for this, but the checkbox names are not always going to be "checkbox1","checkbox2"...

posted date: 2009-04-13 21:13:00


Re: PHP form checkbox and undefined index#6
Yeah! I like the way you think. Thank you!

posted date: 2009-04-13 21:18:00


Re: PHP form checkbox and undefined index#7
If you want a on/off checkbox you can write a hidden value before you write the checkbox.<input type="hidden" name="checkbox1" value="no" /><input type="checkbox" name="checkbox1" value="yes" />This will always return a value, either no (default unless checkbox is checked by default) or yes.You can validate input with the filter functions with FILTER_VALIDATE_BOOLEAN.Its easier if you write a function for this, like formCheckbox($name), with options for values (value (on( means checkbox is checked by default), attributes, etc.

posted date: 2009-04-13 21:23:00


Re: PHP form checkbox and undefined index#8
Does this actually work? That's crazy... never heard of that before. It would depend on the browser though? I'm sure not all browsers support this even if it's in the w3c spec?

posted date: 2009-04-13 21:30:00


Re: PHP form checkbox and undefined index#9
This works in all browsers I know about. The browsers return the names with values in order from first to last. In PHP only the last name will make it into GET/POST super globals (without name[]), and if the checkbox is not checked it will not be submitted.

posted date: 2009-04-13 21:43:00


select page: « 1 »
Copyright ©2008-2017 www.momige.com, all rights reserved.