【问题标题】:Form submission using AJAX in Laravel 5.5在 Laravel 5.5 中使用 AJAX 提交表单
【发布时间】:2019-05-04 03:58:59
【问题描述】:

我正在尝试在 Laravel 5.5 中使用 ajax 提交表单 问题是页面正在刷新并且没有在数据库中提交数据。我需要在不刷新页面的情况下将数据存储在数据库中。
这是我的代码:

控制器

public function new_timing_table(Request $request)
{
    if (Request::ajax()) {
        $timing_tables =  new Timing_Table;
        $timing_tables->timing_tables_name = $request->timing_tables_name;
        $timing_tables->save();
        $msg = "yes";
    } else {
        $msg = "yes";
    }
    return ['msg'=> $msg];
}

查看

<form id="timeForm" class="form-horizontal form-material" >                                                                      
  <div class="form-group">                                                                      
   {{ csrf_field() }}
   <div class="col-md-12 m-b-20">
   <label> Table Name</label>
   <input type="text" id="timing_tables_name" class="form-control" 
   name="timing_tables_name" />                                                                            
   </div>
   <div class="modal-footer">
   <input type="button" value="Replace Message" id='btnSelector'>
   </div>
  </div>
</form>

Ajax 脚本

const xCsrfToken = "{{ csrf_token() }}";
$.ajaxSetup({
    headers: {
        'X-CSRF-TOKEN': xCsrfToken
    }
});


jQuery(document).ready(function() {
    jQuery('#btnSelector').click(function(e) {
        event.preventDefault();
        getMessage();
    });
});
var getMessage = function() {
    var timing_tables_name = $("input[name=timing_tables_name]").val();
    $.ajax({
        type: 'post',
        url: '/new_timing_table',
        dataType: 'json', //Make sure your returning data type dffine as json
        data: timing_tables_name,
        //data:'_token = <php echo csrf_token() ?>',
        success: function(data) {
            console.log(data); //Please share cosnole data
            if (data.msg) //Check the data.msg isset?
            {
                $("#msg").html(data.msg);
            }
        }
    });
}

路由器

 Route::post('/new_timing_table','Timing_TableControoler@new_timing_table');

【问题讨论】:

  • 我做得很好,谢谢你的提问。 :) 更严肃的一点是:在 SO 上不需要问候和问候,相反,它们经常使问题变得混乱并使其难以阅读。请保持高质量的问题,因为本网站的未来用户可能会搜索类似的问题,而您的问题也可能会帮助他们找到答案。
  • 在你的 jquery 上使用 e.preventDefault();您正在传递 e 并期待事件

标签: javascript php jquery ajax laravel


【解决方案1】:

在 Ajax 脚本中添加 beforeSend: function (request) 后,我的代码现在可以工作了

var getMessage = function(){
var timing_tables_name = $("#timing_tables_name").val();
console.log(timing_tables_name);

$.ajax({
    type:'GET',
    url:'/new_timing_table', //Make sure your URL is correct
    dataType: 'json', //Make sure your returning data type dffine as json
     data: 
     {
         timing_tables_name
     },
    beforeSend: function (request) {
            return request.setRequestHeader('X-CSRF-Token', $("meta[name='csrf- 
     token']").attr('content'));
        },
    success:function(data){
        console.log(data); //Please share cosnole data
        if(data.msg) //Check the data.msg isset?
        {
            $("#msg").html(data.msg); //replace html by data.msg
        }

    }
});
 }

将控制器编辑得像这个一样简单

  public function new_timing_table(Request $request){
    $timing_tables =  new Timing_Table;
    $timing_tables->timing_tables_name = $request->timing_tables_name;
    $timing_tables->save();
     $msg = "This is a simple message.";
     return ['msg'=> $msg];
      }

谢谢大家的帮助

【讨论】:

    【解决方案2】:

    您的脚本中有错字或错误。

    jQuery('#btnSelector').click(function(e){
        // An error here - it should be e.preventDefault();
        event.preventDefault();
        getMessage();
    });
    

    【讨论】:

    • 什么不起作用?我无法帮助你,当你只是说它不起作用并让它继续存在时 - 给我细节。什么不工作?你得到什么错误?
    • 感谢您的帮助,我现在已经解决了问题,因为我已经在下面的回放中添加了它
    猜你喜欢
    • 1970-01-01
    • 2016-12-16
    • 1970-01-01
    • 1970-01-01
    • 2015-02-05
    • 1970-01-01
    • 2018-06-22
    • 2020-03-25
    • 2015-08-16
    相关资源
    最近更新 更多