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 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)