Here my javascript function:
var lastid = 0;
function charger() {
setTimeout( function(){
$.post(
'http://localhost:8000/chat',
{id: lastid},
function(data) {
$('#chat').prepend(data);
}
);
charger();
},5000);
}
Here my .php controller:
public function addMessagesAction(Request $request){
$id = $request->request->get('lastid');
$idall = $request->request->all();
var_dump($id);
var_dump($idall);
}
and my result from var_dump:
NULL array(0) { }
my question is:
How to get my "lastid" in my controller ?
1 Answer 1
That's probably because yo've set in JS id param, not lastid. lastid is JS variable which is assigned to id param
{id: lastid}, //{name: value}
Therefore you should change it to:
{lastid: lastid},
answered May 13, 2016 at 10:34
Jakub Matczak
15.7k5 gold badges52 silver badges68 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
default
lastidvariable from your javascript called asid, so you won't get neverlastidin you php code. If you want to getlastidyou have to send it as{lastid: lastid}in your javascript code.