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.

  • 写回答

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条)

报告相同问题?

悬赏问题

  • ¥20 机器学习或深度学习问题?困扰了我一个世纪,晚来天欲雪,能饮一杯无?
  • ¥15 c语言数据结构高铁订票系统
  • ¥15 关于wkernell.PDB加载的问题,如何解决?(语言-c#|开发工具-vscode)
  • ¥15 (标签-STM32|关键词-智能小车)
  • ¥20 关于#stm32#的问题,请各位专家解答!
  • ¥15 (标签-python)
  • ¥20 搭建awx,试了很多版本都有错
  • ¥15 java corba的客户端该如何指定使用本地某个固定IP去连接服务端?
  • ¥15 activiti工作流问题,求解答
  • ¥15 有人写过RPA后台管理系统么?