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

Pauline Orr
Pauline Orr
15,321 Points

Whats wrong with this code?

Not sure what I am doing wrong?

Also do I need to include this escape htmlspecialchars to all my inputs? Including ones for email, phones, name?

Also how do you filter/sanitize a drop down object/select value? The same way you do other values?

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>h
                </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>

1 Answer

Hey.

Yes, you should print out variables wich contain user input always with htmlspecialchar.

The Challenge:

You need to print the Variables you defined and not the ones from the POST Object. Here's the Code:

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

</body>
</html>