网站优化

网站优化

Products

当前位置:首页 > 网站优化 >

Bootstrap模式中的WordPress自定义联系表单未显示验证和提交响应

GG网络技术分享 2025-03-18 16:12 3


问题描述:

For my WordPress I am using Bootstrap and built a Custom Contact form with below HTML (followed this tutorial: https://premium.wpmudev.org/blog/how-to-build-your-own-wordpress-contact-form-and-why/).

<form id=\"contact-form\" action=\"<?php echo get_site_url(); ?>\" method=\"post\">

<div class=\"modal-body\">

<?php echo $response; ?>

<div class=\"form-group\">

<input class=\"form-control my-2\" type=\"text\" name=\"message_name\" size=\"50\" placeholder=\"Your full name\" value=\"<?php echo esc_attr($_POST[\'message_name\']); ?>\">

<input class=\"form-control my-2\" type=\"email\" name=\"message_email\" size=\"50\" placeholder=\"Email address\" value=\"<?php echo esc_attr($_POST[\'message_email\']); ?>\">

<input class=\"form-control my-2\" type=\"tel\" name=\"message_tel\" size=\"50\" placeholder=\"Country code, Phone number\" value=\"<?php echo esc_attr($_POST[\'message_tel\']); ?>\">

<textarea class=\"form-control my-2\" name=\"message_text\" rows=\"2\" placeholder=\"Your message\" value=\"<?php echo esc_attr($_POST[\'message_text\']); ?>\"></textarea>

<input class=\"form-control my-2\" type=\"text\" name=\"message_human\" placeholder=\"Human check: Enter 2\">

<input type=\"hidden\" name=\"message_url\" value=\"<?php the_permalink(); ?>\">

<input type=\"hidden\" name=\"message_page\" value=\"<?php the_title(); ?>\">

</div>

</div>

<div class=\"modal-footer\">

<input type=\"hidden\" name=\"submitted\" value=\"1\">

<button type=\"submit\" value=\"Submit\" class=\"btn btn-search form-control\">Send Enquiry</button>

</div>

Below the function to validate the Forms & show responses while Form submission:

//response generation function

$response = \"\";

//function to generate response

function contact_g_form_response($type, $message) {

global $response;

if ($type == \"success\") {

$response = \"<div class=\'message-success text-center\'>{$message}</div>\";

} else {

$response = \"<div class=\'message-error text-center\'>{$message}</div>\";

}

}

//response messages

$not_human = \"Enter current year in numbers.\";

$missing_content = \"Missing something.\";

$email_invalid = \"Check your Email address.\";

$message_unsent = \"Message was not sent. Try Again.\";

$message_sent = \"Thanks! We got your enquiry.\";

//user posted variables

$name = $_POST[\'message_name\'];

$email = $_POST[\'message_email\'];

$fromEmail = $name . \'<\' . $email . \'>\';

$tel = $_POST[\'message_tel\'];

$text = $_POST[\'message_text\'];

$url = $_POST[\'message_url\'];

$page = $_POST[\'message_page\'];

$human = $_POST[\'message_human\'];

//php mailer variables

$to = get_option(\'admin_email\');

$subject = \'[General Enquiry] \' . $name . \' | Phone Number:\' . $tel;

$headers = \'From: \' . $fromEmail . \"

\" .

\'Reply-To: \' . $email . \"

\";

$message = \'<html><body><h1>New general enquiry from \' . $name . \'!</h1>\'

. \'<p>Email: \' . $email . \'</p>\'

. \'<p>Phone Number: \' . $tel . \'</P>\'

. \'<p>Inquiry: \' . $text . \'</P>\'

. \'<p>From page: <b>\' . $page . \'</b></P>\'

. \'<p>Page URL: \' . $url . \'</p>\'

. \'</body></html>\';

if (!$human == 0) {

if ($human != 2) {

contact_g_form_response(\"error\", $not_human); //not human!

} else {

//validate email

if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {

contact_g_form_response(\"error\", $email_invalid);

} else { //email is valid

//validate presence of name, phone number

if (empty($name) || empty($tel)) {

contact_g_form_response(\"error\", $missing_content);

} else { //ready to go!

$sent = wp_mail($to, $subject, $message, implode(\"

\", $headers)); //mail to admin - striptags removing formatting

// $sent2 = wp_mail($email, $subject, $body, $headers); //mail to visitor

// if ($sent || $sent2) {

if ($sent) {

contact_g_form_response(\"success\", $message_sent); //message sent!

} else {

contact_g_form_response(\"error\", $message_unsent); //message wasn\'t sent

}

}

}

}

} else if ($_POST[\'submitted\']) {

contact_g_form_response(\"error\", $missing_content);

}

Although the Form is being successfully submitted, no response / validation messages are shown as per contact_g_form_responsefunction and after the Form submission, it is being redirected to a random WordPress post.

网友观点:

You Just Need To replace this:

<form id=\\\"contact-form\\\" action=\\\"<?php echo get_site_url();?>\\\" method=\\\"post\\\">

With This:

<form id=\\\"contact-form\\\" method=\\\"post\\\">

Because You are displaying the error on the same page but on click of the submit button the action is taking you to a specific url and that\'s the reason the validations are not shown.

And This:

if ($type == \\\"success\\\") {

$response = \\\"<div class=\'message-success text-center\'>{$message}</div>\\\";

} else {

$response = \\\"<div class=\'message-error text-center\'>{$message}</div>\\\";

}

With This also:

if ($type == \\\"success\\\") {

$response = $message;

echo $response;

} else {

$response = $message;

echo $response;

}

标签:

提交需求或反馈

Demand feedback