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

Andrew Lenti
seal-mask
.a{fill-rule:evenodd;}techdegree
Andrew Lenti
Front End Web Development Techdegree Student 7,193 Points

Using the htmlspecialchars function

Hello, I am trying to escape out the two input fields and the textarea field on the attached code but am getting syntax errors. I would appreciate it if someone could lead me in the right direction. Thanks, Andrew

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($name)) (echo htmlspecialchars($_POST[$listing_name])); ?>">
                </td>
            </tr>
            <tr>
                <th>
                    <label for="Link">Link</label>
                </th>
                <td>
                    <input id="link" name="link" value="<?php if (isset ($link)) (echo htmlspecialchars($_POST[$listing_link])); ?>">
                </td>
            </tr>
            <tr>
                <th>
                    <label for="Description">Description</label>
                </th>
                <td>
                    <textarea id="description" name="description"><?php echo htmlspecialchars($_POST[$listing_description]); ?></textarea>
                </td>
            </tr>    
        </table>
        <input type="submit" value="Save">
    </form>

</body>
</html>

2 Answers

Hi Andrew,

The 3 variables that are used are coming from an external source and aren't post variables. You only need to pass those 3 variables directly into the htmlspecialchars() function to escape the malicious output.

the first one would look like this:

<?php echo htmlspecialchars($listing_name); ?>

You're welcome. If you're just starting out then it's going to take a lot of practice and you'll make a lot of mistakes.