undefine index in simple post method

Discussion in 'PHP' started by Roshaan.N, May 5, 2012.

  1. #1
    <form action="forum get.php" method="post">
    <input type="text" name="id">
    <input type="password" name="pass">
    <input type="submit" value="Login">
    </form>

    <?php
    $a=$_POST["id"];
    $b=$_POST["pass"];
    echo $a.$b;
    ?>


    Here is my simple code, it always echo values but with error "undefine index on 1 and 2 in php code". i have also tried get, request method aswell, but still i get the error.
    plz help me out, give me a solution.
     
    Roshaan.N, May 5, 2012 IP
  2. Joker-jar

    Joker-jar Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    1
    Trophy Points:
    0
    #2
    There's wrong action in the form "forum get.php". for testing you can use something like this:

    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
     <input type="text" name="id">
     <input type="password" name="pass">
     <input type="submit" value="Login">
     </form>
    
     <?php
    if ( count($_POST) > 0 )
    {
         $a=$_POST["id"];
         $b=$_POST["pass"];
         echo $a.$b;
    }
     ?>
    Code (markup):
     
    Joker-jar, May 5, 2012 IP
  3. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #3
    Either you give the forum inputs preset values of empty, value="" or check if id & pass are set before you assign them to variables.

    
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
     <input type="text" name="id" value="">
     <input type="password" name="pass" value="">
     <input type="submit" value="Login">
     </form>
    
     <?php
    $a = $b = '';
    if (isset($_POST['id'])) {
         $a=$_POST["id"];
    }
    if (isset($_POST['pass'])) {
         $b=$_POST["pass"];
    }
    
    echo $a.$b;
    
    PHP:
     
    ThePHPMaster, May 6, 2012 IP
  4. PK-Host

    PK-Host Guest

    Messages:
    109
    Likes Received:
    3
    Best Answers:
    1
    Trophy Points:
    0
    #4
    The script works as it is however you have your error_reporting on high undefined index is just a warning so yes you can use what Joker-jar and ThePHPMaster have said to get rid of the error or you can just change your error reporting level by using hte error_reporting function http://www.php.net/manual/en/function.error-reporting.php
     
    PK-Host, May 7, 2012 IP
  5. Roshaan.N

    Roshaan.N Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thanks all of you, it works now :)
     
    Roshaan.N, May 7, 2012 IP