Ryan hipkiss
Ryan hipkiss

Reputation: 49

Laravel Post Form data

I am having trouble taking data from a form in my laravel app, and displaying it using another method in the same controller.

This is my FormController

class ForumController extends Controller {
+
+   private $name,
+           $description;
+
+   public function __construct() {
+       $this->middleware('auth');
+   }
+
+   /**
+    * Show the form for creating a new resource.
+    *
+    * @return Response
+    */
+   public function create()
+   {
+       return view('forum.create');        
+
+       $this->name = Input::post('name');  
+   }
+
+   /**
+    * Show the details of their input once its created
+    */
+   public function created()
+   {   
+       var_dump($this->name);
+   }
+}

This is my Routes.php

Route::get('/group/create', 'ForumController@create');
+Route::post('/group/create', 'ForumController@created');

This is my forum create view.

@extends('app')
+
+@section('content')
+   
+   <div class="container">
+       <h1>Create a Forum</h1>
+       <hr>
+       
+       <div class="panel panel-default">
+           <div class="panel-heading">Create your own forum here!</div>
+           
+           <div class="panel-body">
+               @if (count($errors) > 0)
+                   <div class="alert alert-danger">
+                       <strong>Whoops!</strong>There were some problems with your input.<br><br>
+       <ul>
+           @foreach ($errors->all() as $error)
+               <li>{{ $error }}</li>
+           @endforeach
+       </ul>
+               @endif
+               <form class="form-horizontal" method="POST" action="{{ url('/group/create') }}">
+
+                   <input type="hidden" name="_token" value="{{ csrf_token() }}">  
+                   
+                   <div class="form-group">
+                       <label class="col-md-4 control-label">Name</label>
+                       <div class="col-md-6">
+                           <input type="text" class="form-control" name="name" min="1" max="20">
+                       </div>
+                   </div>
+
+                   <div class="form-group">
+                       <label class="col-md-4 control-label">Description</label>
+                       <div class="col-md-6">
+                           <input type="text" class="form-control" name="description" min="1" max="255">
+                       </div>
+                   </div>
+                   
+                   <div class="form-group">
+                       <div class="col-md-6 col-md-offset-4">
+                           <button type="submit" class="btn btn-primary">
+                               Create
+                           </button>
+                       </div>
+                   </div>
+               </form>
+           </div>
+       </div>  
+   </div>  
+@stop

Could somebody point me in the right direction please?

Upvotes: 4

Views: 18517

Answers (2)

Matthew Brown
Matthew Brown

Reputation: 5136

Well, the point of failure on your example is that PHP is not going to persist $this->name when you load the created route. In fact, as the commenter stated, $this->name is never set because you have a return statement prior to its definition.

After you set $this->name in the create method. When you navigate to the created route, $this->name is no longer set, even if you were to move your return statement after the setting of name.

You will need to pesist the data in a database/session/cache or redirect from create method to created method passing the data.

public function create() 
{
    // Handle POST Here
    if (Input::has('name')) {
       $this->name = Input::get('name'); 

        // $this->name is redundant but even still...
        return Redirect::to('created', array('name' => $this->name));
    }

    return view('forum.create');        
}

Upvotes: 4

Marko
Marko

Reputation: 11002

$name = Input::get('name');

or check:

echo var_dump($_POST); 

You should have this code in the controller method you're posting to. And you can save the values in session or flashdata if you want something to save one request and then throw it away. And you need to redirect the view to something also a view or a route.

http://laravel.com/docs/4.2/session#flash-data

Upvotes: 2

Related Questions