我有一个联系表,当有人填写字段并单击“发送”时,该表会向我发送电子邮件。
现在,我需要一个类似“谢谢”的页面,在该页面上,我需要加载一段代码来对其进行跟踪。
这是我现在拥有的代码:
<form id="contact-form" method="POST" action="contact.php" role="form" class="wpcf7">
这是按钮:
<div class="col-md-12">
<a href="#" onclick="ga('send', 'event', 'contact', 'button-verstuur')";>
<input type="submit" class="btn btn-success btn-send wpcf7-submit" value="Verstuur">
</a>
</div>
我想做的是在有人单击“发送”按钮后重定向页面。但仅当所有必填字段均已填写时,当前它会检查是否已填写必填字段。因此,仅当填写了必填字段后,重定向到“谢谢”页面才有效。
希望有人可以帮助我解决这个问题。
编辑:
这是PHP代码
// configure
$from = 'Contactform <myform@forming.com>';
$sendTo = '<sendto@myself.com>';
$subject = 'New message';
$fields = array('name' => 'Naam', 'surname' => 'Achternaam', 'phone' => 'Telefoonnummer', 'email' => 'Email', 'message' => 'Bericht'); // array variable name => Text to appear in email
$okMessage = 'Your message is sent successfully!';
$errorMessage = 'Error, please try again later.';
// let's do the sending
if(isset($_POST['url']) && $_POST['url'] == '') try
{
$emailText = "Uw heeft een nieuw bericht\n=============================\n";
foreach ($_POST as $key => $value) {
if (isset($fields[$key])) {
$emailText .= "$fields[$key]: $value\n";
}
}
mail($sendTo, $subject, $emailText, "From: " . $from);
$responseArray = array('type' => 'success', 'message' => $okMessage);
}
catch (\Exception $e)
{
$responseArray = array('type' => 'danger', 'message' => $errorMessage);
}
if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
$encoded = json_encode($responseArray);
header('Content-Type: application/json');
echo $encoded;
}
else {
echo $responseArray['message'];
}
HTML表单:
<form id="contact-form" method="POST" action="contact.php" role="form" class="wpcf7">
<div class="messages"></div>
<div class="controls">
<div class="form-group">
<label for="form_name"><span class="required">*</span> Naam:</label>
<span class="wpcf7-form-control-wrap your-name">
<input id="form_name" type="text" name="name" class="wpcf7-text" required="required" data-error="Uw naam is verplicht">
</span>
<div class="help-block with-errors"></div>
</div>
<div class="form-group">
<label for="form_email"><span class="required">*</span> E-mail:</label>
<span class="wpcf7-form-control-wrap your-email">
<input id="form_email" type="email" name="email" class="wpcf7-text" required="required" data-error="Een geldig e-mailadres is verplicht">
</span>
<div class="help-block with-errors"></div>
</div>
<div class="form-group">
<label for="form_phone"><span class="required">*</span> Telefoonnummer:</label>
<span class="wpcf7-form-control-wrap your-url">
<input id="form_phone" type="text" name="phone" class="wpcf7-text">
</span>
<div class="help-block with-errors"></div>
</div>
<div class="form-group">
<label for="form_message"><span class="required">*</span> Bericht:</label>
<span class="wpcf7-form-control-wrap your-message">
<textarea id="form_message" name="message" class="wpcf7-textarea" rows="4" required="required" data-error="U moet een bericht invullen"></textarea>
</span>
<div class="help-block with-errors"></div>
</div>
<div class="form-group antispam">
<label for="form_phonee"><span class="required">*</span> Veld leeg laten:</label>
<span class="wpcf7-form-control-wrap your-url">
<input id="form_phonee" type="text" name="url" class="wpcf7-text">
</span>
<div class="help-block with-errors"></div>
</div>
<div class="col-md-12"><a href="#" onclick="ga('send', 'event', 'contact', 'button-verstuur')";>
<input type="submit" class="btn btn-success btn-send wpcf7-submit" value="Verstuur"></a>
</div>
</div>
</form>
答案 0 :(得分:0)
最好的选择是检查每个变量是否为空。如果有错误,请向您的HTML加载错误,否则继续执行您的代码 例如:
$form_nameErr = $form_emailErr = $form_phoneErr = $form_messageErr = $form_phoneeErr = "";
if(isset($_POST['url']) && $_POST['url'] == '') try
{
$emailText = "Uw heeft een nieuw bericht\n=============================\n";
if(!empty($_POST['form_name'])){
$emailText .= $_POST['form_name']."\n";
}else{
$form_nameErr = "Form Name required";
}
if(!empty($_POST['form_email'])){
$emailText .= $_POST['form_email']."\n";
}else{
$form_emailErr = "Email Text required";
}
}
if(!empty($form_nameErr) && !empty($form_emailErr) && !empty($form_phoneErr) && !empty($form_messageErr) )){
mail($sendTo, $subject, $emailText, "From: " . $from);
//TO redirect
header("Location: http://www.yourwebsite.com/thankyou.php");
exit();
$responseArray = array('type' => 'success', 'message' => $okMessage);
}else{
//load you html with error variables
}
您的html应包含
<div class="form-group">
<label for="form_name"><span class="required">*</span> Naam:</label>
<span class="wpcf7-form-control-wrap your-name">
<input id="form_name" type="text" name="name" class="wpcf7-text" required="required" data-error="Uw naam is verplicht">
</span>
<span class="error">* <?php echo $nameErr;?></span>
<br><br>
</div>
定义错误样式 请参阅:https://www.w3schools.com/php/php_form_required.asp