That's pretty cool, PHP is telling you that you have a empty variable lying down, this may be that you have the variable to take value from a submitted form and had not yet done so. There are lots of solutions online, but I found the solution at Notesofgenius.com very quickly, which suggest that two solution:
(1) One simple answer – isset() !
isset() function in PHP determines whether a variable is set and is not NULL. It returns a Boolean value, that is, if the variable is set it will return true and if the variable value is null it will return false. More details on this function can be found in PHP Manual.The above is good but can be time consuming as you may likely repeat it for all variable (in case you have something like 200 variable in the page), although the isset() function can take multiple parameter ($variables) like this: isset($var1, $var2, var3)
You can view the example used here as will not give any
(2) We can just ignore it by hiding the notices like this.
error_reporting(E_ALL ^ E_NOTICE);This is not totally advisable as you will not see further NOTICE.
The solution I later discover is to create function like this:
function check_var($var){What the function does is that; if you call the function passing your variable to it, will run the isset() on any variable pass in the function call, so you don’t have to do if(isset(….)) all over again for all your variables. In the past I do use variable like this:
if(isset($var)){}
return $var;
}
$name = $_POST[name];But now I fashion it:
$name = check_var($_POST[name]);You can include() the file that contain the check_var() function in any php file on your site
Login Example
$user_email = $_POST["userEmail"];Using isset
$user_pass = POST["userPass"];
If(isset($user_email, $user_pass )) {// the isset() function can take multiple parameterMY SOLUTION
$user_email = $_POST["userEmail"];
$user_pass = POST["userPass"];
}
function check_var($var)
{
if(isset($var))}
{
return $var;
}
$user_email = check_var($_POST["userEmail"]);
$user_pass = check_var(POST["userPass"]);
Note: this is my first time of writing tutorial and you may find it difficult to grab your comment will be attend to ASAP, also remember to check Notes of Genius for better understanding on how to fixed PHP’s E_NOTICE "Undefined index“
Have a nice day,
2 Comments
I have error:
ReplyDeleteUndefined index: product_id in /home/tokoling/public_html/vqmod/vqcache/vq2-catalog_controller_module_sales_motivator.php on line 43
Line43 contains:
$product_id = $this->request->get['product_id'];
How do you suggest to fix this?
Sorry for Replying you late.
ReplyDeleteI suppose the "product_id" is a form variable,
Try using if isset():
if(!isset($product_id)){
$product_id = $this->request->get['product_id'];
}
else
{
$product_id = $this->request->get['product_id'];
}
I dont know what your code does but i hope this should help