Notice Undefined index (how to set to no value?)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
pearjam
Forum Commoner
Posts: 29
Joined: Sun May 31, 2009 5:05 pm

Notice Undefined index (how to set to no value?)

Post 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"?
User avatar
mikemike
Forum Contributor
Posts: 355
Joined: Sun May 24, 2009 5:37 pm
Location: Chester, UK

Re: Notice Undefined index (how to set to no value?)

Post 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...
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Notice Undefined index (how to set to no value?)

Post 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.
Last edited by McInfo on Tue Jun 15, 2010 10:04 pm, edited 1 time in total.
pearjam
Forum Commoner
Posts: 29
Joined: Sun May 31, 2009 5:05 pm

Re: Notice Undefined index (how to set to no value?)

Post 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: :arrow: 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: :arrow: Posting Code in the Forums to learn how to do it too.
pearjam
Forum Commoner
Posts: 29
Joined: Sun May 31, 2009 5:05 pm

Re: Notice Undefined index (how to set to no value?)

Post by pearjam »

oops - left my notes in there... lol
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Notice Undefined index (how to set to no value?)

Post 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.
Last edited by McInfo on Tue Jun 15, 2010 10:05 pm, edited 1 time in total.
pearjam
Forum Commoner
Posts: 29
Joined: Sun May 31, 2009 5:05 pm

Re: Notice Undefined index (how to set to no value?)

Post by pearjam »

Right on McInfo!

I'll give that a shot... What exactly is it doing there - is it saying "who equals who"?
User avatar
mikemike
Forum Contributor
Posts: 355
Joined: Sun May 24, 2009 5:37 pm
Location: Chester, UK

Re: Notice Undefined index (how to set to no value?)

Post by mikemike »

It's saying 'if the POST variable 'who' is available then set it, otherwise do nothing'.
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Notice Undefined index (how to set to no value?)

Post 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.
Last edited by McInfo on Tue Jun 15, 2010 10:05 pm, edited 1 time in total.
User avatar
ornerybits
Forum Newbie
Posts: 18
Joined: Thu May 14, 2009 2:08 pm
Location: Kansas

Re: Notice Undefined index (how to set to no value?)

Post 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.
pearjam
Forum Commoner
Posts: 29
Joined: Sun May 31, 2009 5:05 pm

Re: Notice Undefined index (how to set to no value?)

Post by pearjam »

:( it didn't work for me...

I'll mess with it at home tonight - instead of at work... lol
pearjam
Forum Commoner
Posts: 29
Joined: Sun May 31, 2009 5:05 pm

Re: Notice Undefined index (how to set to no value?)

Post 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.)
Post Reply