-1

I have an html with a script that is like so (btw, HAVe to use old fashioned post in my html for reasons)...

@extends('layout')
// ... includes for jquery and ajax
<script>
var theVariableINeedInLaravel = "SomeInterestingStringI'mSure"; // in reality, this is a stringify.
$.post ("foo", function(theVariableINeedInLaravel) {
}
</script>
@stop

Then in routes.php...

<?php
Route::post('foo', 'ThatOneController@getValue');
?>

Then, in the related controller.... ThatOneController.php

class ThatOneController extends \BaseController{
    public function getValue(){
    error_log(print_r($_POST,true)); // returns nothing.
    error_log(print_r(input::all()); // returns nothing.
}

}

Or, an alternate version of the function...

public function getValue(Request $request){
error_log(print_r($request->all()); // returns nothing.

}

None of them seem to work. How can I get my post variable?

lilHar
  • 1,735
  • 3
  • 21
  • 35

2 Answers2

1

try this

use Request;
class ThatOneController extends \BaseController{

public function getValue(){
  print_r(Request::all());
}
mdamia
  • 4,447
  • 1
  • 24
  • 23
  • I found this out the hard way before I noticed your response, but it's better than original solution I posted. – lilHar Sep 02 '15 at 16:27
0

Turns out that even if $_post isn't always accessible from inside a controller function, it is directly accessible from Routes. It's a bit hacky, and "not the laravel way" but you can use $_post in routes to get and pass into other variables to get back into the normal flow.

lilHar
  • 1,735
  • 3
  • 21
  • 35
  • That is not true, `$_POST` is global and can be accessed from anywhere. can you post your full js ? – mdamia Aug 21 '15 at 01:11
  • I found out the hard way, but laravel actually can sometimes clear out $_POST before you get to the target page, so it's no longer accessible to internal pages, and you have to go through ::all to get at it. I also found Request::all got me my data as well if $_POST was returning empty, despite javascript passing by $.post – lilHar Sep 02 '15 at 16:26