Yii 2,使用Ajax提交表单

时间:2022-10-05 08:22:21

I'm new to Yii and would appreciate any help. I render the view with form:

我是Yii的新手,非常感谢任何帮助。我用表单渲染视图:

public function actionCreate()
{    
     return $this->render('create');       
}

view:

视图:

<form id="myform" action="/test/web/index.php?r=form/preorder" method="post">
    <input type="text" id="form-firstname" name="Form[firstName]" required maxlength="50">                    
    <input type="text" id="form-lastname" name="Form[lastName]" required maxlength="50">
    <input name="send" type="submit" value="Отправить">
</form>

Im using plain html instead of Yii extensions in form, because I need to have a frontend with html/javascript only. On backend I can use Yii.

我在表单中使用普通的html而不是Yii扩展,因为我需要只有html / javascript的前端。在后端我可以使用Yii。

Now, I try to submit the form using ajax:

现在,我尝试使用ajax提交表单:

 $(document).ready(function(){      
$("body").on('beforeSubmit', 'form#myform', function(e){       
      var form = $(this);
      $.ajax({
            url    : form.attr('action'),
            type   : 'POST',
            data   : form.serialize(),
            success: function (response) 
            {                  
               console.log(response);
            },
            error  : function () 
            {
                console.log('internal server error');
            }
        });
    return false;

  });
});

FormController:

的FormController:

public function actionPreorder(){

     if (Yii::$app->request->isAjax) {
            Yii::$app->response->format = Response::FORMAT_JSON;
            $res = array(
                'body'    => $_POST,
                'success' => true,
            );
            //also I need to save to db
            return $res;
     }
}

The problem is when I submit the form, it redirects to the new page preorder, and I can't see my posted data. Im not sure what Im doing wrong.

问题是当我提交表单时,它会重定向到新页面预订,我看不到我发布的数据。我不知道我做错了什么。

1 个解决方案

#1


1  

Ok, so the solution is to remove action from form:

好的,所以解决方案是从表单中删除操作:

<form id="myform" action="" method="post">

js:

JS:

$("#myform").submit( function(e){
      var form = $(this);
      $.ajax({
            url    : '/megogo/web/index.php?r=form/preorder',
            type   : 'POST',
            data   : form.serialize(),
            success: function (response) 
            {                  
               console.log(response);
            },
            error  : function (e) 
            {
                console.log(e);
            }
        });
    return false;        
  })

#1


1  

Ok, so the solution is to remove action from form:

好的,所以解决方案是从表单中删除操作:

<form id="myform" action="" method="post">

js:

JS:

$("#myform").submit( function(e){
      var form = $(this);
      $.ajax({
            url    : '/megogo/web/index.php?r=form/preorder',
            type   : 'POST',
            data   : form.serialize(),
            success: function (response) 
            {                  
               console.log(response);
            },
            error  : function (e) 
            {
                console.log(e);
            }
        });
    return false;        
  })