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

Warren Johnson
Warren Johnson
6,870 Points

Code Challenge: Adding Search: Controller & View - Escaping Output (1 of 1)

Could really use a hand out there on this one. I added a 'value' attribute to the search input field, and tried to load the 'escaped' value back into the field, after the processing of the search result:

My Code:

$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=""> ***this is where I assigned the input field a 'value' attribut***
                <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>';
                        echo $search_term = htmlspecialcharacters(s.value); ***this is where I tried to reassign the entered parameter to the field***
                    } else {
                        echo '<p>No products were found matching that search term.</p>';
                    }
                }

            ?>

        </div>

1 Answer

This one is definitely a little tricky, but actually requires one line to be modified. I personally think this question should be re-worded a bit, also in this lesson the word Get is constantly replaced with the word Git in the transcripts. What do you think Randy Hoyt, could you take a look into both of those?

I'll help you on the path to your answer with a few hints.

You will only want to update this line (All other code besides this line does not change):

<input type="text" name="s">

You will begin by adding value:

<input type="text" name="s" value"">

One hint before I leave it up to you:

<input type="text" name="s" value"<?php if <!--FILL IN THE BLANK--> ?>">

Lastly, use htmlspecialchars and not htmlspecialcharacters as that does not exist. =)

http://php.net/manual/en/function.htmlspecialchars.php

Warren Johnson
Warren Johnson
6,870 Points

I looked up the documentation (htmlspecialchars) via the link you included in the post, tried a piece of code, and...ZILCH! Really, I'm not sure "what" I am missing here...AND you're right: this challenge is definitely worded obscurely...

My Code:

<?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="./">

              ******* This is where I tried to work my interpretation of the solution out*****
            <input type="text" name="s" value="<?php if (<?php $_GET != "") {
                echo (htmlspecialchars($_GET));
              }
            <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"); ?>

Warren Johnson
Warren Johnson
6,870 Points

UPDATED

Here's my -updated interpretation of the solution (snippet from Code Challenge): Still not right though...

        <h1>Search</h1>

        <form method="get" action="./">
            ****THIS IS THE CODE THAT I ATTEMPTED TO SUBMIT****
            <input type="text" name="s" value="<?php if(isset($_GET)) {
            echo htmlspecialchars($_GET); } ?>" >

            <input type="submit" value="Go">
        </form>

You are getting pretty close, but due to the wording, we'll give you a pass on this one =)

Check out the comment from the teacher at this link.

https://teamtreehouse.com/forum/-escaping-output

and check here for the solution. https://teamtreehouse.com/forum/code-challange-escaping-output-review

Warren Johnson
Warren Johnson
6,870 Points

Hey Ernest...,

You know what? I actually used the "$s" variable as a variation when trying to get this to work, for some reason, I must have had something else "not quite right"...LOL. Now. I tried the solution EXACTLY as it was written, and...HUH?? It still is not passing: Don't know WHAT's WRONG HERE???

The error echo'ed back from the Challenge: "To place text inside a text input field, you should give it a value attribute."

Here's my code:

  <h1>Search</h1>

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

        <?php

In the code above, replace the $s with $search_term.

Warren Johnson
Warren Johnson
6,870 Points

Hey Thanks Ernest! I figured it out about 2 hrs. ago! Thanks once again for getting me on the right track.