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

JavaScript

Ana Camelo
PLUS
Ana Camelo
Courses Plus Student 13,218 Points

Contact Form - jQuery - PHP

Hi,

I have 2 questions about the form functionality.

  1. I'm creating several forms for a site. In the jQuery section there is a PHP file to link to the form we have in the example for "Smells Like Bakin'". I've used all the code there as an example to create and validate my own forms but when I slick on "Submit" the browser just shows me the .php file. What do I have to modify in the .php file or in the jQuery script so when I click on "Submit" it just refreshes the current page of the site?

  2. One of the forms is in the footer and I'm creating the site from scratch with HTML - CSS and jQuery when needed. In the contact page, for example, I'll have 2 forms that I need to validate: the contact form, and the field in the footer for the user to enter his/her email address. Using the jQuery script once with the same class for both email fields would be ok or should I use different IDs for each one and repeat the email validation in jQuery?

Thanks

2 Answers

In order to achieve this all you need to do is incorporate the PHP processing inside the page that you have the form. You would use something like this...

<?php
    if(isset($_POST['submit'])) {
        //DO WHATEVER FORM PROCESSING HERE YOU WOULD HAVE ON YOUR EXTERNAL PHP PAGE
    }
?>
<!DOCTYPE html>
<html>
<head>
    <title>Your Form</title>
</head>
<body>
    <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">>
        <input name="something" type="text">
        <input name="something_else" type="text">
        <button type="submit" name="submit">Submit</button>
    </form>
</body>
</html>

As far as your second question, you could use the same class for your validations. You are only able to use unique id's for an element within each page, so if you have a lot of email validations, you would have to repeat the script multiple times. It isn't best practice to repeat code, so I would just use a single class for all of them.

No problem.