laravel 将数据从刀片文件传递到控制器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35921295/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 13:22:54  来源:igfitidea点击:

laravel pass data from blade file to Controller

phphtmlmysqllaravelblade

提问by ray

I am trying to take data that a user inputs into a form, created in blade, and transport that data to my controller. At that point I can use a function in my controller to write the data into MySQL.

我试图将用户输入到表单中的数据,在刀片中创建,并将该数据传输到我的控制器。那时我可以使用控制器中的函数将数据写入 MySQL。

Here is the code for the form which I have in my blade file.

这是我的刀片文件中的表单代码。

<form action="{{ route("users") }}" method="post">

<input type ="form" id="firstname"> firstname </input> <br>
<input type ="form" id="lastname"> lastname </input> <br>
<input type="form" id="email"> email </input> <br>
<input type="form" id="userlevel"> userlevel </input> <br>
<input type="form" id="password"> password </input> <br>


<br> <br>
<button type="submit"> Submit
</button>
</form>

Here is the code in my controller which writes data into MySQL. The code works when I am using dummy data, instead of the $nameand $lastnamevariables.

这是我的控制器中将数据写入 MySQL 的代码。当我使用虚拟数据而不是$name$lastname变量时,该代码有效。

public function users()
{
    $name = $_POST["firstname"];
    $lastname = $_POST["lastname"];

    DB :: table("newUsers") -> insertGetId
    (
        array("firstname" => $name, "lastname" => $lastname, "email" => "test")
    );

    return view("pages.users");
}

I am currently getting an error that says

我目前收到一个错误提示

Undefined index: firstname

未定义索引:名字

Ideally what I want to happen is for whatever the user entered for firstname and lastname to be written into my MySQL table.

理想情况下,我想要发生的是将用户为名字和姓氏输入的任何内容写入我的 MySQL 表。

回答by DavidT

You need to add nameattributes to your inputs and format them correctly they should look more like:

您需要向name输入添加属性并正确设置它们的格式,它们应该看起来更像:

<input type="text" id="firstname" name="firstname"></input>

If you are placing 'firstname' between your openning and closing input tags for a placholder value use the placeholder attribute instead.

如果您在占位符值的开始和结束输入标签之间放置“名字”,请改用占位符属性。

<input type="text" id="firstname" name="firstname" placeholder="first name"></input>

But do be warned the placeholder attribute doesnt work in older browsers

但请注意占位符属性在旧浏览器中不起作用

Also dont use $_POSTlaravel has built in ways of getting the values from your form.

也不要使用$_POSTLaravel 内置的从表单中获取值的方法。

Laravel 5

Laravel 5

$request->input('firstname');

like this:

像这样:

public function users(Request $request)
{
    $name = $request->input('firstname'); // This is better than using $_POST
    $lastname = $request->input('lastname');

    DB :: table("newUsers") -> insertGetId
    (
        array("firstname" => $name, "lastname" => $lastname, "email" => "test")
     );

    return view("pages.users");
}

Notice, I have passed in Request $requestlike a variable in the method users. You will also need to add use Illuminate\Http\Request as Request;to the top of your class:

注意,我Request $request在方法中像变量一样传入users。您还需要添加use Illuminate\Http\Request as Request;到班级的顶部:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request as Request; //here

class MyController extends Controller
{
}

Laravel 4

Laravel 4

Input::get('firstname');

回答by Jilson Thomas

Change your Form as follows:

更改您的表格如下:

<form action="{{ route("users") }}" method="post">
  <input type ="form" name=firstname" id="firstname" placeholder="First name"> 
  <input type ="form" name=lastname" id="lastname">
  <input type="form" name="email" id="email">

  <button type="submit"> Submit </button>
</form>

You've to provide the nameattribute to your form fields.

您必须为name表单字段提供该属性。

And in the controller, you don't have to use $_POSTanymore.

在控制器中,您不必再使用$_POST了。

Just use:

只需使用:

public function users(Request $request)
{
   $firstname = $request->input('firstname');
   $lastname = $request->input('lastname');

    DB :: table("newUsers") -> insertGetId(
       array("firstname" => $name, "lastname" => $lastname, "email" => "test")
    );
    return view("pages.users");
 }

回答by Alexey Mezenin

You can access form data with $request->get('first_name'), for example.

$request->get('first_name')例如,您可以使用 访问表单数据。

To make it work, add nametags to your form inputsand catch data, for example, if you use store action:

要使其工作,请将name标签添加到您的表单inputs并捕获数据,例如,如果您使用 store 操作:

function store(Request $request){

}

In this case you also can easilyvalidate input data by using custom Reuqest classes: https://laravel.com/docs/master/validation#form-request-validation

在这种情况下,您还可以easily使用自定义 Reuqest 类来验证输入数据:https://laravel.com/docs/master/validation#form-request-validation