Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8

Thread: PHP errors?

  1. #1
    New Coder
    Join Date
    Sep 2010
    Posts
    67
    Thanks
    9
    Thanked 0 Times in 0 Posts

    PHP errors?

    I am running Lubuntu 12.10 and having installed Xampp-1.8.1 started to learn PHP. All went well until i followed a tutorial which showed how a PHP script passes data to an html form.

    The html form
    Code:
    <html>
    <head>
    </head>
    <body>
    
    <form action="form_script.php" method="POST">
    
    <p>Name: <input type="text"  name="name" size="30"/></p>
    
    <p>Shirt size:
    <select name="size">
    <option value="small">Small</option>
    <option value="medium">Medium</option>
    <option value="large">Large</option>
    </select>
    </p>
    
    <p>Gender:
    <input type="radio" name="gender" value="girl"/>Girl
    <input type="radio" name="gender" value="boy"/>Boy
    </p>
    
    <input type="submit" name="submit" value="Submit"/>
    
    </body>
    </html>
    The php script
    Code:
    <?php
    
    $name=$_POST['name'];
    $size=$_POST['size'];
    $gender=$_POST['gender'];
    
    print "<p>Name: $name<br />Size: $size<br />Gender: $gender</p>";
    
    ?>
    When i try to run the code all i get is:
    Code:
    Notice: Undefined index: name in /opt/lampp/htdocs/form_script.php on line 3
    
    Notice: Undefined index: size in /opt/lampp/htdocs/form_script.php on line 4
    
    Notice: Undefined index: gender in /opt/lampp/htdocs/form_script.php on line 5
    
    Name:
    Size:
    Gender:
    Could someone help me to resolve this problem please.

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,683
    Thanks
    25
    Thanked 656 Times in 655 Posts
    I don't have Lubuntu 12.10 or Xampp-1.8.1, but could the missing close tag for the form have any effect?

    Code:
    ....
    <input type="submit" name="submit" value="Submit"/>
    </form>  <!--      ADD THIS-->
    </body>
    </html>

  • #3
    New Coder
    Join Date
    Sep 2010
    Posts
    67
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Hi sunfighter

    Trust me to have made that mistake. But i have now rectfied the error.
    There's still no improvement in the situation. For some reason when i call the html form it is only displayed as code?

    Shall get the html form up to standard and validated.

  • #4
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,474
    Thanks
    63
    Thanked 537 Times in 524 Posts
    Try this and get back to us with the results:

    PHP Code:
    <?php

    var_dump
    ($_POST);

    $name=$_POST['name'];
    $size=$_POST['size'];
    $gender=$_POST['gender'];

    print 
    "<p>Name: $name<br />Size: $size<br />Gender: $gender</p>";

    ?>
    I can't really think of anything to write here now...

  • #5
    New Coder
    Join Date
    Sep 2010
    Posts
    67
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Have finally worked it out by myself,lol. I was (as a noob) calling the php script via Firefox instead of the html form.

    For example i was openning Firefox and typing: localhost/form_script.php and it was this that generated the errors.

    On the other hand when i open Firefox and typed: localhost/simple_form.html
    the form duly appeared and i successfully entered the required data.

    Thanks for the help and advice all

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    That can be fixed by checking the isset status:
    PHP Code:
    <?php

    if (isset($_POST['name'], $_POST['size'], $_POST['gender']))
    {
        
    $name=$_POST['name'];
        
    $size=$_POST['size'];
        
    $gender=$_POST['gender'];

        print 
    "<p>Name: $name<br />Size: $size<br />Gender: $gender</p>";
    }
    else
    {
        
    header('Location: http://yoursite.com/yourform.html');
        exit();
    }
    ?>
    Would do it. You'll get very familiar with the isset construct in PHP.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #7
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,474
    Thanks
    63
    Thanked 537 Times in 524 Posts
    Except he wasn't even submitting the form lol. Instead he was browsing to it lol.
    I can't really think of anything to write here now...

  • #8
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Yep I know. But it would have redirected to the form assuming the header was changed for it.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •