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 Build a Basic PHP Website (2018) Enhancing a Form Escaping Output

What's wrong with this code?

Here is the quiz. "The PHP file below includes another PHP file that retrieves information from an external source for a directory listing. The page is supposed to display two input fields and a textarea, but it contains some malicious code. Modify the PHP code below to escape the output and display the correct HTML elements on the screen."

And I got this error. "! I still see some malicious code in there. You want to escape the name variable before displaying it to the screen." "PHP Notice: Undefined index: name in views_listing_edit.php on line 13 PHP Notice: Undefined index: link in views_listing_edit.php on line 21 PHP Notice: Undefined index: description in views_listing_edit.php on line 29"

and here is the code.

views_listing_edit.php
<?php require_once("controllers_listing.php"); ?><html>
<body>

    <h1>Edit Listing</h1>

    <form method="post">
        <table>
            <tr>
                <th>
                    <label for="name">Name</label>
                </th>
                <td>
                    <input id="name" name="name" value="<?php if(isset($listing_name)){echo htmlspecialchars($_POST["name"]); }?>">
                </td>
            </tr>
            <tr>
                <th>
                    <label for="Link">Link</label>
                </th>
                <td>
                    <input id="link" name="link" value="<?php if(isset ($listing_link)){echo htmlspecialchars($_POST["link"]);} ?>">
                </td>
            </tr>
            <tr>
                <th>
                    <label for="Description">Description</label>
                </th>
                <td>
                    <textarea id="description" name="description"><?php if(isset($listing_description)){echo htmlspecialchars($_POST["description"]); }?></textarea>
                </td>
            </tr>    
        </table>
        <input type="submit" value="Save">
    </form>

</body>
</html>

2 Answers

Henrik Hansen
Henrik Hansen
23,176 Points

All you have to do is wrap the variables in the htmlspecialchars function.

Thank you very much. :)