Forum Moderators: coopster

Message Too Old, No Replies

Undefined index error

error with checkbox during captcha integration of securimage

         

fubar2323

1:20 am on May 18, 2009 (gmt 0)

10+ Year Member



Hi there,

I'm currently trying to implement Securimage captcha into a webpage, I've got it working with my standard contact form perfectly. The issues start when I incorporate checkboxes and specifically when the checkboxes are not selected and the form is sent.

For every unselected checkbox i'm getting errors like these:

Notice: Undefined index: category1 in /home/content/p/r/o/prop/html/reviewform.php on line 18

Notice: Undefined index: category2 in /home/content/p/r/o/prop/html/reviewform.php on line 18

Notice: Undefined index: category3
in /home/content/p/r/o/prop/html/reviewform.php on line 18

From what I gather, I need to use some kind of if/else statement but i'm not sure what to do from here?

this is the code in my form page:


<img id="captcha" src="/testing/securimage/securimage_show.php" alt="CAPTCHA Image" />
<br />
<input type="text" name="captcha_code" size="10" maxlength="6" />
a href="#" onclick="document.getElementById('captcha').src = '/testing/securimage/securimage_show.php?' + Math.random(); return false">Reload Image</a>

and here's my process php page


<?php session_start(); ?>
<?php error_reporting(E_ALL); ini_set('display_errors', 'on'); ?>
<?php
include_once $_SERVER['DOCUMENT_ROOT'].'/securimage/securimage.php';
$securimage = new Securimage();
if ($securimage->check($_POST['captcha_code']) == false) {
die('The code you entered was incorrect. Go back and try again.');
}
?>
<?php
$to = "email@host.com" ;
$from = $_REQUEST['email'] ;
$name = $_REQUEST['name'] ;
$headers = "From: $from";
$subject = "Website - Form Data";
$fields = array();
$fields{"name"} = "name";
$fields{"number"} = "number";
$fields{"email"} = "email";
$fields{"category1"} = "category1";
$fields{"category2"} = "category2";
$fields{"category3"} = "category3";
$fields{"category4"} = "category4";
$fields{"message"} = "message";
$body = "The website received the following form submission:\n\n"; foreach($fields as $a => $b){ $body .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]); }
$headers2 = "From: noreply@host.com.au";
$subject2 = "Thanks for the message";
$autoreply = "Autoreply Message";
if($from == '') {print "You have not entered an email, please go back and try again";}
else {
if($name == '') {print "You have not entered a name, please go back and try again";}
else {
$send = mail($to, $subject, $body, $headers);
$send2 = mail($from, $subject2, $autoreply, $headers2);
if($send)
{header( "Location: form_return.php" );}
else
{print "We encountered an error sending your mail, please retry"; }
}
}

if anyone one could help it would be greatly appreciated.

fubar2323

1:38 am on May 18, 2009 (gmt 0)

10+ Year Member



problem solved...

i used my little nooob head and thunk it out.

dreamcatcher

5:48 am on May 19, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Glad you got sorted. As you probably found out, checkboxes are only a part of the post array if they are checked. So, you simply need to check for this before trying to access it using isset [uk2.php.net].

if (isset($_POST['category1 '])) {
// do something
}

dc