moh
moh

Reputation: 443

yii2 ajax submit custom form do nothing

**hello

i want to submit my form value with ajax i have a modal in my view file in modal i built a custom form to submit my data to a action**

<?php

use yii\bootstrap\Modal;
use yii\helpers\Html;
use yii\helpers\Url;

Modal::begin([
    'header' => 'روز تخمینی برای اتمام پروژه انتخاب کنید',
    'id' => 'modal-accept',
    'size' => 'modal-sm',
    'clientOptions' => [
    'backdrop' => 'static',
    'keyboard' => false,

    ],
]);

?>
<div id="modal-content">
<form method="post" action="<?= Url::to(['admin/site-developer/accept-task'])?>" id="modal_form">

    <?php
    echo jDate\DatePicker::widget([
            'name' => 'datepicker',
            'id' => 'estimate',
        ]);

    ?>
    <?= Html::submitButton('submit',['class' => 'btn btn-sm btn-success']) ?>
</form>

</div>

<?php
Modal::end();
?>

for submit my value and get the result i used this function ,,, but when i click submit no ajax call will perform nothing will happening why i cant see any network activity in my browser inspect ...

 $('#modal_form').on('beforeSubmit', function(e) {
    var form = $(this);
    var formData = form.serialize();
    $.ajax({
        url: form.attr("action"),
        type: form.attr("method"),
        data: formData,
        success: function (data) {
             console.log("successfull");
        },
        error: function () {
            alert("Something went wrong");
        }
    });

}).on('submit', function(e){
    e.preventDefault();
});

any suggestion?

Upvotes: 0

Views: 1251

Answers (1)

Gabriele Carbonai
Gabriele Carbonai

Reputation: 459

You don't need to use before submsision, this is the problem

$('#modal_form').on('submit', function(e){
    e.preventDefault();
     var form = $(this);
    var formData = form.serialize();

    $.ajax({
        url: form.attr("action"),
        type: form.attr("method"),
        data: formData,
        success: function (data) {
             console.log("successfull");
        },
        error: function () {
            alert("Something went wrong");
        }
    });
    return false;

});

Upvotes: 1

Related Questions