-1

I have this registration code. I want people to decide if they want to be added to an email listserv or not. The code executes appropriately when the box is checked but I get:

Notice: Undefined index: maillist in C:\xampp\htdocs\website\register.php on line 21

when the box is unchecked. Below is the code. I'm not great at programming so please pretend like I'm stupid when responding. Thanks.

    <tr><td>Add me to your listserve:</td><td><input type="checkbox" name="maillist">  </td></tr>

    $name=mysql_real_escape_string($_POST['name']);
    $dob=mysql_real_escape_string($_POST['dob']);
    $loginid=mysql_real_escape_string($_POST['loginid']);
    $password=mysql_real_escape_string($_POST['password']);
    $emailaddress=mysql_real_escape_string($_POST['emailaddress']);
    $description=mysql_real_escape_string($_POST['description']);
    $maillist=$_POST['maillist'];

    mysql_query("INSERT INTO demographics    values('$name','$dob','$loginid','$password','$description')") or DIE(mysql_error());

    if($maillist==true){
      mysql_query("INSERT INTO maillist values('$name','$emailaddress')") or DIE(mysql_error());
    }
Roopendra
  • 7,674
  • 16
  • 65
  • 92
  • If the checkbox is unchecked the no value will be sent through the form so you can do something like: `$maillist = isset($_POST['maillist']);` – Cyclonecode Dec 08 '13 at 04:13
  • Please [use the search](http://stackoverflow.com/search?q=%5Bphp%5D+Undefined+index) before asking a question. This problem has been answered several thousand times before. – Sverri M. Olsen Dec 08 '13 at 04:20

3 Answers3

3

HTML forms don't create a POST variable for unchecked checkboxes. You need to check if the variable exists to know if it's checked or not:

$maillist = isset($_POST['maillist']);
bchociej
  • 1,369
  • 9
  • 12
0

If your checkbox is unchecked the maillist will be not set. Check $_POST['maillist'] before using it whether is comes in post array.

$maillist=isset($_POST['maillist']) ? $_POST['maillist'] : '';
Roopendra
  • 7,674
  • 16
  • 65
  • 92
  • I see. Thanks for the help. Also, I tried to search for the answer but couldn't find it. I'll try harder next time. – user3079087 Dec 08 '13 at 19:34
0

You have to check if the variable is set:

<input type="checkbox" name="maillist" value= true>

if(isset($_REQUEST['maillist']) {
....
}
Fabrizio Mazzoni
  • 1,831
  • 2
  • 24
  • 46