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
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.
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?
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.
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!
Your test is saying:
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
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'])) {
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With