drl9940
2016-03-10 15:55
浏览 29
已采纳

laravel将数据从刀片文件传递到Controller

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.

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 $name and $lastname variables.

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.

图片转代码服务由CSDN问答提供 功能建议

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

以下是我在刀片文件中的表单代码。

 &lt; form action =“{{route(”users“)}}”method =“post”&gt; 
 
&lt; input type =“form”id  = “姓名” &GT; 名字&lt; / input&gt;  &lt; br&gt; 
&lt; input type =“form”id =“lastname”&gt; 姓氏&lt; / input&gt;  &lt; br&gt; 
&lt; input type =“form”id =“email”&gt; 电子邮件&lt; / input&gt;  &lt; br&gt; 
&lt; input type =“form”id =“userlevel”&gt;  userlevel&lt; / input&gt;  &lt; br&gt; 
&lt; input type =“form”id =“password”&gt; 密码&lt; / input&gt;  &LT峰; br&GT; 
 
 
&LT峰; br&GT;  &lt; br&gt; 
&lt; button type =“submit”&gt; 提交
&lt; / button&gt; 
&lt; / form&gt; 
   
 
 

以下是我的控制器中将数据写入MySQL的代码。 代码在我使用虚拟数据时起作用,而不是 $ name $ lastname 变量。

  public function  users()
 {
 $ name = $ _POST [“firstname”]; 
 $ lastname = $ _POST [“lastname”]; 
 
 DB :: table(“newUsers”) - &gt;  insertGetId 
(
 array(“firstname”=&gt; $ name,“lastname”=&gt; $ lastname,“email”=&gt;“test”)
); 
 
返回视图(“pages。 用户“); 
} 
   
 
 

我目前收到的错误是

未定义的索引 :firstname

理想情况下,我想要发生的是用户输入的firstname和lastname写入我的MySQL表。

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

3条回答 默认 最新

  • drflkphi675447 2016-03-10 15:56
    最佳回答

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

    <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 $_POST laravel has built in ways of getting the values from your form.

    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 $request like a variable in the method users. You will also need to add use Illuminate\Http\Request as Request; to the top of your class:

    <?php
    
    namespace App\Http\Controllers;
    
    use Illuminate\Http\Request as Request; //here
    
    class MyController extends Controller
    {
    }
    

    Laravel 4

    Input::get('firstname');
    
    评论
    解决 无用
    打赏 举报
查看更多回答(2条)