While its justa notice: undefined index. Which means PHP is telling you there are some variables you are trying to process with PHP but they were not sent with form.
When you are catching the variables with PHP but there are some $_POST variables which weren’t defined in
This error comes usually on localhost xampp, but not in online server, cause online servers switch off this feature in php to notice for undefined variables. But in localhost or xampp configuration this is not switched off so we see this error.
Handling Checkbox Undefined Index
Handling empty or undefined checkbox submission is a bit tricky. Unlike text or other fields if checkbox isn’t selected PHP form wouldn’t submit even empty value. So in that case its important to check if a checkbox is set or not. For text fields if you are sure they are being submitted you do not need to check cause they submit empty value at least. But for checkbox you have to check something like.
This is PHP’s inline if condition.
$_POST['wc_use_taxes'] = !isset($_POST['wc_use_taxes'])? "" : $_POST['wc_use_taxes'];
The code above would check if a variable wc_use_taxes wasn’t sent through the form it would set its default value empty and would define an empty variable to use in checkboxes.
Undefined Index
Undefined index actually means you have a variable or constant in your php file which is not defined or assigned any value means that is empty variable.
Suppose you are saving posted value from a form into a variable called user_email like this
$user_email = $_POST[‘user_email’];.
When you submit form you will not see this error cause that time our $user_email variable will hold a string which can be empty or having anything in it. But when you access page directly via url and by not submitting form then there is not way php can assign value to $user_email variable that’s why php will produce a notice for you saying this is an undefined index. I do not suggest switching off Notices via PHP configuration.
How to remove Notice: Undefined Index
As a php developer i faced this problem several times but i always used only 2 methods to handle these notices.
- Put your variable defining when its getting value from something else in if() statement. Like this
if(isset($_POST)) { $user_email = $_POST[‘user_email’]; }
This statement will only run if POST array is set if not then this will not run and will not produce any notice. As POST is also not set why not error? cause we are just checking it with isset function within if condition. - When i have several variables in 1 file and i want to switch off these notices for that file but not for other files then i put this in top of my php document.
[php]//to disable Notice errors on System. Notices removal.
error_reporting( error_reporting() & ~E_NOTICE );[/php]
This will not only disable undefined index notices but all other notices php want to give you as well.
What is PHP isset function?
Isset function in PHP returns true or false if the variable set or not. Its always a good practice to check if variables set or not before you process them.
if(!isset($myvariable)) {
$myvariable = ""; // Make it empty but defined.
}