我正试图转到上一页,但它不工作,不知何故给我"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’)

此外,保存按钮也不起作用.

推荐答案

你该换衣服了

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

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

Because of type of but至n is submit will submit form instead and put this block

// Back but至n 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') }}");
        }
   });
});

至 the outside $(document).on('submit')...

Php相关问答推荐

有条件的运费率基于购物车小计与WooPayments货币转换

如果再次调用SESSION_START(),会话.gc-max是否会重新启动?或者它是从第一次创建会话开始计算的?

PHP DateInterval天数不一致

Laravel数据表在一个视图中传递两个实例

如何更改数据表行背景 colored颜色

如何保存基于用户 Select 的复选框 Select 模式

列出所有WooCommerce处理订单中的产品数量,SKU和品牌

基于自定义域的每个购物车项目的WooCommerce定制数量输入步骤

Regex:高级删除单行PHP注释

Filament v3:具有 hasMany 和 BelongsTo 的关系管理器

如何使用 PHP 创建简化的 twig 替换?

在 Heroku 上部署 Sylius - 路由不正确

使用 Composer 找不到 Google APIClient 类

如何在WooCommerce管理产品列表中显示运输类别列

ACF 更新字段功能不更新 WordPress 中的任何数据

Symfony 迁移判断器在我的工作中失控

同时执行odbc_execute、odbc_fetch_row和odbc_result

如何限制for循环php中的条目数

来自命令行的 PHP 没有给出正确的结果

getenv() 有时在 CodeIgniter 4 中返回 false.为什么?