Question

I want to create 2 forms in a view. Only 1 form is visible. The another one is hidden.

Hence, I want to have only 1 submit button for both form.

Code:

<?php echo $this->Form->create('Payment', array('type' => 'post', 'url' => 'https://uat.pbbank.com/payment/dpayment.jsp')); 
echo $this->Form->hidden('merchant_id', array('value'=>'3242', 'name'=>'merchant_id', 'id'=>'merchant_id')); 
echo $this->Form->end(__('Submit')); ?>

<?php echo $this->Form->create('Payment'); 
echo $this->Form->hidden('merchant_id', array('value'=>'3242', 'name'=>'merchant_id', 'id'=>'merchant_id')); 
echo $this->Form->end(__('Submit')); ?>
Was it helpful?

Solution

The best way to achieve this would be with Ajax, or JS/Jquery.

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript">
    $(document).ready(function () {
        $("#submit").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');
              });
        });
    });
</script>

The form:

<form id="addToDB" action="addtodatabase.php" method="post">
    <input type="button" id="submit" value="Submit" />
</form>

<!-- Hidden form for payment gateway -->
<form id="paymentGateway" action="paymentgateway.php" method="post">
    <!-- Payment gateway input fields -->
</form>

Clicking the button with id="submit" calls function to post both forms by form id.

OTHER TIPS

you better wrap your form data and send your request with ajax to (https://uat.pbbank.com/payment/dpayment.jsp) and your action normal click event.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top