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 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化