Stephanie wrote:
I tried to create an email/contact form but i'am stuck. (i'am a php-newbie)
Let me explain (sorry for my bad english, it's not my native language)
I have a form with the required fields and the necessary php-code.
There's also some code to validate if a field is empty or there's an invalid
email address.
But how can i display error messages (e.g. empty field), in this (same)
form. (e.g. at the top of the page)
I searching fore several day's but can't find an example.
Something like:
<?php
$display_form = true;
function validate_data()
{
// Here is the code that validates
// all the data from post and if
// there are some errors, then it proper
// error messages and/or other error flags.
// It returns true if data is valid and
// false if not.
}
if (!empty($_POST['go']) && validate_data())
{
// Do data processing (eg. send mail).
// If something goes wrong, then set proper
// error messages and/or error flags and.
// If everything is OK, then set $display_form
// to false, display some confirmation message
// and/or redirect to page which confirms
// finishing the process.
}
if ($display_form)
{
// Here show error messages if are set.
// And show the form (you can use some error
// flags to point the fields which are not
// filled properly). Use data from $_POST
// (or $_GET) to fill the "value" attributes
// of <input>, to display the data which
// the user allready filled in.
// Put some hidden value named "go", or use
// this name on "submit" button in the form
// so the expression "empty($_POST['go'])"
// above would evaluate to "true" when the
// form is submited (or resubmited).
}
?>
You can split the file above and use "include".