简体   繁体   English

一种形式2提交按钮2动作

[英]One form 2 submit buttons 2 actions

I have a form. 我有一个表格。 Form has 1 action and 1 submit button. 表单有1个动作和1个提交按钮。 If I press submit it makes validation and if everything is ok it sends data to third-party site and card payment is done on that site and mail is sent to owner of the site that transaction has been made. 如果我按提交,它将进行验证,如果一切正常,它将数据发送到第三方站点,并在该站点上完成卡付款,并将邮件发送给已进行交易的站点的所有者。 Now I want to add another submit button that only send email. 现在,我想添加另一个仅发送电子邮件的提交按钮。 I have form like this: 我有这样的形式:

<form name="bookingForm" class="cmxform" id="bookingForm" method="post"  action="third-party-site.php" accept-charset="utf-8">

and then I have fields like name, e-mail etc. 然后我有诸如姓名,电子邮件等字段。

and submit button: 并提交按钮:

<input type="submit" value="PROCEED TO BOOKING" name="submit1" id="submitButton" class="btn btn-info pull-right" title="Click here to submit!"/>

So I tried this: 所以我尝试了这个:

$('#submitButton2').click(function(){
$('form[name=bookingForm]').setAttrib('action','send_mail.php');
$('form[name=bookingForm]').submit();
});

and this: 和这个:

$('#submitButton2').click(function(){
$('#bookingForm').attr('action', 'send_mail.php');
});

but nothing works. 但没有任何效果。 So if somebody can help me I would appreciate it. 因此,如果有人可以帮助我,我将不胜感激。

Now i added script that validate on submit click and I added process.php which sends mail. 现在,我添加了可对提交点击进行验证的脚本,并添加了发送邮件的process.php。 Validations is ok but when it needs to be proceeded to process.php it gives an error because it constantly adds domain name in front of path to process.php. 验证是可以的,但是当需要继续进行process.php时,会出现错误,因为它不断在process.php的路径前面添加域名。 Process.php is located inside theme folder and I defined path to like publichtml/pagename.com/wp/content/themes/mytheme/process.php and in console it says page can not be found http://www.example.com/publichtml/pagename.com/wp/content/themes/mytheme/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


Now i made ajax call so it sends email on another button. 现在我打电话给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;
}

and my process.php : 和我的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;
    }

but I can not get values? 但我无法获得价值? Anybody knows where I made mistake? 有人知道我在哪里犯错了吗?

While clicking on the Submit Button 2, the form will be submitted before updating the action . 单击提交按钮2时,将在更新操作之前提交表单。 So the result would be Same Action Page for both the buttons. 因此,两个按钮的结果将相同。 You can use the preventDefault(); 您可以使用preventDefault(); to prevent the form being submitted on click. 以防止点击时提交表单。 Here is a working code: 这是一个工作代码:

<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>

I changed type to get and now it works. 我将类型更改为get,现在可以使用了。

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

So to have 2 action on 1 form just add second button and post it to php. 因此,要对1种表单进行2次操作,只需添加第二个按钮并将其发布到php。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM