Undefine index, please help... please :(

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • sam

    #1

    Undefine index, please help... please :(

    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
  • Zurab Davitiani

    #2
    Re: Undefine index, please help... please :(

    sam wrote:

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

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

    Comment

    • Lāʻie Techie

      #3
      Re: Undefine index, please help... please :(

      Sam,

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

      <?php
      if (array_key_exis ts('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

      Comment

      • MasterDrive
        New Member
        • Apr 2006
        • 1

        #4
        Notice: Undefined index: login in D:\WebDeveloped \jobsfortest\lo gin.php on line 13

        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=$_POS T['username'];
        $password = md5($_POST['password']);
        // the above lines set variables with the submitted information.
        $info = mysql_query("SE LECT * FROM users WHERE username = '$username'") or die(mysql_error ());
        $data = mysql_fetch_arr ay($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("SE LECT * FROM users WHERE username = '$username'") or die(mysql_error ());
        $user = mysql_fetch_arr ay($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>Welco me <b>$logged[username]</b><br /></center>
        - <a href=\"editprof ile.php\">Edit Profile</a><br />
        - <a href=\"members. php\">Member List</a><br />
        - <a href=\"logout.p hp\">Logout</a>");
        }
        Last edited by MasterDrive; Apr 25 '06, 04:44 AM.

        Comment

        Working...