簡體   English   中英

一種形式2提交按鈕2動作

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM