【发布时间】:2020-05-03 17:13:11
【问题描述】:
我正在使用 javascript 动态创建一个表单,该表单从聊天中获取答案并将其添加到表单中。该表单存在于单独的刀片文件register.blade.php 中。我在主页 (index.blade.php) 中生成了表单,但 @section('registration-form') 在读取 js 之前生成了页面。出于这个原因,来自聊天的答案永远不会输入到表单中。
register.blade.php 持有(自定义)表单@section('registration-form')。从那里我想在routes/web.php 中创建一个route::get('/index', 'CustomAuthController') 来将表单数据存储在我的数据库中。关于如何完成这项工作的任何想法?
对 Laravel 非常陌生。使用 Laravel 5.4。
代码:
布局/app.blade.php
@include('includes.head')
<body>
@include('includes.navbar')
<div class="container">
@yield('content')
</div>
@include('includes.footer')
</body>
index.blade.php
@extends('layouts.app')
@section('content')
<main>
<section class="row" >
<!------ a bunch of content here ----------->
<!----- form yielded here -------->
@yield('registration-form')
<!--- form end ----->
<!------ more content -------->
</section>
</main>
@endsection
register.blade.php
@section('registration-form')
<div class="padding-32" id="signup-two">
<h4>Review and submit your information</h4>
<form method="POST" action="{{ route('signup') }}">
{{ csrf_field() }}
<div class="form-group" id="fname">
<label></label>
<input name="fname"/>
</div>
<div class="form-group" id="email">
<label></label>
<input name="email"/>
</div>
<div class="form-group" id="password">
<label></label>
<input name="password"/>
</div>
<div class="form-group" id="BMI">
<label></label>
<input name="BMI"/>
</div>
<div class="form-group" id="height">
<label></label>
<input name="height"/>
</div>
<div class="form-group" id="weight">
<label></label>
<input name="weight"/>
</div>
</form>
@endsection
我尝试在app.blade.php 中生成@section(registration-form),但表单仍然没有加载。我也不知道如何为此创建路由和控制器。代码如下所示:
布局/app.blade.php
@include('includes.head')
<body>
@include('includes.navbar')
<div class="container">
@yield('content')
@yield('registration-form')
</div>
@include('includes.footer')
</body>
</html>
路由/web.php
Route::get('/index', function () {
return view('registration-form', [
'fname' => 'fname',
'BMI' => 'BMI',
'height' => 'height',
'weight' => 'weight'
]);
});
【问题讨论】:
-
如果 js 是专门用于注册表单的,为什么不创建另一个 js 文件并在内部添加和加载脚本(在底部)
@section('registration-form')。 -
你的
registration-form是什么样的? -
就在上面的register.blade.php下面的代码块中。我将它包裹在@section('registration-form')中,并尝试在 index.blade.php 中生成它。
-
@yunhai 我的 JavaScript 会计算并制作和移动页面上的对象,因此我无法将表单的 js 移动到单独的文件中。
-
@bella 你的问题比较混乱,你想在
@yield(form)之前还是之后加载js。看起来问题的标题和你想要接近的东西是自相矛盾的。您可能想稍微修改一下您的问题。
标签: javascript php forms laravel-5