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

PHP Creating Search Form - Getting htmlspecialchars() error

I am having a problem with the search_term variable showing up in the value of the input field. When I entered the information for the code challenge it passed, but when tested on my live site, I get this error that shows up inside of the text input field:

<br /><b>Warning</b>:  htmlspecialchars() expects parameter 1 to be string, array given in <b>/home/savvychi/public_html/TreeHouse/shirts4mike/search/index.php</b> on line <b>24</b><br />

Has anyone else had this issue?

Here is the code I have for the search page:

<?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="<?php echo BASE_URL; ?>search/">
            <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 != ""){
                echo '<ul class="products">';
                foreach ($products as $product) {
                    echo get_list_view_html($product);
                }
                echo '</ul>';
            }

        ?>

        </div>
    </div>

1 Answer

I figured it out, thanks!