How do I recover and calculate 2 numbers via POST in Controller in Laravel?

Asked

Viewed 433 times

2

I am a beginner in the language and I am trying to learn by myself, I created a php .blade.file in order to fill two simple fields and return the calculation via Submit, after that I intend to list a history stored in SQL database but I hardly know how to recover information from INDEX file !!! So you can see that I am very lost xD...and all help and tip will be very useful in my case, even more if I am doing it the WRONG way !!! Below are the files:

HTML:

<html>
    <head>
        <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
        <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
        <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
    </head>

    <div class='section'>
        <div class='container'>
            <div class='row'>    
                <div class='col-sm-3'>
                    <form method='POST'>
                        <input type='text' name='elemento1'>
                        <input typle='text' name='elemento2'><br>
                        <a href="calcular"><button type='submit'>Calcular</button></a><br>
                        <input typle='text' id='elemento2'>
                    <form method='POST'>
                </div>
            </div>
        </div>
    </div>
</html>

ROUTE:

<?php

Route::get('/', 'admin\DashBoardController@index');

Route::prefix('/')->group(function(){
    Route::post('calcular','admin\DashboardController@calcular');
});

CONTROLLER:

<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class DashboardController extends Controller
{
    public function index(){
        return view('index');
    }
    public function calcular(Request $requisicao){

        return $_POST;
    }
}

2 answers

1

Good morning friend, it is very simple to work with Alavel, I recommend very much that you learn the framework reading the documentation.

1º- Your form is set in the wrong way. This would be the right way to create, clear to your application.

<form method="POST" action="{{route('calcularNumero')}}">
  <input type='number' name='elemento1'>
  <input type='number' name='elemento2'><br>
  <a href="calcular">
    <button type='submit'>Calcular</button>
   </a><br>
  <input type='text' id='elemento2'>
</form>

Remembering which property Action is set on which route your request will go, where will all the information that is in your form.

I recommend reading this documentation about the tag form.


2º- I put a name for your controller to be able to access it in a better way in the view.

Route::prefix('/')->group(function(){
    Route::post('calcular','admin\DashboardController@calcular')->name('calcularNumero');
});

The estate name serves to name your routes to be able to access it easily.

Documentation of the language better on the routes.


3º- Difinindo the way you can get the values that comes from your formlário.

<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class DashboardController extends Controller
{
    public function index() 
    {
        return view('index');
    }

    public function calcular(Request $request) //Retirei o $requisição e coloqei $request 
    {
      $numeroUm = $request->input('elemento1');
      $numeroDois = $request->input('elemento2');
      $calq = $numeroUm + $numeroDois;
      return $calq; //ou return response()->json($calq);
    }
}

Documentation of the Laravel that explains better how the Controller works, and how Request/Request works.

0


First replace your Blade from the Section, by this code here:

<div class='section'>
    <div class='container'>
        <div class='row'>    
            <div class='col-sm-3'>
                <form method='POST' action='/calcular'>
                    <label>valor 1 :</label><input type='text' name='valor1'><br />
                    <label>valor 2 </label><input typle='text' name='valor2'><br />
                    <input type="submit" value="Enviar" /><br>
                </form>
            </div>
        </div>
    </div>
</div>

Every change POST you do have to have an action ( a path to which you want the data to be received ). The values you want to catch will always be inside the tag form preferably the inputs should be named.

Replace the method calculate in his Dashboardcontroller by that piece of code:

public function calcular(Request $requisicao){

        $valor1 = $requisicao->input('valor1');
        $valor2 = $requisicao->input('valor2');

        return response()->json(['valor1 : ' => $valor1, 'valor2' => $valor2]);
    }

Use the $_POST is not wrong, but you are using the Laravel a php framework, so you can abstract this using the class Request using the same class method o input('valor1') ( that takes the value of the name of the input set in your Blade ).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.