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 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了