index.php?
But the form action is thankyou.php.
The form action should be whatever script processes the form.
If you were to follow the principle of “separation of concerns” all the scripting and form processing would be in a controller script and the “pages” would be different html templates included depending on various conditions. Eg, first the user would see the form html, what comes next depends on the results of the form processing, if there is an error, they may be returned to the form, with some additional error message telling them where they went wrong. Only if the validation is successful, are they forwarded to the “Thank you” page.
Though this is another biggish subject and may confuse at this stage (although it should make things simpler when understood).
The (big) problem here is, the script has no output, so will appear to do nothing.
Supposing someone does not fill in their name, the string 'Please enter your name' gets assigned to the variable $name_error, but then what do you do with that variable? Nothing.
Should the error message not be echoed out somewhere?
Any script should return some result according to what happens.
If you take a look at the example code that is being used for reference, it does appear to be legacy code. Not just judging by the php, but the html and css too.
It’s all to easy for a novice to pick up bad ideas form the wrong places without being aware of it, I’ve been there. Unfortunately, I can’t think of a good form validation tutorial off hand just now.
Because index.php is the form action, that is where the processing will have to take place, so it’s all done in one file in this instance.
The downside to this is the user will always get returned to the form regardless of the outcome. So on successful completion, they see the form again, not the “Thank You” page. This should be enough to give you a start and get you up and running though.
But by using a controller script and splitting the html away to separate templates, you can have more control over the outcome.
This is a basic, bare-bones example to illustrate that logic.
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") { // The form is submitted
// do the validation here
// Then, depending on how that turns out...
if(isset($errors)) { // There are errors in the submission (example if condition)
// Show the user the form again, with errors listed
include 'html_form_template.php' ;
}
else { // Everything is cool with the form submission
// Send the email
// Record to database Etc...
// Show the user Thank You message
include 'html_thankyou_template.php' ;
}
} // end the form is submitted
else { // Nothing has been submitted yet
// Show the user the form for the first time
include 'html_form_template.php' ;
}
?>
Though you may not want to confuse things with that at this stage.
It’s there on my code, that must of been an error copying and pasting within the nano text editor in the terminal. I’m sshing into my machine to edit the code you see