Reputation: 185
Hello i´m new in laravel. I dont know how to store the value of a select in a variable. This is how i do it in php. But how in laravel? Thank you for help :)
<?php
session_start();
//Database
$servername = "127.0.0.1";
$username = "root";
$password = "root";
//Connection to Database
$conn = new mysqli($servername, $username, $password);
//Connection Test
if(!$conn) {
echo "Not connected to Server"
}
if(!mysqli_select_db($conn, 'test')){
echo "No connection to Database";
}
//store data in variable
$vorname = $_POST['name'];
$name = $_POST['age'];
?>
<!--Form Select-->
<form action="test.php" method="post">
<select name="name">
<option value="Lisa"></option>
<option value="Laura"></option>
</select>
<select name="age">
<option value="20">20</option>
<option value="21">21</option>
</select>
<button type="submit">Sent</button>
</form>
<?php
$query = "SELECT * FROM test WHERE name = $name AND age = $age";
$profile = $conn->query($query);
?>
I want a Form with a select and the selected option should be stored in a variable. Then checked in the database. And then show the results. i dont know how to do thisin laravel. thanks :)
Upvotes: 2
Views: 6824
Reputation: 8348
All right let's get started. I suggest you study laravel documentation so things get more clear to you.
First of all you need to create your routes in your web.php file.
Route::get('/test', 'testController@index')->name('test.index');
Route::post('/values', 'testController@getValues')->name('test.values');
The first one will return your view the second one is to insert the data. Hang in there i will explain everything in the next lines.
Now you need a controller to handle the data and of course a view to preview your dropdowns.
In order to make a controller you can simple use php artisan make:controller testController
command.
This will create a controller named testController like we named it in our routes.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use DB;
class testController extends Controller
{
public function index()
{
return view('test');
}
}
This is how your controller will look on this step. Simply return your view template (which i named it test for example pursposes). Now you actually need to create this view that you are trying to return. So inside the views file you create a test.blade.php file and post your html code a bit modified.
<form action="{{ action('testController@getValues') }}" method="post" id="postData">
{{ csrf_field() }}
<select name="name">
<option value="Lisa">Lisa</option>
<option value="Laura">Laura</option>
</select>
<select name="age">
<option value="20">20</option>
<option value="21">21</option>
</select>
<button type="submit">Sent</button>
</form>
You will notice that the action of the form is pointing straight to a controller function which will create in the next step. It's used to insert the data in the Database.
The csrf_field creates a token. For now you will realize that it helps your session not to "timeout" but does a lot more than that. Read more about it here!!!
The way to access your form is simple that's why laravel's routing makes things so simple. Go like "localhost/my_project_folder/test" and you should be able to see your view.
All right moving forward. Now you need to send the data from view to controller so you store it in the db.
We need a new function in the controller named getValues
like we named it in the web.php file in the beginning. Now your controller should look like that:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use DB;
class testController extends Controller
{
public function index()
{
return view('test');
}
public function getValues(Request $request){
$name=$request->get('name');
$age=$request->get('age');
$insertData=DB::table('data')->insert(
['name' => $name, 'age' => $age]
);
}
}
Request method is really useful in laravel so investigate some more about this method here!!!
Now the final part. Connect your database. All the connections happen in the .env file.
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=testingdata
DB_USERNAME=root
DB_PASSWORD=root
This is my private database in my localhost so you can modify it using your credentials. After doing this step you are good to go.
If you have used different database or made a lot of changes etc etc and feel like you are using old settings run php artisan config:cache
command to configure your cache with the latest changes.
This is the simplest walk through for insert data from the form to the database. You can optimize it and expand it a lot so my suggestion to you is to start reading laravel documentation, if possible get access to laracast and if you have time participate in the laracasts forum.
Upvotes: 3