I
I
IvanMogilev2020-08-13 16:00:21
Yii
IvanMogilev, 2020-08-13 16:00:21

registerjs not working in ajax?

The form is loaded by ajax, the file of the controller called by ajax

public function actionSignup()
    {
        $model = new FormSignUp();
        return $this->renderPartial('/popup/signup', compact('model'));
    }


view code
<?php $form = ActiveForm::begin(
            [
                'id' => 'right-form',
                'action' => \frontend\helpers\Url::to(['form/save-sign-up']),
                'enableAjaxValidation' => true,
                'validationUrl' => \frontend\helpers\Url::to(['form/validate-sign-up']),
                'options' => [],
                'fieldConfig' => [
                    'options' => [
                        'tag' => false,
                    ],
                ],
            ]);
        ?>
        <div class="box-form">
            <div class="item-form">
                <?= $form->field($model, 'name')->textInput(['placeholder' => "Имя", 'type' => 'text'])->label(false); ?>
            </div>
            <div class="item-form">
                <?= $form->field($model, 'phone')->textInput(['placeholder' => "Телефон", 'type' => 'tel'])->label(false); ?>
            </div>

            <div class="item-form">
                <div class="select-radio close-on">
                    <span class="active-item">Выберите парк</span>
                    <ul>
                        <li class="city-select">Минск</li>
                        <li><label for="signup1">
                                <?= $form->field($model, 'city')->radio(['id' => "signup1", 'value' => "trc_arena_city", 'uncheck' => null, 'template' => '{input}'])->label(false) ?>
                                <span>ТРЦ Arena City</span></label></li>
                        <li><label for="signup2">
                                <?= $form->field($model, 'city')->radio(['id' => "signup2", 'value' => "trc_Diamond_city", 'uncheck' => null, 'template' => '{input}'])->label(false) ?>
                                <span>ТРЦ Diamond City</span></label>
                        </li>
                        <li><label for="signup3">
                                <?= $form->field($model, 'city')->radio(['id' => "signup3", 'value' => "trc_evroopt", 'uncheck' => null, 'template' => '{input}'])->label(false) ?>
                                <span>ТРЦ Евроопт</span></label></li>
                        <li class="city-select">Брест</li>
                        <li><label for="signup4">
                                <?= $form->field($model, 'city')->radio(['id' => "signup4", 'value' => "trc_eurospar", 'uncheck' => null, 'template' => '{input}'])->label(false) ?>
                                <span>ТРЦ EuroSpar</span></label></li>
                    </ul>
                </div>
            </div>

            <div class="item-form">
                <div class="sub-item-form">
                    <?= $form->field($model, 'date')->textInput(['placeholder' => "Дата", 'type' => 'tel'])->label(false); ?>
                    <span class="ic_calendar"></span></div>
                <div class="sub-item-form">
                    <?= $form->field($model, 'time')->textInput(['placeholder' => "Время", 'type' => 'tel'])->label(false); ?>
                    <span class="ic_watch"></span></div>
            </div>
            <div class="item-form">
                <?= $form->field($model, 'count')->textInput(['placeholder' => "Кол-во человек", 'type' => 'tel'])->label(false); ?>
            </div>
            <?= \yii\bootstrap\Html::submitButton(\Yii::t('app', 'Записаться'), ['class' => 'btn-red']); ?>
        </div>

        <?php ActiveForm::end(); ?>
<?php
$js = <<<JS

    
 jQuery(document).ready(function($) {
         $("form#{$form_id}").on('beforeSubmit', function(event){
            event.preventDefault(); // stopping submitting
            
            var data = $(this).serializeArray();
            var url = $(this).attr('action');
            $.ajax({
                url: url,
                type: 'post',
                dataType: 'json',
                data: data
            })
            .done(function(response) {
                if (response.success == true) {
                    alert('успешно отправлено');                    
                    $("form#{$form_id}")[0].reset();
                }
            })
            .fail(function() {
                alert('ошибка отправки');
            });
        return false;
        }).on('ajaxComplete', function(event, jqXHR, textStatus) {
            
        });
    }); 
   
 
 
JS;

$this->registerJs($js);


?>

and here register js does not work, how can I get around this problem?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question