How To: Get rid of undefined index errors
So this is one of the most common questions i see people post throughout forums. Hopefully it will be made as a sticky topic on this forum because i think its well needed. Anyways heres a few different methods for writing your post/get request without getting an undefined index error.
Method 1
PHP Code:
<?php
/**
Checks to be sure the post array isset before actually attempting to get the value
if it isset we get the value otherwise we return a default value of false
**/
$name = (isset($_POST['name'])) ? $_POST['name'] : false;
?>
Method 2
PHP Code:
<?php
/**
Create a function to handle the post request and to be sure it isset
**/
function post($post){
if(isset($_POST[$post])){
return $_POST[$post];
}
}
//Create a post request to get a form field value that has a name of name
$name = post('name');
?>
Method 3 (see replies, below)
Code:
<?php
//Declare the variable before trying to do a post request
$name = "";
$name = $_POST['name'];
?>
so anyways these are just a few methods. Hope it helps people not to have to ask this question so often.