Cakephp Multiple Forms 1 Submit button

五迷三道 提交于 2019-12-23 03:52:12

问题


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')); ?>

回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/23078573/cakephp-multiple-forms-1-submit-button

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!