JQuery表单提交到数据库中

时间:2016-02-14 04:37:58

标签: php jquery ajax twitter-bootstrap laravel-5.2

我想使用模态引导和Jquery提交按钮和框架,我使用的是laravel 5.2,下面是我的控制器,模态,jquery和路由,问题是当我点击保存更改没有发生任何事情,是否有我做错了什么?

  

模态

<div class="modal fade" id="addModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal">&times;</button>
                <h3 class="modal-title"><span class="glyphicon glyphicon-plus"></span> Add Barang</h3>
            </div>
            <div class="modal-body" id="addModal">
                <form id="tambahform" class="form-horizontal" role="form">
                    <div class="form-body">
                        <div class="form-group">
                            <label class="col-md-3 control-label">Name</label>
                            <div class="col-md-9">
                                <input type="text" id="client-nama" name="clientName" class="form-control" placeholder="Your Name">
                            </div>
                        </div>
                    </div>
                </form>
            </div>
        <div class="modal-footer">
            <button type="button" class="btn-add btn-primary">Save changes</button>
        </div>
    </div>
  </div>
</div>
  

JQuery的

$(document).ready(function(){
    var check1=0;
    $('#tambahform').submit(function(e){
    if (check1==0){
        $("#addModal").animate({scrollTop:0}, 'slow');
    }else{
        e.preventDefault();
        var formData = new FormData($(this)[0]);
        $.ajax({
            url:'form/insert',
            data:formData,
            type:'POST',
            contentType: false,
            processData: false,
            success:function(data){
                $("#addModal").hide();
                window.location.reload(true);
            }
        });
    }
    return false;
    });
  

控制器

public function insert(){
    $nama = Input::get('nama');
    $this->nama = Input::get('clientName');
    $query = DB::table('profile')->insert(array('nama'=>$nama));  

}
  

路线

Route::get('form/insert', 'FormController@insert');

2 个答案:

答案 0 :(得分:1)

您需要定义用于提交form的方法。没有提到method因此提交了defaultGET

因此form标记应如下所示:

<form method="POST" id="tambahform" class="form-horizontal" role="form">

url中,您需要为其提供完整路径,否则会出现404 not found错误。看看这段代码:

e.preventDefault();
var formData = $(tambahform).serialize(); //<--use Serialize
 $.ajax({
     url:'form/insert.php', //<--add full path including the files extension here
     data:formData,
     type:'POST',
     contentType: false,
     processData: false,
     success:function(data){
            $("#addModal").hide();
            window.location.reload(true);
            }

答案 1 :(得分:0)

而不是这个

Route::get('form/insert', 'FormController@insert');

使用此

Route::post('form/insert', 'FormController@insert');