Undefined index error
3 posts by 2 authors in: Forums > CMS Builder
Last Post: March 25, 2012 (RSS)
By nmsinc - March 24, 2012
I'm adding addtional code to the user-signup.php file for addtional fields that we have added to the 'accounts' section. Most of the these are check boxes. I have the input values set to "1" with the box "checked". When a user unchecks any box I get a undefined index error for that field upon submit. I do not want to turn-off error checking so I tried adding the isset code below to capture and set the vaule of unchecked box to 0 (zero) as the pass through without any luck. This code is located just above the mysql_query("INSERT INTO `{$TABLE_PREFIX}accounts` SET
// Check for null in checkboxes
if (!isset($_POST['view_map']))
{
$_POST['view_map'] = "0";
}
Suggestions anyone? - Thanks nmsinc
// Check for null in checkboxes
if (!isset($_POST['view_map']))
{
$_POST['view_map'] = "0";
}
Suggestions anyone? - Thanks nmsinc
nmsinc
Re: [nmsinc] Undefined index error
By Dave - March 25, 2012
Hi nmsinc,
Checkboxes are the only kind of html form field that don't submit anything at all if they're not checked. What I usually do is use a hidden field field like this:
<input type="hidden" name="view_map" value="0" />
<input type="checkbox" name="view_map" value="1" />
If you have two fields with the same name then the value of the last one is used. So if checked you'll get view_map=1 and with unchecked you'll get view_map=0
Another trick is to use @ which suppresses errors and warnings in php. So instead of $_REQUEST['view_map'] use @$_REQUEST['view_map']
Also, note that we use $_REQUEST, which has both $_GET and $_POST in it, which is why changing $_POST didn't make a difference.
Hope that helps, either of the above two solutions should fix it. Let me know if you have any other problems.
Checkboxes are the only kind of html form field that don't submit anything at all if they're not checked. What I usually do is use a hidden field field like this:
<input type="hidden" name="view_map" value="0" />
<input type="checkbox" name="view_map" value="1" />
If you have two fields with the same name then the value of the last one is used. So if checked you'll get view_map=1 and with unchecked you'll get view_map=0
Another trick is to use @ which suppresses errors and warnings in php. So instead of $_REQUEST['view_map'] use @$_REQUEST['view_map']
Also, note that we use $_REQUEST, which has both $_GET and $_POST in it, which is why changing $_POST didn't make a difference.
Hope that helps, either of the above two solutions should fix it. Let me know if you have any other problems.
Dave Edis - Senior Developer
interactivetools.com
interactivetools.com
Re: [Dave] Undefined index error
By nmsinc - March 25, 2012
The hidden fields worked perfect - thanks Dave!
nmsinc