I have 2 forms one ordinary

<form id="payment" name="payment" method="post" action="https://sci.interkassa.com/" enctype="utf-8" target="_blank"> <input type="hidden" name="ik_co_id" value="hdjfhjsdjsdhkj7833jfjsdhf"/> <input type="hidden" name="ik_pm_no" value="ID_<?= rand(1000, 9999) ?>"/> <input type="hidden" name="ik_am" value="<?= $game->price ?>"/> <input type="hidden" name="ik_cur" value="UAH"/> <input type="hidden" name="ik_desc" value="<?= $game->name ?>"/> </form> 

and one ActiveForm

  <?php $form = ActiveForm::begin() ?> <div class="buymail"><?= $form->field($model, 'email')->input('email')->label("E-mail *") ?></div><div class="checkaccept"> <?= $form->field($model, 'accept') ->checkbox([ 'label' => 'Я ознакомлен с пользовательским соглашением и описанием товара*', ]) ?></div> <div class="buybtn"><?= Html::submitButton('Перейти к оплате →', ['class' => 'asbtn subbtnbuy', 'onclick' => 'submitform()', 'disabled' => true]) ?></div> <?php ActiveForm::end() ?> 

I need to somehow make it so that 1 regular form is sent after successfully filling out 2 forms and clicking on its submit, first did so in js dpsal

 function submitform() { document.payment.submit(); } 

but on submit hung onclick' => 'submitform()' , but so 1 form is sent with any click on submit, regardless of whether the 2nd form passed the validation, then did so set the submit attribute' disabled '=> true, and js wrote

 var regex = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/; $('#buy-accept').change(function(){ if(regex.test($('#buy-email').val())){ $(".subbtnbuy").prop("disabled", !$(this).is(':checked')); } }); 

that is, the button will be inactive until the user completes the 2 fields of the form, namely email and checkbox, in principle it works as it should, but no messages will be issued about the empty fields, and then if you tick the box before filling the field with mail will have to remove it again and set to unlock the submit button. This is a question that can be attached to a submission of one form, let’s allow a new window of another form, while transferring 2 forms to work after correctly filling the first one, or how it can be done differently so that 2 forms are sent by pressing 1 submit, but after validation fields of the form in which submit is specified?

  • As for the field from the form number 1 to put inside the form number 2, in the end everything will work without dancing with a tambourine and without too much POST. - Makarenko_I_V

1 answer 1

Of course, the on / off button is the simplest solution. But it would be correct to make a validation and, when clicking on the submit of the first form, check the validity of the second data. For example, remake the submitform function:

 var validateAlgoritm = { email: { rule: function (email) { return /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/.test(email); }, errorText: 'Ваш e-mail не прошел валидацию' } }, $email = $('#buy-email'); function validateForm2() { var result = { status: true, text: 'ok' }; if(!validateAlgoritm.email.rule($email.val())) { result.status = false; result.text = validateAlgoritm.email.errorText; } return result; }; function submitForm() { var valid = validateForm2(); if(valid.status) { document.payment.submit(); } else { alert(valid.text); } } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input name="email" id="buy-email"> <form id="payment" name="payment" method="post" action="https://sci.interkassa.com/" enctype="utf-8" target="_blank"> <input type="hidden" name="ik_co_id" value="hdjfhjsdjsdhkj7833jfjsdhf"/> <input type="hidden" name="ik_pm_no" value="ID_<?= rand(1000, 9999) ?>"/> <input type="hidden" name="ik_am" value="<?= $game->price ?>"/> <input type="hidden" name="ik_cur" value="UAH"/> <input type="hidden" name="ik_desc" value="<?= $game->name ?>"/> <input type="button" value="send" onclick="submitForm()"> </form>