0

好的,我对PHP一无所知。我是新手,我设法根据自己的需要调整代码,但有些事情我无法完成。我有这个联系表,PHP 看起来像这样:

<?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();
}
?>

该表单有四个必填字段 $naam、$email、$onderwerp 和 $bericht。单击提交按钮并且其中任何一个未填写后,都会出现错误。到目前为止一切顺利,但它在新窗口中发布错误,覆盖了联系人。我希望将错误消息显示在表单本身上。我只是不知道该怎么做,也找不到可以在我的代码中实现的在线解决方案。

你们中的任何人都可以对此有所了解吗?

4

2 回答 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;
于 2012-04-24T08:46:39.910 回答
0

You might like to make the following changes:

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

to

<?php echo $myError; ?>

于 2012-04-24T08:45:13.547 回答