Page 1 of 1
Notice Undefined index (how to set to no value?)
Posted: Wed Jun 03, 2009 7:24 pm
by pearjam
Hi again!
So I'm working on building a form, and it's working great considering I barely know what I'm doing. lol
Here's what I'm trying to figure out now... I'm getting a "Notice: Undefined index: who in......"
I can fix error reporting, but I'd like to try to do it right and fix it.
How can I define what "who" is before the code is ran?
Is there a way to hard define "who" as nothing until the code runs?
or...
Is there a way to say "ignore indexes"?
Re: Notice Undefined index (how to set to no value?)
Posted: Wed Jun 03, 2009 8:16 pm
by mikemike
This is happening because you have an array you're calling with an index 'who' which doesn't exist. Simple solution, fix your code
We can help you, but first we need to see the code...
Re: Notice Undefined index (how to set to no value?)
Posted: Wed Jun 03, 2009 8:47 pm
by McInfo
Use the
isset() function.
Code: Select all
<?php
if (isset($array['who'])) {
echo $array['who'];
}
?>
Edit: This post was recovered from search engine cache.
Re: Notice Undefined index (how to set to no value?)
Posted: Wed Jun 03, 2009 10:04 pm
by pearjam
pickle | Please use [ code=php ], [ code=text ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:
Posting Code in the Forums to learn how to do it too.
Ok, here's the code...
I know it has bad form (no pun intended - ugh), but I'm just learning - so bear with me...
Code: Select all
<form method="post" action="processing.php">
Who:<br /><input type="text" name="who" style="width:130px;" /><br />
What?<br /><textarea name="comment" style="width:130px;"></textarea><br />
<input type="submit" value="post" />
</form>
<?php
$user = $_POST["who"]; //Gets the user from the from and asigns it to $user.
$message = $_POST["comment"]; //Gets the comment from the form and asigns it to $message.
$date = date("[ Md 'y ]"); //Assigns the date at that time to $date.
if($user != ''){ //Says "If nothing in field, write nothing instead of empty breaks.
$out = fopen("indexdata.txt", "a+"); //Opens data.txt to append (add) to.
fwrite($out, $date. " <b>$user</b> said:<br /><i>$message</i><br /><br />");
fclose($out); }
?>
pickle | Please use [ code=php ], [ code=text ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:
Posting Code in the Forums to learn how to do it too.
Re: Notice Undefined index (how to set to no value?)
Posted: Wed Jun 03, 2009 10:05 pm
by pearjam
oops - left my notes in there... lol
Re: Notice Undefined index (how to set to no value?)
Posted: Thu Jun 04, 2009 12:03 am
by McInfo
Both A and B have the same result.
A
Code: Select all
$user = '';
if (isset($_POST['who'])) {
$user = $_POST['who'];
}
$message = '';
if (isset($_POST['message'])) {
$message = $_POST['message'];
}
B
Code: Select all
$user = isset($_POST['who']) ? $_POST['who'] : '';
$message = isset($_POST['message']) ? $_POST['message'] : '';
Edit: This post was recovered from search engine cache.
Re: Notice Undefined index (how to set to no value?)
Posted: Thu Jun 04, 2009 7:48 am
by pearjam
Right on McInfo!
I'll give that a shot... What exactly is it doing there - is it saying "who equals who"?
Re: Notice Undefined index (how to set to no value?)
Posted: Thu Jun 04, 2009 8:02 am
by mikemike
It's saying 'if the POST variable 'who' is available then set it, otherwise do nothing'.
Re: Notice Undefined index (how to set to no value?)
Posted: Thu Jun 04, 2009 10:46 am
by McInfo
More accurately...
Code: Select all
// Assigns an empty string to the $user variable. $user is now "set".
$user = '';
/* Checks if $_POST['who'] is set. $_POST is a superglobal array. The $_POST
* array is always set. This condition checks just the 'who' index of the
* $_POST array. If the 'who' index exists and the value is not null, isset()
* returns true. The condition is then true, so the code within the block is
* executed.
*/
if (isset($_POST['who'])) {
// Assigns the value of $_POST['who'] to $user.
$user = $_POST['who'];
}
PHP Manual:
isset()
This
Code: Select all
$user = isset($_POST['who']) ? $_POST['who'] : '';
means
Code: Select all
if (isset($_POST['who'])) {
$user = $_POST['who'];
} else {
$user = '';
}
PHP Manual:
Ternary Operator
Edit: This post was recovered from search engine cache.
Re: Notice Undefined index (how to set to no value?)
Posted: Thu Jun 04, 2009 11:36 am
by ornerybits
Also checkout out the array_key_exists(),
http://us.php.net/manual/en/function.ar ... exists.php. I've had a pitfall or two working with arrays, null values, and isset().
Code: Select all
$test['one'] = null;
isset($test['one']); // FALSE
array_key_exists('one', $test); // TRUE
in your example, isset() should work, but this is a handy thing to keep in mind.
Re: Notice Undefined index (how to set to no value?)
Posted: Thu Jun 04, 2009 3:49 pm
by pearjam

it didn't work for me...
I'll mess with it at home tonight - instead of at work... lol
Re: Notice Undefined index (how to set to no value?)
Posted: Thu Jun 04, 2009 6:49 pm
by pearjam
Ok - I'm having 2 more challenges for each one I fix... lol - so I'm going to start a new thread, with the idea I'm shooting for, and the code.
Maybe you guys can show me what I'm doing wrong... or show me how you all would do it.
(I'm doing a new thread because it will likely take a whole different route.)