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

Alex Watts
Alex Watts
8,396 Points

Forms and PHP

Hello,

I have two forms on my website each will use the same code. However, I need the code to echo out an error when the user inputs invalid data. Should I duplicate this code across two HTML pages although it might seem bad practice? Or should I keep the code in a separate file, but how would I show the error message within the submitted web page?

Thanks for your help and support!

1 Answer

Generally the answer to "should I duplicate X" is going to be no. Without knowing the structure of your application the most basic solution is to create a smaller PHP file that checks whether the error(s) exist and spit out the error for the user and then <?php include(''); ?> that in each page. This means you now have one thing to maintain instead of 2 (until your page grows and 6 months later it's 20).

The same can be done to reuse the form since it sounds like the form is a duplicate as well.