Notice Undefined index (how to set to no value?)
Moderator: General Moderators
Notice Undefined index (how to set to no value?)
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"?
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?)
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...
We can help you, but first we need to see the code...
Re: Notice Undefined index (how to set to no value?)
Use the isset() function.
Edit: This post was recovered from search engine cache.
Code: Select all
<?php
if (isset($array['who'])) {
echo $array['who'];
}
?>
Last edited by McInfo on Tue Jun 15, 2010 10:04 pm, edited 1 time in total.
Re: Notice Undefined index (how to set to no value?)
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...
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:
Re: Notice Undefined index (how to set to no value?)
oops - left my notes in there... lol
Re: Notice Undefined index (how to set to no value?)
Both A and B have the same result.
A
B
Edit: This post was recovered from search engine cache.
A
Code: Select all
$user = '';
if (isset($_POST['who'])) {
$user = $_POST['who'];
}
$message = '';
if (isset($_POST['message'])) {
$message = $_POST['message'];
}Code: Select all
$user = isset($_POST['who']) ? $_POST['who'] : '';
$message = isset($_POST['message']) ? $_POST['message'] : '';
Last edited by McInfo on Tue Jun 15, 2010 10:05 pm, edited 1 time in total.
Re: Notice Undefined index (how to set to no value?)
Right on McInfo!
I'll give that a shot... What exactly is it doing there - is it saying "who equals who"?
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?)
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?)
More accurately...
PHP Manual: isset()
This
means
PHP Manual: Ternary Operator
Edit: This post was recovered from search engine cache.
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'];
}This
Code: Select all
$user = isset($_POST['who']) ? $_POST['who'] : '';Code: Select all
if (isset($_POST['who'])) {
$user = $_POST['who'];
} else {
$user = '';
}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.
- 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?)
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().
in your example, isset() should work, but this is a handy thing to keep in mind.
Code: Select all
$test['one'] = null;
isset($test['one']); // FALSE
array_key_exists('one', $test); // TRUE
Re: Notice Undefined index (how to set to no value?)
I'll mess with it at home tonight - instead of at work... lol
Re: Notice Undefined index (how to set to no value?)
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.)
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.)