Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Kevin Frostad
Kevin Frostad
3,483 Points

PHP: Making form for validating input -Help?

I'm trying to make a form in php/html to give me an error message when required input does not exist. The form looks ok and the second file to check for errors are included correctly, but when i hit submit, nothing happens.

I suspect the fault might exist within this code, inside the Main file:

    <form>
        <p>
             Name: <input type="text" name="navn">
            <span class="Error message">*
             <?php echo $nameErr;?> </span>
        </p> 
        <p>
            Email: <input type="text" name="email">
            <span class="Error message">*
             <?php echo $emailErr; ?> </span>
        </p>

Php Main file:

<?php include('file2'); ?>


<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
    <h1>PHP-validating data</h1>
    <form>
        <p>
             Name: <input type="text" name="navn">
            <span class="Error message">*
             <?php echo $nameErr;?> </span>
        </p> 
        <p>
            Email: <input type="text" name="email">
            <span class="Error message">*
             <?php echo $emailErr; ?> </span>
        </p>
        <p>URL: <input type="text"></p>
        <p>
            Comment: <br/>
            <textarea rows="4" cols="30"> </textarea>
         </p>
        <p>Gender:
            <input type="radio" name="man"> Man
            <input type="radio" name="woman"> Woman <br/>
        </p>
        <p><input type="submit" value="submit"></p>
    </form>

    <form method="post" action= "<?php echo
        htmlspecialchars($_SERVER["PHP_SELF"])?>"> 

</body>
</html>

Php file2:

<?php  

    function test_input($data) {
        $data = trim($data);
        $data = stripslashes($data);
        $data =
    htmlspecialchars($data);
        return $data;
    } 

    $name="";
    $email="";
    $url="";
    $comment="";
    $gender="";

    if ($_SERVER["REQUEST_METHOD"]==“POST”) {
        $name = test_input($_POST["name"]);
        $email = test_input($_POST["email"]);
        $url = test_input($_POST["url"]);
        $comment = test_input($_POST["comment"]);
        $gender = test_input($_POST("gender"));
    }

    $nameErr = $emailErr = $urlErr = $commentErr =
    $genderErr = ""; 

    if ($_SERVER["REQUEST_METHOD"]=="POST") {
        if (empty($_POST["name"]))
            $nameErr = "name må fylles ut!";
        else
            $name = testInput($_POST["name"]);
         if (empty($_POST["url"]))
            $urlErr = "";
        else
            $url = testInput($_POST["url"]);
    }


?>

1 Answer

I'd start here with the syntax error: $gender = test_input($_POST("gender")); --- you'd want that to read $_POST['gender'].

Also, there's some funky stuff going on here:

$nameErr = $emailErr = $urlErr = $commentErr =
    $genderErr = ""; 

    if ($_SERVER["REQUEST_METHOD"]=="POST") {
        if (empty($_POST["name"]))
            $nameErr = "name må fylles ut!";
        else
            $name = testInput($_POST["name"]);
         if (empty($_POST["url"]))
            $urlErr = "";
        else
            $url = testInput($_POST["url"]);
    }

I'd take the time to itemize each variable, i.e.

$nameErr = "";
$emailErr = "";   
etc.

Then you're missing some curly braces, try this:

    if ($_SERVER["REQUEST_METHOD"]=="POST") {
        if (empty($_POST["name"])) {
            $nameErr = "name må fylles ut!";
            } else {
            $name = testInput($_POST["name"]);
            }
         if (empty($_POST["url"])) {
            $urlErr = "";
             } else {
            $url = testInput($_POST["url"]);
           }
     }