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 Enhancing a Simple PHP Application Adding Search: Controller & View Escaping Output Review

Prakhar Patwa
Prakhar Patwa
11,260 Points

whats wrong with that code code challaange

<?php

require_once("inc/config.php");

$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($search_term)) { echo htmlspecialchars($search_term); }  ?> " >
                <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"); ?>

```index.php
<?php

require_once("inc/config.php");

$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($search_term)) { echo htmlspecialchars($search_term); }  ?> " >
                <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"); ?>

2 Answers

I got the challenge to pass with:

<input type="text" name="s" value="<?= htmlspecialchars($search_term) ?>">

<!-- or -->

<input type="text" name="s" value="<? echo htmlspecialchars($search_term) ?>">

I don't think you need to check if the search term is set - it's always defined at the top of your script.

Prakhar Patwa
Prakhar Patwa
11,260 Points

yess i got that, later thankx.