Undefined index even with isset
Asked Answered
C

3

5

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

Couldst answered 2/1, 2014 at 11:29 Comment(2)
how should i edit it?Couldst
@AliGajani Why is it wrong? isset() accepts multiple arguments.Gabon
M
8

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

Moya answered 2/1, 2014 at 11:32 Comment(7)
Hahaha, is that comma separated values working as && wow :DPetuu
It's what the docs say. Considering I learned everything I know about PHP from the docs, it's kind of embarrassing that I didn't know this until I looked it up while writing the answer!Moya
i'm watching jquery tutorial onthenewboston and || in needed for part of program.in actual program that he's writing it works just fine.btw thanks for answer i need time to think.Couldst
@NiettheDarkAbsol - Thanks to you as many of us including you, learnt a new side of the isset function today.Seabury
This is the first time I see someone using isset() with multiparams, I was so used to have isset(...) && isset(...) that I never searched for any different solution. Feel embarrased tooPetuu
@NiettheDarkAbsol when i delete || isset($_POST['action']) setInterval will not work.and because of that i"m not able to check database every 0.5 secondCouldst
0.5 or 5 is not the case.also browser console shows nothing the odd thing is tutorial uses the same code but not get any error!Couldst
P
1

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'])) {

Petuu answered 2/1, 2014 at 11:32 Comment(0)
A
0

change to:

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

in || (or) having one value(action) is true all the statement will be true even user_name does empty

if (isset($_POST['user_name']  , $_POST['action']) || isset($_POST['action'])) {
Allseed answered 2/1, 2014 at 11:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.