dongyounai6281 2015-07-13 07:36
浏览 61
已采纳

一个表单2提交按钮2动作

I have a form. Form has 1 action and 1 submit button. 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. 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 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.


Now i made ajax call so it sends email on another button.

$(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 :

<?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']."
Subject: " . 'inquiry';
    $body .= "
First Name: ".$_POST['first_name'];
    $body .= "
Last 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?

  • 写回答

2条回答 默认 最新

  • duanjupiao4730 2015-07-15 07:35
    关注

    I changed type to get and now it works.

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

    So to have 2 action on 1 form just add second button and post it to php.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝