2015-07-13 51 views
1

我有一個表單。表格有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; 
    } 

,但我不能得到價值?有人知道我犯錯的地方嗎?

+0

您是否在控制檯中發現任何錯誤? - 你的「submitButton2」在哪裏?我可以只有1 – Epodax

+0

大概你已經添加了' MaggsWeb

+0

是的,我添加了第二個按鈕id爲submitButton2,我發送send_mail.php應該發送郵件。它的輸入像submitbutton1 – KondukterCRO

回答

0

我改變了類型得到,現在它的工作。

$body .= "\nFirst Name: ".$_GET['firstName']; 

因此,要在2表單上添加2個動作,只需添加第二個按鈕並將其發佈到php。

0

點擊提交按鈕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> 
+0

沒有任何反應。沒有控制檯錯誤,並沒有發生。 – KondukterCRO

+0

你確定*#submitButton2'。點擊*事件被觸發?嘗試提醒某事並在此處發佈結果。 –

+0

抱歉沒有注意到您正在使用一個提交按鈕和按鈕。所以這不會是一個問題。但是,這應該工作。您正在使用第二個選項的按鈕。因此,只需將* $('#bookingForm')。submit(); *添加到您的代碼中即可。 –