Friday, September 14, 2018

Getting a notice about Undefined index when reading POST values to use with the PHP Toolkit

When reading the POST values in PHP submitted by a form, there is an error thrown:
Notice: Undefined index: title in C:\apache\www\php\my_file.php on line XX

This is just a warning and does not interrupt the execution of the code. It is usually caused by trying to assign a value to a variable using a POST value that has not been defined. There are 2 ways to handle it:
In a development environment you can edit your php.ini to supress all notices by adding "& ~E_NOTICE" to your error_reporting setting. For example:

error_reporting  =  E_ALL & ~E_NOTICE

In a production environment you may want to check if the POST value exists before assigning it to a variable. So instead of:

$POSTValues['Title'] = $_POST['Title'];

you can use

$POSTValues['Title'] = isset($_POST['Title']) ? $_POST['Title'] : null;


No comments:

Post a Comment