0

I have a check box and two date fields(from,to). Date fields are disabled by default and gets enabled when checkbox is checked. If check box is checked, then there is no problem with POST and date values are obtained in posted page. But when check box is not checked, date fields are disable and on POSTING , I am getting following errors.

Notice: Undefined index: from
Notice: Undefined index: To
Notice: Undefined index: checked

How can I solve this issue?

user3589496
  • 35
  • 1
  • 6

2 Answers2

1

Check to see if the checkbox is checked. If so then you can use the other posted vars. If not then those vars are not set.

if(isset($_POST['checked'])) {
    //do your stuff
} else {
    //not checked do something else (from and to values don't exist)
}
AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
0

Well this is because $_POST['from'], $_POST['To'] and $_POST['checked'] is not set.

You need to use isset function to make sure that it exists.

Use:

if(isset($_POST['from']) && isset($_POST['To']) && isset($_POST['checked']) { // if all are required or your custom condition
   // do stuff
} else {
   // error handling
}
Guns
  • 2,678
  • 2
  • 23
  • 51
  • if `checked` isset then `from` and `to` will be set, but they may be empty. – AbraCadaver Apr 30 '14 at 17:40
  • @AbraCadaver, a very true statement, but wanted to show the OP to use an `isset()` function. Just an example. But thanks for the comment which make OP alert of not using it as is, but modifying as per his requirements! – Guns Apr 30 '14 at 17:42