Display form data in another view

0

I have to pass the data of a form, by means of a controller to a new view (different from the form).

I have written the following:

In web.php:

//ruta para meter datos y llamar al controlador:
Route::get('/formalumno','AlumnosController@mostrar');

//ruta para mostrar datos en una vista nueva:
Route::post('/veralumno/{nombre}/{nota}','AlumnosController@mostrar');

In AlumnosController.php:

$nombre=$_POST['nombre']:
$nota=$_POST['nota']:
public function mostrar($nombre,$nota){
    $cal="No apto";
    if($nota>=5){
        $cal="Apto";
    }
    return view('veralumno',['nombre'=>$nombre,'cal'=>$cal]);
}

In the formalumno.php form:

<html>
<head>
<title>Formulario Alumno</title>
</head>
<body>
<form name='formulario_alumno' action='formalumno' method='post'>
    <div>
       <label for="nombre">Nombre:</label>
        <input type="text" name="nombre" value="<?php $nombre ?>"/>
    </div>
    <div>
        <label for="nota">Nota:</label>
        <input type="number" name="nota" value="<?php $nota ?>"/>
    </div>
    <div class="button">
        <input type="submit" name="enviar" value="ENVIAR"><br>
    </div>
</form>
</body>
</html>

And in veralumno.php:

<?php
    echo $nombre." ".$cal;
?>

I get an error in the driver:

  

"Parse error: syntax error, unexpected '$ name' (T_VARIABLE),   expect function (T_FUNCTION) or const (T_CONST) "

    
asked by Charly Utrilla 03.02.2018 в 13:28
source

2 answers

3

I think you have a mess of concepts, let's put some order.

We declare the routes first

// Ruta para mostrar el formulario, método get
Route::get('/formalumno', 'AlumnosController@crear');
// Ruta para recibir datos del formulario y mostrarlos, método post
Route::post('/veralumno','AlumnosController@mostrar');

Notice that in the path /veralumno I do not expect data about the route (by get) ( {nombre}/{nota} ) these will be sent by the form with post. And in the path /formalumno I call the create method ( AlumnosController@crear ).

Suppose we have 2 files for the views of students in the path resources/views/alumno/ , called crear.blade.php and mostrar.blade.php . The first with the form and the second one that will show the data sent from the first.

Now in the controller we must define 2 methods to create and to show.

crear() that will call the view crear.blade.php and mostrar() that will receive the data of the form and will call the view mostrar.blade.php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
// Incluimos la clase Request 
use Illuminate\Http\Request;

class AlumnosController extends Controller
{
    // Creamos un método para mostrar el form
    public function crear()
    {
        // retornamos la vista del formulario 
    return view('alumno.crear');
    }

    // Creamos un método para recibir los datos del form
    public function mostrar(Request $request)
    {
        // retornamos la vista mostrar y le pasamos los datos que necesitemos  
    return view('alumno.mostrar')
                     ->with('nombre', $request->nombre)
                     ->with('nota', $request->nota);
                    // with() nos permite pasar variables a la vista
                    // el primer parámetros es el nombre con el que estará disponible en la vista
                    // el segundo son los datos. 
    }
}

Note: the definition of the views would remain as you have them, you just have to place them in the path resources/views/alumno/ and rename them with the names indicated above. You should also add the csrf token to the form, more info here (documentation) and here (What is it?)

The use of $_POST or $_GET in Laravel is totally unnecessary, laravel provides a class ( Request ) to handle this in a more appropriate way you can review the documentation about it here: link

    
answered by 03.02.2018 / 14:34
source
1

In both routes you are called to the same method @mostrar . In the path GET formalumno you must return the view to your form. If you have already defined in your POST route indicate {name} and {note}, there is no need to use $ _POST for those variables. For example, if this is the path users / edit / {id} to load a user's edit form, which calls the edit method. The edit method also receives the id. You could define the edit method in this way:

function edit($id) {
$myID = $id;
...
}

With regard to the line return view('veralumno',['nombre'=>$nombre,'cal'=>$cal]); there is a function that you can use to send data to your view, and that is not as cumbersome as defining an array, the function is called compact and you can send as many variables as you want. In your case, with the variable compact the result would be something like:

return view('veralumno', compact('nombre', 'cal'))
    
answered by 03.02.2018 в 13:52