0

Ok, I'm pretty clueless when it comes to PHP. I'm new to it and I manage to adjust code to my needs but there is something that I just can't get done. I have this contact form and the PHP looks like this:

<?php
/* Set e-mail recipient */
$myemail  = "";

/* Check all form inputs using check_input function */
$naam = check_input($_POST['naam'], "Vul uw naam in");
$bedrijf  = check_input($_POST['bedrijf']);
$email  = check_input($_POST['email'], "Vul uw emailadres in");
$telefoonnummer  = check_input($_POST['telefoonnummer']);
$onderwerp = check_input($_POST['onderwerp'], "Vul een onderwerp in");
$bericht = check_input($_POST['bericht'], "Stel een vraag of plaats een opmerking");

/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email))
{
show_error("Vul een geldig e-mailadres in");
}

/* Let's prepare the message for the e-mail */
$message = "Beste,

Een bezoeker heeft een bericht gestuurd:

Naam: $naam
E-mail: $email

Onderwerp: $onderwerp
Bericht:
$bericht

Met vriendelijke groet,
$naam
";

/* Send the message using mail() function */
mail($myemail, $onderwerp, $message);

/* Redirect visitor to the thank you page */
header('Location: thanks.htm');
exit();

/* Functions we used*/ 
function check_input($data, $problem='')
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if ($problem && strlen($data) == 0)
{
    show_error($problem);
}
return $data;
}

function show_error($myError)
{
?>
<html>
<body>

<b>Please correct the following error:</b><br />
<?php echo $myError; ?>

</body>
</html>
<?php
exit();
}
?>

The form has four required fields $naam, $email, $onderwerp and $bericht. After clicking the submit button and any of those aren't filled in, it gives an error. So far so good but it posts the error in a new window, overwriting the contacform. I want the error messages to be displayed on the form itself. I just don't see how to do it and I couldn't find a good answer to this matter online that I could implement in my code.

Any of you could shine a light on this?

1
  • 1
    You mean without reloading? Then you need some client-side validation with javascript. But please be aware that you should always validate your data on the server too, because javascript can easily be turned of. Commented Apr 24, 2012 at 8:36

2 Answers 2

1

You need to post the HTML form back to itself. Then at the top of the form page, you need an if statement that does a check like this:

if(isset($_POST['submit'])) {
 //do validation
 //$errorMessage = "whatever message you want to show the user"
 if ($errors == 0){
  /* Redirect visitor to the thank you page */
  header('Location: thanks.htm');
  exit();
 }
}

and when displaying the form:

if(isset($errorMessage))
  print $errorMessage;
Sign up to request clarification or add additional context in comments.

Comments

0

You might like to make the following changes:

alert(<?php echo $myError; ?>);

to

<?php echo $myError; ?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.