public function insert()
{ //The front-end submission must be an Ajax request and then verify and add new operations
if(Request::isAjax()){
//1. Data verification
$data = Request::post(); //Data to be verified
$rule = 'app\common\validate\User'; //Customized validator
//Start verification: Save error information in $res, return true successfully
$res=$this-> ;validate($data,$rule);
if (true !== $res){ //Validation failed
return ['status'=> -1, 'message' =>$res];
}else { //Verification successful
//2. Write the data to the data table zh_user and judge the writing result
if(UserModel::create($data)){
//After successful registration, automatic login is achieved
return ['status'=>1, 'message'= >'Congratulations, registration successful~~'];
} else {
return ['status'=>0, 'message'=>'Registration failed~~'] ;
}
}
}else{
$this->error('Request type error','register');
}
}
<script type="text/javascript">
$(function(){
$( '#register').on('click',function(){
//Use ajax to submit user information
$.ajax({
type: 'post ',
url: "{:url('index/user/insert')}",
data: $('#login').serialize(),
dataType: 'json',
success: function(data){
switch (data.status)
{
case 1:
alert(data.message);
window.location.href = "{:url('index/index')}";
break;
case 0:
case -1:
alert(data.message);
window.location.back();
</script>
Have you solved it? I am the same way. How did you become successful?