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,
]);
}
}