jquery, modal form submit , post serialize ..... almost there!

I have a modal form, and when it submits the jquery below is executed.

If the form submits successfully then it should return 1 and hide the modal window.

However it would appears to return nothing!

The alert below in the jquery goes off, implying 1 was not returned, so a failure, but the alert message is blank.

If there was a problem I would have expected to see "TWEET!"


<?php 

$script = <<< JS


$('form#{$model->formName()}').on('beforeSubmit', function(e)

{ 

    var \$form = $(this);

    $.post(

        '{URL}', //serialize Yii2 form

        \$form.serialize()

    )

        .done(function(result) {


        if(result == 1)

        {

            $(document).find('#modal').modal('hide');

        }else

        {

            //$(\$form).trigger("reset");

            alert(result);

        }

        }).fail(function()

        {

            console.log("server error");

        });

    return false;

});


JS;

$script = str_replace('{URL}', Url::toRoute(['/event-parent/create']), $script);

$this->registerJs($script);

?>

My EventParentController:


public function actionCreate()

    {

    

        $model = new EventParent();


        if (Yii::$app->request->isAjax && $model->load($_POST))

        {

            Yii::$app->response->format = 'json';

            return \yii\widgets\ActiveForm::validate($model);

        }


        if ($model->load(Yii::$app->request->post()) ) {

            if($model->save())

            {

                echo 1;

            } 

            else 

            {

                echo "TWEET!";

            }

            //return $this->redirect(['view', 'id' => $model->id]);

        } else {

            return $this->renderAjax('create', [

                'model' => $model,

            ]);

        }

    }

RIGHT! solved!

Turns out two sets of AJAX calls got the better of me.

so I have now passed a parameter in the URL called submitpress. If this is “T” then the submit button was pressed and so don’t do the ajax validation, but try and save.

If it is not "T" then do the ajax validation.

I had a problem where the record was valid, but the submit was not supposed to be done yet, but the previous code saved the event every time I tabbed around a valid record. This also fixed that.

Anyway, below is the fixed code:

The create action:


/**

     * Creates a new EventParent model.

     * If creation is successful, the browser will be redirected to the 'view' page.

     * @return mixed

     */

    public function actionCreate()

    {

    

        $model = new EventParent();

        $submitPress = Yii::$app->getRequest()->getQueryParam('submitpress');


        if (Yii::$app->request->isAjax && $model->load($_POST) && $submitPress != "T")

        {

            Yii::$app->response->format = 'json';

            return \yii\widgets\ActiveForm::validate($model);

        }      


        if ($model->load(Yii::$app->request->post()) && $submitPress === "T") {

            if($model->save())

            {

                echo 1;

            } 

            else 

            {

                echo 0;

            }

            //return $this->redirect(['view', 'id' => $model->id]);

        } else {

            return $this->renderAjax('create', [

                'model' => $model,

            ]);

        }

    }



The Jquery


<?php 

$script = <<< JS


$('form#{$model->formName()}').on('beforeSubmit', function(e)

{ 

    var \$form = $(this);

    $.post(

        '{URL}?submitpress=T', //serialize Yii2 form

        \$form.serialize()

    )

        .done(function(result) {


        if(result == 1)

        {

            $(document).find('#modal').modal('hide');

            $.pjax.reload({container:'#fullCalendarPJax'});

        }else

        {

            //$(\$form).trigger("reset");

            alert(result);

        }

        }).fail(function()

        {

            console.log("server error");

        });

    return false;

});


JS;

$script = str_replace('{URL}', Url::toRoute(['/event-parent/create']), $script);

$this->registerJs($script);

?>