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

报告相同问题?

悬赏问题

  • ¥15 shape_predictor_68_face_landmarks.dat
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制