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.
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.
$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
{
}
Input::get('firstname');
Change your Form as follows:
<form action="{{ route("users") }}" method="post">
<input type ="form" name=firstname" id="firstname" placeholder="First name">
<input type ="form" name=lastname" id="lastname">
<input type="form" name="email" id="email">
<button type="submit"> Submit </button>
</form>
You've to provide the name
attribute to your form fields.
And in the controller, you don't have to use $_POST
anymore.
Just use:
public function users(Request $request)
{
$firstname = $request->input('firstname');
$lastname = $request->input('lastname');
DB :: table("newUsers") -> insertGetId(
array("firstname" => $name, "lastname" => $lastname, "email" => "test")
);
return view("pages.users");
}
You can access form data with $request->get('first_name')
, for example.
To make it work, add name
tags to your form inputs
and catch data, for example, if you use store action:
function store(Request $request){
}
In this case you also can easily
validate input data by using custom Reuqest classes: https://laravel.com/docs/master/validation#form-request-validation
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.