jquery 返回按钮不工作laravel AJAX crud

oewdyzsn  于 2023-08-04  发布在  jQuery
关注(0)|答案(1)|浏览(89)

我试图转到上一页,但它不工作,不知何故给我"SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'name' cannot be null (Connection: mysql, SQL: insert into 'students' ('name', 'email', 'contact', 'course', 'updated_at','created_at') values (?, ?, ?, ?, 2023-07-12 08:59:56, 2023-07-12 08:59:56))"这个错误
create.blade

<div class="div2">

    <form method="post" name="form" class="create_form" action="{{ route('students.store') }}" enctype="multipart/form-data">
        @csrf
        Name <input type="text" name="name" id="name"><br><br>
        Email <input type="text" name="email" id="email"><br><br>
        Contact <input type="text" name="contact" id="contact"><br><br>
        Course <input type="text" name="course" id="course"><br><br>

        <button type="submit" class="save_create">Save</button>
        <button type="submit" class="back_create" formaction="{{ route('students.index') }}">Back</button>
    </form>
</div>

字符串
AJAX 提交和返回按钮代码块

$(document).on('submit', '.create_form', function(event) {
  event.preventDefault();
  var data = $(this).serialize();
  $.ajax({
    url: "{{ route('students.store') }}",
    data: data,
    type: "POST",
    dataType: "json",
    success: function(response) {
      window.location.href = ("{{ route('students.index') }}");
    },
    error: function(error) {
      console.log("Errors :", error);
    }
  });
});

// Back button clicked on create page
$(".back_create").on("click", function(event) {
  event.preventDefault();
  $.ajax({
    type: "GET",
    url: "{{ route('students.index') }}",
    success: function(response) {
      window.location.href = ("{{ route('students.index') }}");
    }
  });
});


路由是完全控制器:
Route::resource('/students',StudentController::class);
我试着把url使用
$(this).attr('formaction ')
但保存按钮也不起作用。

qgzx9mmu

qgzx9mmu1#

你该换衣服了

<button type="submit" class="back_create" formaction="{{ route('students.index') }}">Back</button>

字符串

<button type="button" class="back_create" data-formaction="{{ route('students.index') }}">Back</button>


由于按钮的类型是submit将提交表单,而不是把这个块

// Back button clicked on create page
$(".back_create").on("click",function(event){
   event.preventDefault();
   $.ajax({
        type: "GET",
        url: "{{ route('students.index') }}",
        success: function (response) {
             window.location.href = ("{{ route('students.index') }}");
        }
   });
});


向外$(document).on('submit')...

相关问题