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 trialWarren Johnson
6,870 PointsCode 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
Ernest Grzybowski
Treehouse Project ReviewerThis 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. =)
Warren Johnson
6,870 PointsWarren Johnson
6,870 PointsI 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"); ?>
<?php include(ROOT_PATH . "inc/footer.php"); ?>
Warren Johnson
6,870 PointsWarren Johnson
6,870 PointsUPDATED
Here's my -updated interpretation of the solution (snippet from Code Challenge): Still not right though...
Ernest Grzybowski
Treehouse Project ReviewerErnest Grzybowski
Treehouse Project ReviewerYou 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
6,870 PointsWarren Johnson
6,870 PointsHey 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:
Ernest Grzybowski
Treehouse Project ReviewerErnest Grzybowski
Treehouse Project ReviewerIn the code above, replace the
$s
with$search_term
.Warren Johnson
6,870 PointsWarren Johnson
6,870 PointsHey Thanks Ernest! I figured it out about 2 hrs. ago! Thanks once again for getting me on the right track.