Jump to content

Checkbox Problem


ulisses

Recommended Posts

hi guys, i need your help again.I have 2 php pages one called switch_services.php and another called subscribe.phpUsers have 3 options for services and they choose which they want to subscribe to. They can subscribe to 1, 2 or all services.I have used checkboxes and is working correctly, but if the user doesn't fill one box, i have a notice like this:"Notice: Undefined index: b in C:\Programas\EasyPHP 2.0b1\faf\subscribe.php on line 2"I'll show extracts of the codes bellow, to help u understand my problem and if there's a way to avoid that notice, please tell me. I appreciate-The "switch_service.php" page:<html><body><form action="subscribe.php" method="post">Amore:<input type="checkbox" name="a" value=1 /><br />Horoscope:<input type="checkbox" name="b" value=2 /><br />Sport:<input type="checkbox" name="c" value=3 /><input type="submit" name="submit" value="submit"/></form></body></html>-The "subscribe.php" page:<?php$x = $_POST['a'] . $_POST['b'] . $_POST['c'];switch ($x){case 1: mysql_query("INSERT INTO service ... break;case 2: mysql_query("INSERT INTO service ... break;case 3: mysql_query("INSERT INTO service ... break;case 12: mysql_query("INSERT INTO service ... break;case 13: mysql_query("INSERT INTO service ... break;case 23: mysql_query("INSERT INTO service ... break;case 123: mysql_query("INSERT INTO service ... break;}?>the insertion part is working correctly, but if i fill for examples the boxes for "Amore" and "Horoscope" it gives me this notice:Notice: Undefined index: c in C:\Programas\EasyPHP 2.0b1\faf\subscribe.php on line 2thanks in advance

Link to comment
Share on other sites

Data not posted by a form does not get placed into the $_POST array. ALWAYS use isset() or empty() to verify that post/get variables have been set BEFORE you try to access them. This means using statements like this:if (isset($_POST['a'])) {//do something}Also, your variables may get cast from one type to another automatically, but it's best practice to keep them the same if you can. That means putting your switch/case values in 'quotes', since you are dealing with strings, not numbers.

Link to comment
Share on other sites

To clarify, if you have a checkbox that is not checked, it doesn't get submitted with the form data at all. Only checked boxes get submitted.
What do you mean with not checked. Do you mean that if i don't fill a box it will always give that notice?
Link to comment
Share on other sites

I couldn't make the isset() function run. It still giving the same notification. So, I decided to use (E_ALL ^ E_NOTICE) and it's running as I want. But I'm asking to myself if is this the more appropriate solution. Which bennefits or disadvantages could it bring?

Link to comment
Share on other sites

here is the code i have now:<?php$x = $_POST['a'] . $_POST['b'] . $_POST['c'];switch ($x){case 1:if(isset($_POST['a'])){mysql_query("INSERT INTO service ...}break;case 2:if(isset($_POST['b'])){mysql_query("INSERT INTO service ...}break;case 3:if(isset($_POST['c'])){mysql_query("INSERT INTO service ...}break;......}?> and so on, it keeps giving me the notification. But let me take this time to give thanks for your help!

Link to comment
Share on other sites

Well, i found a way to avoid the notifications and now i'm declaring the variable $x like this:$x = @$_POST['a'] . @$_POST['b'] . @$_POST['c'];it stopped giving me that notice.

Link to comment
Share on other sites

Just so you'll know, the @ prefix suppresses errors. It is convenient in some applications, but generally leads to sloppy programming.Since you seem to be at your wit's end, I'll help a bit more:

$x = "";if (isset($_POST['a'])) {	 $x .= $_POST['a']; }if (isset($_POST['b'])) {	 $x .= $_POST['b']; }if (isset($_POST['c'])) {	 $x .= $_POST['c']; }

There is probably a more elegant solution, but this is easy to understand and should get the job done.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...