Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

PHP Enhancing a Simple PHP Application Adding Search: Controller & View Escaping Output Review

I am having problem with this exercise. I just did what the task asked me to and it is displaying an error

The error says that i should put a value attribute to the input text, but it ois already there.

index.php
<?php

require_once("inc/config.php");
$name=$_GET["s"];
$search_term = "";
if (isset($_GET["s"])) {
    $search_term = trim($_GET["s"]);
    if ($search_term != "") {
        require_once(ROOT_PATH . "inc/products.php");
        $products = get_products_search($search_term);
    }
}

$pageTitle = "Search";
$section = "search";
include(ROOT_PATH . "inc/header.php"); ?>

    <div class="section shirts search page">

        <div class="wrapper">

            <h1>Search</h1>

            <form method="get" action="./">
                <input type="text" name="s"  value="<?php if (isset($name)) { echo htmlspecialchars($name); } ?>">
                <input type="submit" value="Go">
            </form>

            <?php

                if ($search_term != "") {
                    if (!empty($products)) {
                        echo '<ul class="products">';
                        foreach ($products as $product) {
                            echo get_list_view_html($product);
                        }
                        echo '</ul>';
                    } else {
                        echo '<p>No products were found matching that search term.</p>';
                    }
                }

            ?>

        </div>

    </div>

<?php include(ROOT_PATH . "inc/footer.php"); ?>

Looks like you have a conditional on value which could make it unset if $name isn't set to anything. considering that it's a get variable that's setting name, I would check the url to see if you can see the variable set, if so, then do a var dumb on $name to check to see if it's being stored there. If it's stored there, I would try to remove the conditional from the value by maybe cleaning it up at the top after retrieving the $GET variable and then storing the stripped version and then simply echoing the variable for value down below instead of right then. either way, I would probably do a var dumb after it's cleaned to be sure I was getting what I wanted for value. Example would be something like this :

<?php

require_once("inc/config.php");
//process $name a little further here so I won't need to do it below
$name= htmlspecialchars($_GET["s"]);
$search_term = "";
if (isset($_GET["s"])) {
    $search_term = trim($_GET["s"]);
    if ($search_term != "") {
        require_once(ROOT_PATH . "inc/products.php");
        $products = get_products_search($search_term);
    }
}

$pageTitle = "Search";
$section = "search";
include(ROOT_PATH . "inc/header.php"); ?>

    <div class="section shirts search page">

        <div class="wrapper">

            <h1>Search</h1>

            <form method="get" action="./">           
                <input type="text" name="s"  value="<?php echo $name ?>"> //Now I can remove this conditional and just echo the variable
                <input type="submit" value="Go">
            </form>

            <?php

                if ($search_term != "") {
                    if (!empty($products)) {
                        echo '<ul class="products">';
                        foreach ($products as $product) {
                            echo get_list_view_html($product);
                        }
                        echo '</ul>';
                    } else {
                        echo '<p>No products were found matching that search term.</p>';
                    }
                }

            ?>

        </div>

    </div>

<?php include(ROOT_PATH . "inc/footer.php"); ?>

Don't know what your working on, but it's worth a shot removing the conditional as it could be the reason why you have no value set and causing the error. If you want to elaborate more on the question I'm here.