-1

I have two forms in a page. And I need the form to be submit on single click.

i use below code:

form:

<?php echo $this->Form->create('Payment', array('id' => 'addToDB', 'type' => 'post')); ?>
<?php echo $this->Form->input('merID'); ?>
<?php echo $this->Form->input('card_number', array('label'=>false, 'min'=>16, 'type'=>'select')); ?>
<?php echo $this->Form->input('name_on_card', array('label'=>false, 'type'=>'select')); ?>
<?php echo $this->Form->input('security_code', array('label'=>false, 'type'=>'select')); ?>
<?php $options = array
    (
    'label' => 'Submit',
    'id' => 'submit',
    'div' => array(
    'class' => 'glass-pill',
    )
); 
?>
<?php echo $this->Form->end($options); ?>

<?php echo $this->Form->create('Payment', array('id' => 'paymentGateway', 'type' => 'post', 'url' => 'https://uat.pbbank.com/payment/dpayment.jsp')); ?>
<?php echo $this->Form->input('merID'); ?>
<?php echo $this->Form->input('card_number', array('label'=>false, 'min'=>16, 'type'=>'select')); ?>
<?php echo $this->Form->input('name_on_card', array('label'=>false, 'type'=>'select')); ?>
<?php echo $this->Form->input('security_code', array('label'=>false, 'type'=>'select')); ?>
<?php $options = array
    (
    'label' => 'Submit',
    'id' => 'submit2',
    'div' => array(
    'class' => 'glass-pill',
    )
); 
?>
<?php echo $this->Form->end($options); ?>

javascrip:

$(document).ready(function () {
        $("#submit2").click(function () {
            $.post($("#addToDB").attr("action"), $("#addToDB").serialize(),
              function () {
                  alert('Add to Database submitted');
              });

            $.post($("#paymentGateway").attr("action"), $("#addToDB").serialize(),
              function () {
                  alert('Payment Gateway submitted');
              });
        });
    });

it's work fine on chrome, but not mozilla. Can someone please help me. Thanks in advance.

dancingAngel
  • 67
  • 3
  • 14
  • First of all, you use `$("#addToDB").serialize()` in second `$.post` instead of `$("#paymentGateway").serialize()`. Could you provide forms HTML and errors you get? – Zudwa Apr 17 '14 at 04:55
  • @Zudwa i already update my question. I didnt get any error. seems like only 1 form was submitted. – dancingAngel Apr 17 '14 at 05:04
  • You are trying to make ajax request to another domain. This is restricted due to security reasons. You can use a JSONP request in order to make it work. Note that you can only make GET request, not POST. And don't forget to prevent default event on click in `.click()`, otherwise in addition to ajax calls your forms are submitted normally. You can read about making JSONP request here [link](http://stackoverflow.com/questions/6809053/simple-jquery-php-and-jsonp-example). – Zudwa Apr 17 '14 at 05:40
  • @Zudwa hi, i try to understand what you explain. But i didnt get it. I've never use JSONP before. can you please give me the example. – dancingAngel Apr 17 '14 at 07:04
  • Is uat.pbbank.com your domain? I mean, are you developing it's backend? – Zudwa Apr 17 '14 at 07:06
  • @Zudwa nope. that is for the payment. i want to save the data to my database, and also to sent the data to that url. – dancingAngel Apr 17 '14 at 07:16
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/50854/discussion-between-zudwa-and-dancingangel) – Zudwa Apr 17 '14 at 08:54
  • possible duplicate of [Cakephp Multiple Forms 1 Submit button](http://stackoverflow.com/questions/23078573/cakephp-multiple-forms-1-submit-button) – Fury Apr 17 '14 at 16:36
  • nope. it's different question. @IsaacRajaei that is my question. and i already got the answer there. but the code is working fine on IE, chrome, opera. but not mozilla. thats y im asking now HERE – dancingAngel Apr 18 '14 at 01:10

1 Answers1

0

Your solution is very poor.

  1. you just need one form
  2. You will need to wrap(serialize) your form[paymentGateway] and
  3. send the request to Payment Gateway
  4. then when you get the result OK
  5. save your data into Database

    $("#submit2").click(function () {
       //wrap your form once
       $data = $("#addToDB").serialize();
    
        $.post($("#paymentGateway").attr("action"), $data,
          function (firstresult) {
              //if Payment Gateway result is correct 
              if (firstresult==OK){
                   //then save it into Database
                   $.post($("#addToDB").attr("action"), $data,
                   function (secondresult) {
                          if (secondresult) {
                                alert('Payment Successful');
                          }
                    });
              }
          });
    });
    
Fury
  • 4,643
  • 5
  • 50
  • 80