setnumber }}, total:{{ count($ques)}}}}).done(function(data){ con... "> setnumber }}, total:{{ count($ques)}}}}).done(function(data){ con... "> setnumber }}, total:{{ count($ques)}}}}).done(function(data){ con... ">
View Feed
group-icon
PHP Programmers
Community for PHP Programmers : Ask questions, doubts and help each other get better at PHP
299 Members
Join this group to post and comment.

jquery AJAX call (Laravel 4 PHP): Unable to retrieve data on my Controller

I wrote the following code to make an AJAX post request to a function in my controller,
$.ajax({
url:"{{ URL::route('res_data') }}",
type:"POST",
data:{candidate:{{ json_encode($name)}}, ans : ans_array, stamps: timestamp,set:{{ $ques[0]->setnumber }}, total:{{ count($ques)}}}}).done(function(data){
console.log(data);}).fail(function(jqXHR, ajaxOptions, thrownError){
console.log(jqXHR);
console.log(ajaxOptions);
console.log(thrownError);});
The problem I face is the candidate key in 'data' tag.

json_encode($name)
stringify's the data, On going to the route res_data, I perform a decode to convert the data received into the object. Here's my Controller function,
publicfunction resultData(){
$res = array();
$count =0;
$res =Input::get('ans');foreach($res as $r){if($r >0)
$count = $count+1;}
$data = json_decode(Input::get('candidate'));//error on this

$entry =newResult;
$entry->id = $data->id;
$entry->name = $data->name;
$entry->gender = $data->gender;
$entry->email = $data->email;
$entry->fblink = $data->link;
$entry->submissions = $count;
$entry->setnumber =Input::get('set');
$entry->totalquestions =Input::get('total');
$entry->answers =Input::get('ans');
$entry->stamps =Input::get('stamps');
$entry->save();
return"success";
}
But, I get an error stating "json_decode() expects parameter 1 to be string, array given" The error says that the parameter is array, Hence to obtain the data as an object I tried a solution on the web and replaced the comment marked line to
$data = json_decode(json_encode(Input::get('candidate')), FALSE);
This satisfies the error I received before but now I get another error stating "preg_replace(): Parameter mismatch, pattern is a string while replacement is an array"

I don't understand this. I even tried leaving the data as array and tried accessing data as
$data['name']
but, Now another error, that says $data is non-array element. Can anyone help me with this?
is there anyone who can at least give me a suggestion?
figured out 😒
Vishal0203
figured out 😒
dataType option?
Kaustubh Katdare
Kaustubh Katdare • Oct 21, 2014
Vishal0203
figured out 😒
Sorry that no concrete replies were received in time? Could you please share the fix for the readers?

Share this content on your social channels -