Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Undefined index even with isset

although i used if(isset()) still get

 Notice: Undefined index: user_name in C:\xampp\htdocs\www\jq\182-186 Users online sample application\users.php

here is part of PHP code

if (isset($_POST['user_name']  , $_POST['action']) || isset($_POST['action'])){

    $user_name = $_POST['user_name'];
    $action = $_POST['action'];

EDIT: || isset($_POST['action']) IS needed for part of jquery that checks DATABASE every half second.

jquery

setInterval(function(){
    $.post('users.php', { action : 'list'} , function(data){
        $('#users_online').html(data);
        });


    },500);

so i can't delete it

like image 743
MJ1 Avatar asked Jan 02 '14 11:01

MJ1


People also ask

What are undefined offset and index errors?

Undefined offset and index errors. An undefined offset notice will occur if you attempt to access an index that does not exist. This is where you’ll encounter nasty errors such as the following.

How to fix undefined Index in PHP?

Quite expectedly, it does not work and raises a notice called Undefined Index in PHP. You can fix it using the isset () function, which we will discuss further in the upcoming sections. Undefined Index is a notice in PHP, and it is a choice of the developer to either ignore it or fix it. How to Ignore PHP Notice: Undefined Index?

What is an undefined offset notice in PHP?

An undefined offset notice will occur if you attempt to access an index that does not exist. This is where you’ll encounter nasty errors such as the following. Or, if you are using array keys instead of numerical indexes. PHP will display these notices if you attempt to access an array index that does not exist.

What does undefined Index test in/path/to file?

Notice: Undefined index: test in / path / to / file. php on line 2 These notices will occur whenever you attempt to access an index that does not exist! Although the execution of your script will not be halted (it is not a fatal error), these kind of notices tend to cause bugs that can lead to other issues!


2 Answers

Your test is saying:

  • If:
    • user_name is set
    • AND
    • action is set
  • OR
    • action is set

What this means is that so long as action is set, the test will pass, even if user_name is not set, as is clearly the case in your error message.

Just remove that || isset($_POST['action']) bit and it should work fine.

Also, have a +1 for making me realise that all my isset(...) && isset(...) chains that I've ever written are superfluous XD

like image 87
Niet the Dark Absol Avatar answered Sep 21 '22 17:09

Niet the Dark Absol


Whatever you are trying to achieve with this part isset($_POST['user_name'] , $_POST['action']) it will always be ignored if you have isset($_POST['action']). So if only action index is present, this line will be executed $user_name = $_POST['user_name']; and it will search for user_name index, no matter it does not exist.

You might want to have: if(isset($_POST['user_name']) && isset($_POST['action'])) {

like image 42
Royal Bg Avatar answered Sep 21 '22 17:09

Royal Bg