By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,825 Members | 1,456 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,825 IT Pros & Developers. It's quick & easy.

Undefine index, please help... please :(

P: n/a
sam
HI,

I installed php4 for apached and restart apache afterward. but my little
php script generated error followint error:
PHP Notice: Undefined index: myname in
/usr/local/www/data-dist/www.authtec.com/php-test.php on line 2

I was trying to run the following php script in apache:

<?php
if($_POST["myname"]) {
print "Hello, ".$_POST["myname"]."<p>\n"
.nl2br($_POST["textbox"]);
} else {
?>
<form method="post" action="php-test.php">
<input type=input type=text name=myname>
<textarea cols=20 rows=4 wrap=virtual name=textbox></textarea>
<input type=submit value="Submit">
</form>
<?php
}
?>

What could be wrong with my configuration? and how can I fix it?

Thanks
Sam
Jul 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
sam wrote:

<snip>
<?php
if($_POST["myname"]) {
print "Hello, ".$_POST["myname"]."<p>\n"
.nl2br($_POST["textbox"]);
} else {

<snip>

You are getting a notice, not an error. Do this instead:
if(isset($_POST["myname"]))
and your notice will be gone.
Jul 17 '05 #2

P: n/a
Sam,

You should _always_ make sure a key exists in an array before using it.

<?php
if (array_key_exists('myname', $_POST) ) {
echo "<p>Hello, ", $_POST['myname'], "</p>";
}
?>

I also would suggest a function which looks in both $_POST and $_GET for a
parameter, unescaping it if necessary.

HTH,
La'ie Techie

Jul 17 '05 #3

P: 1
please help me with this:
i have the same problem and when i tried to do same thing as u stated wherein adding an "isset" function, the form will not appear when i run it.
i recieved this warning:
Notice: Undefined index: login in D:\WebDeveloped\login.php

code is here:

if (!$logged['username'])
{
if (!$_POST['login'])
{
echo("
<center><form method=\"POST\">
<table>
<tr>
<td align=\"right\">
Username: <input type=\"text\" size=\"15\" maxlength=\"25\" name=\"username\">
</td>
</tr>
<tr>
<td align=\"right\">
Password: <input type=\"password\" size=\"15\" maxlength=\"25\" name=\"password\">
</td></tr><tr>
<td align=\"center\">
<input type=\"submit\" name=\"login\" value=\"Login\">
</td></tr><tr>
<td align=\"center\">
<a href=\"register.php\">Register Here</a>
</td></tr></table></form></center>");
}
if ($_POST['login']) {
// the form has been submitted. We continue...
$username=$_POST['username'];
$password = md5($_POST['password']);
// the above lines set variables with the submitted information.
$info = mysql_query("SELECT * FROM users WHERE username = '$username'") or die(mysql_error());
$data = mysql_fetch_array($info);
if($data[password] != $password) {
// the password was not the user's password!
echo "Incorrect username or password!";
}else{
// the password was right!
$query = mysql_query("SELECT * FROM users WHERE username = '$username'") or die(mysql_error());
$user = mysql_fetch_array($query);
// gets the user's information
setcookie("id", $user['id'],time()+(60*60*24*5), "/", "");
setcookie("pass", $user['password'],time()+(60*60*24*5), "/", "");
// the above lines set 2 cookies. 1 with the user's id and another with his/her password.
echo ("<meta http-equiv=\"Refresh\" content=\"0; URL=http://test/editprofile.php\"/>Thank You! You will be redirected");

}
}
}
else
{
// we now display the user controls.
echo ("<center>Welcome <b>$logged[username]</b><br /></center>
- <a href=\"editprofile.php\">Edit Profile</a><br />
- <a href=\"members.php\">Member List</a><br />
- <a href=\"logout.php\">Logout</a>");
}
Apr 25 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.