[英]One form 2 submit buttons 2 actions
我有一个表格。 表单有1个动作和1个提交按钮。 如果我按提交,它将进行验证,如果一切正常,它将数据发送到第三方站点,并在该站点上完成卡付款,并将邮件发送给已进行交易的站点的所有者。 现在,我想添加另一个仅发送电子邮件的提交按钮。 我有这样的形式:
<form name="bookingForm" class="cmxform" id="bookingForm" method="post" action="third-party-site.php" accept-charset="utf-8">
然后我有诸如姓名,电子邮件等字段。
并提交按钮:
<input type="submit" value="PROCEED TO BOOKING" name="submit1" id="submitButton" class="btn btn-info pull-right" title="Click here to submit!"/>
所以我尝试了这个:
$('#submitButton2').click(function(){
$('form[name=bookingForm]').setAttrib('action','send_mail.php');
$('form[name=bookingForm]').submit();
});
和这个:
$('#submitButton2').click(function(){
$('#bookingForm').attr('action', 'send_mail.php');
});
但没有任何效果。 因此,如果有人可以帮助我,我将不胜感激。
现在,我添加了可对提交点击进行验证的脚本,并添加了发送邮件的process.php。 验证是可以的,但是当需要继续进行process.php时,会出现错误,因为它不断在process.php的路径前面添加域名。 Process.php位于主题文件夹内,我定义了类似于publichtml / pagename.com / wp / content / themes / mytheme / process.php的路径,并且在控制台中表示找不到页面http://www.example.com /publichtml/pagename.com/wp/content/themes/mytheme/process.php 。
现在我打电话给ajax,所以它在另一个按钮上发送电子邮件。
$(document).ready( function() {
$('input[placeholder], textarea[placeholder]').placeholder();
$('#submit2').removeAttr('disabled');
$('#bookingForm').validate({
debug: true,
//submitHandler: ajaxSubmit
rules: {
},
messages: {
first_name: "First name field required.",
last_name: "Last name field required.",
email: {
required: "Email address required",
email: "Email address must be in the format name@domain.com."
}
}
});
$('#submit2').click( function() {
if( $('#bookingForm').valid() ) {
ajaxSubmit();
}
else {
$('label.error').hide().fadeIn('slow');
}
});
});
function ajaxSubmit() {
$('#bookingForm').attr('disabled', 'disabled');
var firstName = $('#first_name').val();
var lastName = $('#last_name').val();
var email = $('#email').val();
var data = 'firstName=' +firstName+ '&lastName=' +lastName+ '&email=' +email;
$.ajax({
url: '<?php bloginfo('template_directory'); ?>/process.php',
type: 'POST',
dataType: 'json',
data: data,
cache: false,
success: function(response) {
if( response.error != 'true' ) {
$('#loading, #bookingForm, .message').fadeOut('slow');
$('#response').html('<h3>Message sent successfully! </h3>').fadeIn('slow');
}
else {
$('#loading').fadeOut('slow');
$('#submit2').attr('disabled', 'disabled');
$('#response').html('<h3>There was a problem sending mail!</h3>').fadeIn('slow');
}
},
error: function(jqXHR, textStatus, errorThrown) {
$('#loading').fadeOut('slow');
$('#submit2').removeAttr('disabled');
$('#response').text('Error Thrown: ' +errorThrown+ '<br>jqXHR: ' +jqXHR+ '<br>textStatus: ' +textStatus ).show();
}
});
return false;
}
和我的process.php:
<?php
sleep(2);
define('TO', 'example.mail@mail.com');
define('FROM', $_POST['email']);
define('SUBJECT', 'inquiry');
$isValid = true;
$return = array("error" => "false");
if( $_SERVER['REQUEST_METHOD'] == 'POST' ) {
global $return;
if ( sendEmail() ) {
//return "Message sent successfully!";
echo json_encode($return);
}
else {
echo json_encode($return);
}
}
function sendEmail() {
global $return;
$body = "";
$body .= "From: ".$_POST['email']."\nSubject: " . 'inquiry';
$body .= "\nFirst Name: ".$_POST['first_name'];
$body .= "\nLast Name: " .$_POST['lastName'];
if ( @mail( TO, 'inquiry', $body ) ) {
//if(true) {
$return['error'] = 'false';
}
else {
$return['error'] = 'true';
}
return $return;
}
但我无法获得价值? 有人知道我在哪里犯错了吗?
单击提交按钮2时,将在更新操作之前提交表单。 因此,两个按钮的结果将相同。 您可以使用preventDefault(); 以防止点击时提交表单。 这是一个工作代码:
<form action="" method="post" id="bookingForm">
<input type="submit" value="Payment" name="s1" id='s1'>
<input type="submit" value="Email" name="s2" id='s2'>
</form>
<script type="text/javascript" src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
<script type="text/javascript">
$(function() {
$('#s2').click(function(e){
alert('hi');
e.preventDefault();
$('#bookingForm').attr('action', 'send_mail.php');
$('#bookingForm').submit();
});
});
</script>
我将类型更改为get,现在可以使用了。
$body .= "\nFirst Name: ".$_GET['firstName'];
因此,要对1种表单进行2次操作,只需添加第二个按钮并将其发布到php。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.