我正在使用Contact Form 7 with ajax jquery
. 第一部分供用户插入vehicle number
和email
.
如果成功,它将通过ajax加载联系人表单7。当我点击提交时,它将重定向到一个新页面,并将输出0。我在谷歌上做了一项研究,发现wpcf7-submit
作用
我尝试创建一个警报函数,它会按预期进行响应。如果表单上没有错误,我想如何将其与联系人表单7一起使用并通过电子邮件发送表单详细信息?
Coding
表单。php<?php
get_header();
// Include all the theme functions
?>
<form name="season-form" class="form-horizontal" method="post" action="">
<div class="form-group form-group-lg">
<label for="carNo" id="label-cust"> Vehicle Registration No</label>
<input type="text" class="form-control" name="vehicle_no" id="vehicle_no" placeholder="BHK1991">
</div>
<div class="form-group form-group-lg">
<label for="email" id="email">Email</label>
<input type="email" class="form-control" name="email" id="email" placeholder="[email protected]">
</div>
<button type="submit" class="btn btn-primary" id="submit_payment">Make Payment</button>
</form>
<div id="result"></div>
<?php get_footer(); ?>
功能。phpfunction _myConfirmHandler()
{
if(isset($_POST[\'vehicle_no\']))
{
$vehicle_no = $_POST[\'vehicle_no\'];
$email = $_POST[\'email\'];
echo do_shortcode(\'[cfdb-table form="season parking form_copy" show="your-name,mail,your-platno,your-location" filter="your-platno=\' . $vehicle_no . \'" headers="your-name=Name,mail=Email,your-platno=Registration No.,your-location=Parking Location"]\');
// Display the Form
echo do_shortcode(\'[contact-form-7 id="6959" title="Payment season Form_Post"]\');
}
wp_die();
}
add_action(\'wp_ajax_confirmRequest\', \'_myConfirmHandler\');
add_action(\'wp_ajax_nopriv_confirmRequest\', \'_myConfirmHandler\');
jquery。js公司
jQuery(document).ready(function($) {
$(\'#submit_payment\').click(function(e){
e.preventDefault();
var str = $("form[name=season-form]").serialize();
//alert(str);
$.ajax({
type: "POST",
url: \'//www.ktmparking.com.my/wp-admin/admin-ajax.php\',
data: str + \'&action=confirmRequest\'
}).done(function(data){
$("#result").html(data);
$(".cal").datepicker({
showOn: "button",
dateFormat: "dd-mm-yy",
buttonImage: "http://theonlytutorials.com/demo/x_office_calendar.png",
buttonImageOnly: true,
showAnim: \'slideDown\',
duration: \'fast\',
showButtonPanel: true
});
$(\'.wpcf7-submit\').click(function(e) {
e.preventDefault();
alert(\'Submit button press!\');
});
});
});
});
如果成功,我是否必须重写表单上的post方法并通过电子邮件发送详细信息?
最合适的回答,由SO网友:BlueSuiter 整理而成
您一直在加载CF-7 表单使用ajax
也就是说,表单所需的脚本从未为此执行过。而且,由于这件事,你的表格没有按预期工作。
你必须re-initiate
的脚本CF-7 对于特定表单。这里是更新的PHP代码。
现在,您的ajax将是:
<?php
function _myConfirmHandler()
{
if(isset($_POST[\'vehicle_no\']))
{
$vehicle_no = $_POST[\'vehicle_no\'];
$email = $_POST[\'email\'];
echo do_shortcode(\'[cfdb-table form="season parking form_copy" show="your-name,mail,your-platno,your-location" filter="your-platno=\' . $vehicle_no . \'" headers="your-name=Name,mail=Email,your-platno=Registration No.,your-location=Parking Location"]\');
// Display the Form
echo do_shortcode(\'[contact-form-7 id="6959" title="Payment season Form_Post"]\');
?>
<script>
jQuery(\'.wpcf7 > form\').initForm();
var urL = jQuery(\'.wpcf7 > form\').attr(\'action\').split(\'#\');
jQuery(\'.wpcf7 > form\').attr(\'action\', "#" + urL[1]);
</script>
<?php
}
wp_die();
}
?>
Update:自CF7 5.4起,initForm不再存在
非常感谢。@Chris Pink