我有一個表單。表格有1個動作和1個提交按鈕。如果我按提交進行驗證,如果一切正常,它將數據發送到第三方網站,並在該網站上進行卡支付,並將郵件發送給已完成交易的網站的所有者。現在我想添加另一個只發送電子郵件的提交按鈕。 我有形式是這樣的:一個表單2提交按鈕2個動作
<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 [email protected]"
}
}
});
$('#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', '[email protected]');
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;
}
,但我不能得到價值?有人知道我犯錯的地方嗎?
您是否在控制檯中發現任何錯誤? - 你的「submitButton2」在哪裏?我可以只有1 – Epodax
大概你已經添加了' MaggsWeb
是的,我添加了第二個按鈕id爲submitButton2,我發送send_mail.php應該發送郵件。它的輸入像submitbutton1 – KondukterCRO