简体   繁体   中英

Ajax form submit in yii framework

I am sure the problem is with ajax. Without ajax it is fine. This is my javascript code:

 var frm = $('#register-form');
   $(document).on('submit', '#register-form', function (e) {


          $.ajax({

            type: frm.attr('method'),
            url: frm.attr('action'),
            data: frm.serialize(),
            success: function (data) {
                alert('Form was submitted!');
            }
        });

          e.preventDefault();
        });

  });

The form is:

 <form class="formclass" method="POST" action="<?php echo Yii::$app->request->baseUrl;?>/telephone/addnow/" role="form" enctype="multipart/form-data" id="register-form" novalidate="novalidate">
...
</form>

And the controller action is:

   public function actionAddnow()
        {
            if(Yii::$app->request->isAjax)
            {
                      //codes here
     }
}

The problem is that when I click the submit button, nothing is happening.

I see that you are using multipart/form-data, i figure that you are sending files whitin yhe form.

In that case you shuldent use:

data: frm.serialize(),

insted

data: new FormData(frm),

Also you said that when you click submit nothing is hapend, but i think that you should be able to see a error in your browser console. That could be usefull info.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM