This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 8 years ago.
I have two PHP files, index.php and insert.php.
index.php - a form and a button to send data
insert.php - receives the values of the form to insert them into a database, and it works.
When I redirect to the index.php, it shows a successful message.
I have this code in index.php to show the message:
<?php
$mensaje=$_GET['mensaje'];
if($mensaje=="")
{
}
else
{
echo "¡Registro exitoso!";
}
?>
but when I open it the first time, it shows me:
Notice: Undefined index: mensaje in C:\xampp\htdocs\..\index.php on line 41
How could I show the message ONLY after the insert?
Like this:
$mensaje = isset($_GET['mensaje']) ? $_GET['mensaje'] : "";
That way, $mensaje will never be undefined.
The ternary operator works this way:
$mensaje = if($_GET['mensaje'] exists) then $mensaje = $_GET['mensaje'], else $mensaje = "";
It's like doing:
if(isset($_GET['mensaje'])){
$mensaje = $_GET['mensaje'];
}
else
$mensaje = "";
}
For a more accurate information about ternary operators:
http://en.wikipedia.org/wiki/%3F:
For information about function "isset":
http://php.net/manual/en/function.isset.php
This happens because your $_GET['mensaje'] not only is empty but doesn't exist at all.
To work this around, replace
$mensaje = $_GET['mensaje'];
by
$mensaje = empty($_GET['mensaje'])? "": $_GET['mensaje'];
Thus you assign an empty string "" if $_GET['mensaje'] is either blank or not set.
Can $mensaje really be empty (as your if condition implies) or is it simply defined or undefined in $_GET (as your text description implies)? If the latter is the case than your solution is to test for existence, not for content:
<?php
if( isset($_GET['mensaje']) )
{
echo "¡Registro exitoso!";
}
else
{
}
?>
isset() is a language construct, not a function. This is the reason why it can be used to test for existence of an array index without throwing an "undefined index" notice if the index does not exist.
Hint (just for completeness):
isset() will also return False if the value of an array element is NULL. But this is irrelevant in the case of $_GET, as it always and exclusively contains string values.
Related
This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 5 years ago.
I am getting error message :
undefined index:
in my code. Below is the function on the line
if($user['flagged'] == 1){
$flag = "Your last payment has been flagged as NOT RECEIVED.";
}
it says Undefined index: flagged
How can this be fixed
Just check if msg index of global array $_GET is set. For example
if(isset($_GET['msg']) && $_GET['msg']==="success")
I think it will solve your problem.
use isset() to check it first
if(isset($_GET['msg']) && $_GET['msg']==="success"){
$log_prompt = '<span style="color:red">You Have successfully registered. Login Now!</span>';
}
if the first condition isset($_GET['msg']) returned false, it will escape the second part of the if condition and will not print your message.
read more about Logical Operators
When you load the page the URL must me something like this:
localhost/your_page.php?msg=something_value
But your app mustn't return error when in URL there isn't ?msg=value... You have to use isset()
if(isset($_GET['msg']) && $_GET['msg'] == "success")
=== is recommended
if(isset($_GET['msg']) && $_GET['msg'] === "success")
More info: http://php.net/manual/en/language.operators.logical.php
This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 7 years ago.
I have used $value = (isset($_REQUEST['value'])) to define the index variable. However, $value variable shows as a type Boolean and value of 0 or 1 in the debugger, which results in not getting the correct results when the $value is used with a (if) statement.
$page_limit = (isset($_REQUEST["list_page"]));
$viewdate = (isset($_REQUEST["viewdate"]));
How do I correct the following so that the index is defined?
if($_REQUEST["viewdate"] == '') {
$viewdate = 'All';
} else {
$viewdate = $_REQUEST["viewdate"];
}
$targetpage = "newindex.php?viewdate=".$_REQUEST["viewdate"]."&search=Search";
$page = (isset($_REQUEST['page']));
if($page_limit == '') {
The code above works, without the isset() function, but displays Notice - E messages
You should use isset to test whether a variable has been set, then assign the value. Like so:
$something = null;
if (isset($_REQUEST['something'])) {
$something = $_REQUEST['something'];
...
}
// Later in the code
if ($something !== null) {
// Do stuff
This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 8 years ago.
i have a php page which i should post some data to it like this:
$player=$_POST['player']; $age=$_POST['age']; $data=$_POST['data'];
but some times my page posts data and some times it shouldn't, when i dont post data i got Undefined index: error, is there any way to skip $_POST['data'] when no data is posted?
$player = isset($_POST['player'])?$_POST['player']:"";
$age = isset($_POST['age'])?$_POST['age']:"";
$data = isset($_POST['data'])?$_POST['data']:"";
The isset first checks if it's defined, if yes, it assigns the variable else it assigns empty string.
Use simple if (isset($_POST['key'])):
$player = isset($_POST['player']) ? $_POST['player'] : '';
$age = isset($_POST['age']) ? $_POST['age'] : '';
$data = isset($_POST['data']) ? $_POST['data'] : '';
Or better, but every value to some 'refix':
<input name="Data['player']"/>
and in php just do:
if (isset($_POST['Data'])) {
$player = $_POST['Data']['player'];
/* ... */
}
if(isset($_POST['DATA'])){
$data = $_POST['DATA']
}
if(isset($_POST['player'])):
$player = $_POST['player'];
endif;
Check if data is posted
if(isset($_POST['player']))
$player=$_POST['player'];
}
if(isset($_POST['age']))
$age=$_POST['age'];
}
if(isset($_POST['data']))
$data=$_POST['data'];
}
This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 8 years ago.
the message is
Notice: Undefined index: flag in C:\xampp\htdocs\myfiles\mobile tracking\index.php on line 63
my code is
<?php
$stat=$_REQUEST['flag'];
if($stat=="FAILED")
{
echo "Username/password doesnot exists";
}
?>
The global variable $_REQUEST['flag'] is probably having value NULL. This is the reason you are getting this error. Well, try using isset(). to check whether the variable is having any value or not.
You should check if the $_REQUEST['flag'] variable has been set:
<?php
$stat= ( isset($_REQUEST['flag']) ? $_REQUEST['flag'] : null) ;
if($stat=="FAILED")
{
echo "Username/password doesnot exists";
}
?>
You received a notice because you didn't initialized the values of the array. Use this construction to prevent them.
if (! array_key_exists('flag', $_REQUEST)) {
$_REQUEST['flag'] = whatever value goes here;
}
This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 4 years ago.
I have this code and it's giving me an undefined error if country is not a variable in the URL. I've never had this problem before so it's obviously something to do with server settings but I'd rather not change those because this is probably done for security purposes. I've searched Google and can't find the workaround, although I'm sure it's blindingly obvious!
$country = $_GET['country'];
if(!$country){
$incoming = $outgoing = $sms = $free = "---";
} else {
get_rates($country);
}
you should use the following approach:
if (!isset($_GET['country'])) {
$incoming = $outgoing = $sms = $free = "---";
} else {
get_rates($_GET['country']);
}
isset allows you to check if the variable exists (if not we give it the value false).
$country = isset($_GET['country'])? $_GET['country'] : false;
"Undefined index" means that element doesn't exist in the array. Your server is probably set to report these errors, while your system so far wasn't. Check if the element exists before accessing it.
$country = null;
if (!empty($_GET['country']) {
$country = $_GET['country'];
}
You should probably check to see if $_GET['country'] is set. I would wrap the entire code block you posted in an if statement that checks if the country field is set, using the isset function.
Just change:
$country = $_GET['country'];
To this:
$country = #$_GET['country'];
'#' is an operator which will remove errors output.