How to pass a variable view to controller using JavaScript function using URL::action method in laravel 4.2












1














The choice value is assigned from a dropdownlist.That value is passing to javascript myFunction as a parameter. And i want to create a table in view using data in database. The code works fine and creates table when im not passing a parameter. But i want to pass a parameter.Help me here guys.



This is my code in view



 function myFunction($choice) {
document.getElementById("demo").innerHTML = "You selected: " + $choice;

$.get("{{ URL::action('CombinationController@readData',array('choice'=> 'choice'))}}", function (data) {
$.each(data, function (i, value) {
var tr = $("<tr/>");
tr.append($("<td/>", {
text: value.student_id
})).append($("<td/>", {
text: value.initials + " " + value.last_name
})).append($("<input />", {
type: 'checkbox', value: value.student_id
}))

$('#student').append(tr);
});
});
}


This is code in Route



 Route::get('readData/{choice}','CombinationController@readData');


This is my CombinationController



public function readData($choice)
{
if($choice==0) {
$StuPS1ch1 = CombinationSubmits::join('student', 'combination_submits.student_id', '=', 'student.student_id')
->select('combination_submits.student_id', 'student.initials', 'student.last_name')
->where(['choice_1' => 'PS1'])
->get();
}
return $StuPS1ch1;
}









share|improve this question
























  • is it in angular js or angular 5
    – LDS
    Nov 21 at 7:10
















1














The choice value is assigned from a dropdownlist.That value is passing to javascript myFunction as a parameter. And i want to create a table in view using data in database. The code works fine and creates table when im not passing a parameter. But i want to pass a parameter.Help me here guys.



This is my code in view



 function myFunction($choice) {
document.getElementById("demo").innerHTML = "You selected: " + $choice;

$.get("{{ URL::action('CombinationController@readData',array('choice'=> 'choice'))}}", function (data) {
$.each(data, function (i, value) {
var tr = $("<tr/>");
tr.append($("<td/>", {
text: value.student_id
})).append($("<td/>", {
text: value.initials + " " + value.last_name
})).append($("<input />", {
type: 'checkbox', value: value.student_id
}))

$('#student').append(tr);
});
});
}


This is code in Route



 Route::get('readData/{choice}','CombinationController@readData');


This is my CombinationController



public function readData($choice)
{
if($choice==0) {
$StuPS1ch1 = CombinationSubmits::join('student', 'combination_submits.student_id', '=', 'student.student_id')
->select('combination_submits.student_id', 'student.initials', 'student.last_name')
->where(['choice_1' => 'PS1'])
->get();
}
return $StuPS1ch1;
}









share|improve this question
























  • is it in angular js or angular 5
    – LDS
    Nov 21 at 7:10














1












1








1







The choice value is assigned from a dropdownlist.That value is passing to javascript myFunction as a parameter. And i want to create a table in view using data in database. The code works fine and creates table when im not passing a parameter. But i want to pass a parameter.Help me here guys.



This is my code in view



 function myFunction($choice) {
document.getElementById("demo").innerHTML = "You selected: " + $choice;

$.get("{{ URL::action('CombinationController@readData',array('choice'=> 'choice'))}}", function (data) {
$.each(data, function (i, value) {
var tr = $("<tr/>");
tr.append($("<td/>", {
text: value.student_id
})).append($("<td/>", {
text: value.initials + " " + value.last_name
})).append($("<input />", {
type: 'checkbox', value: value.student_id
}))

$('#student').append(tr);
});
});
}


This is code in Route



 Route::get('readData/{choice}','CombinationController@readData');


This is my CombinationController



public function readData($choice)
{
if($choice==0) {
$StuPS1ch1 = CombinationSubmits::join('student', 'combination_submits.student_id', '=', 'student.student_id')
->select('combination_submits.student_id', 'student.initials', 'student.last_name')
->where(['choice_1' => 'PS1'])
->get();
}
return $StuPS1ch1;
}









share|improve this question















The choice value is assigned from a dropdownlist.That value is passing to javascript myFunction as a parameter. And i want to create a table in view using data in database. The code works fine and creates table when im not passing a parameter. But i want to pass a parameter.Help me here guys.



This is my code in view



 function myFunction($choice) {
document.getElementById("demo").innerHTML = "You selected: " + $choice;

$.get("{{ URL::action('CombinationController@readData',array('choice'=> 'choice'))}}", function (data) {
$.each(data, function (i, value) {
var tr = $("<tr/>");
tr.append($("<td/>", {
text: value.student_id
})).append($("<td/>", {
text: value.initials + " " + value.last_name
})).append($("<input />", {
type: 'checkbox', value: value.student_id
}))

$('#student').append(tr);
});
});
}


This is code in Route



 Route::get('readData/{choice}','CombinationController@readData');


This is my CombinationController



public function readData($choice)
{
if($choice==0) {
$StuPS1ch1 = CombinationSubmits::join('student', 'combination_submits.student_id', '=', 'student.student_id')
->select('combination_submits.student_id', 'student.initials', 'student.last_name')
->where(['choice_1' => 'PS1'])
->get();
}
return $StuPS1ch1;
}






javascript laravel laravel-4






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 at 3:34









CozyAzure

5,33041836




5,33041836










asked Nov 21 at 3:11









wow

62




62












  • is it in angular js or angular 5
    – LDS
    Nov 21 at 7:10


















  • is it in angular js or angular 5
    – LDS
    Nov 21 at 7:10
















is it in angular js or angular 5
– LDS
Nov 21 at 7:10




is it in angular js or angular 5
– LDS
Nov 21 at 7:10












1 Answer
1






active

oldest

votes


















0














When your view is rendered, php has already finished compilind all the code. By the time your javascript function runs (client side), your URL (generated by PHP on the server side) is already a string, so you won't have the effect you want.



If you try to generate a url with dinamic segments, like this:



URL::action('CombinationController@readData',array('choice'=> $choice))


You'll probably get an error, because PHP's URL facade requires the $choice variable to be available on render.



I sugest changing the variable to a query string, so you can do something like this:



function myFunction(choice) {
document.getElementById("demo").innerHTML = "You selected: " + choice;

$.get("{{ URL::action('CombinationController@readData') }}?choice=" + choice, function (data) {
$.each(data, function (i, value) {
var tr = $("<tr/>");
tr.append($("<td/>", {
text: value.student_id
})).append($("<td/>", {
text: value.initials + " " + value.last_name
})).append($("<input />", {
type: 'checkbox', value: value.student_id
}));

$('#student').append(tr);
});
});
}


Also you'll have to change your controller:



public function readData()
{
$choice = Request::input('choice');
if($choice==0) {
$StuPS1ch1 = CombinationSubmits::join('student', 'combination_submits.student_id', '=', 'student.student_id')
->select('combination_submits.student_id', 'student.initials', 'student.last_name')
->where(['choice_1' => 'PS1'])
->get();
}
return $StuPS1ch1;
}





share|improve this answer























  • Arthur Samarcos, There is a error in curly brackets in your answer.I couldn't identify it.can you correct it.Thank you
    – wow
    Nov 23 at 11:51










  • @wow Fixed the code.
    – Arthur Samarcos
    Nov 23 at 15:41










  • The have changed the code according to ur code. bt its not working. could you explain the all changes that i should done in the code
    – wow
    Nov 23 at 16:25










  • What exactly is not working. Please give some details.
    – Arthur Samarcos
    Nov 23 at 16:27










  • The value for choice is assigning when selecting the dropdown list. Though that value is printing in demo,the table is not creating. should i change the code in controller as well as route file.
    – wow
    Nov 23 at 17:46











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53404761%2fhow-to-pass-a-variable-view-to-controller-using-javascript-function-using-urla%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














When your view is rendered, php has already finished compilind all the code. By the time your javascript function runs (client side), your URL (generated by PHP on the server side) is already a string, so you won't have the effect you want.



If you try to generate a url with dinamic segments, like this:



URL::action('CombinationController@readData',array('choice'=> $choice))


You'll probably get an error, because PHP's URL facade requires the $choice variable to be available on render.



I sugest changing the variable to a query string, so you can do something like this:



function myFunction(choice) {
document.getElementById("demo").innerHTML = "You selected: " + choice;

$.get("{{ URL::action('CombinationController@readData') }}?choice=" + choice, function (data) {
$.each(data, function (i, value) {
var tr = $("<tr/>");
tr.append($("<td/>", {
text: value.student_id
})).append($("<td/>", {
text: value.initials + " " + value.last_name
})).append($("<input />", {
type: 'checkbox', value: value.student_id
}));

$('#student').append(tr);
});
});
}


Also you'll have to change your controller:



public function readData()
{
$choice = Request::input('choice');
if($choice==0) {
$StuPS1ch1 = CombinationSubmits::join('student', 'combination_submits.student_id', '=', 'student.student_id')
->select('combination_submits.student_id', 'student.initials', 'student.last_name')
->where(['choice_1' => 'PS1'])
->get();
}
return $StuPS1ch1;
}





share|improve this answer























  • Arthur Samarcos, There is a error in curly brackets in your answer.I couldn't identify it.can you correct it.Thank you
    – wow
    Nov 23 at 11:51










  • @wow Fixed the code.
    – Arthur Samarcos
    Nov 23 at 15:41










  • The have changed the code according to ur code. bt its not working. could you explain the all changes that i should done in the code
    – wow
    Nov 23 at 16:25










  • What exactly is not working. Please give some details.
    – Arthur Samarcos
    Nov 23 at 16:27










  • The value for choice is assigning when selecting the dropdown list. Though that value is printing in demo,the table is not creating. should i change the code in controller as well as route file.
    – wow
    Nov 23 at 17:46
















0














When your view is rendered, php has already finished compilind all the code. By the time your javascript function runs (client side), your URL (generated by PHP on the server side) is already a string, so you won't have the effect you want.



If you try to generate a url with dinamic segments, like this:



URL::action('CombinationController@readData',array('choice'=> $choice))


You'll probably get an error, because PHP's URL facade requires the $choice variable to be available on render.



I sugest changing the variable to a query string, so you can do something like this:



function myFunction(choice) {
document.getElementById("demo").innerHTML = "You selected: " + choice;

$.get("{{ URL::action('CombinationController@readData') }}?choice=" + choice, function (data) {
$.each(data, function (i, value) {
var tr = $("<tr/>");
tr.append($("<td/>", {
text: value.student_id
})).append($("<td/>", {
text: value.initials + " " + value.last_name
})).append($("<input />", {
type: 'checkbox', value: value.student_id
}));

$('#student').append(tr);
});
});
}


Also you'll have to change your controller:



public function readData()
{
$choice = Request::input('choice');
if($choice==0) {
$StuPS1ch1 = CombinationSubmits::join('student', 'combination_submits.student_id', '=', 'student.student_id')
->select('combination_submits.student_id', 'student.initials', 'student.last_name')
->where(['choice_1' => 'PS1'])
->get();
}
return $StuPS1ch1;
}





share|improve this answer























  • Arthur Samarcos, There is a error in curly brackets in your answer.I couldn't identify it.can you correct it.Thank you
    – wow
    Nov 23 at 11:51










  • @wow Fixed the code.
    – Arthur Samarcos
    Nov 23 at 15:41










  • The have changed the code according to ur code. bt its not working. could you explain the all changes that i should done in the code
    – wow
    Nov 23 at 16:25










  • What exactly is not working. Please give some details.
    – Arthur Samarcos
    Nov 23 at 16:27










  • The value for choice is assigning when selecting the dropdown list. Though that value is printing in demo,the table is not creating. should i change the code in controller as well as route file.
    – wow
    Nov 23 at 17:46














0












0








0






When your view is rendered, php has already finished compilind all the code. By the time your javascript function runs (client side), your URL (generated by PHP on the server side) is already a string, so you won't have the effect you want.



If you try to generate a url with dinamic segments, like this:



URL::action('CombinationController@readData',array('choice'=> $choice))


You'll probably get an error, because PHP's URL facade requires the $choice variable to be available on render.



I sugest changing the variable to a query string, so you can do something like this:



function myFunction(choice) {
document.getElementById("demo").innerHTML = "You selected: " + choice;

$.get("{{ URL::action('CombinationController@readData') }}?choice=" + choice, function (data) {
$.each(data, function (i, value) {
var tr = $("<tr/>");
tr.append($("<td/>", {
text: value.student_id
})).append($("<td/>", {
text: value.initials + " " + value.last_name
})).append($("<input />", {
type: 'checkbox', value: value.student_id
}));

$('#student').append(tr);
});
});
}


Also you'll have to change your controller:



public function readData()
{
$choice = Request::input('choice');
if($choice==0) {
$StuPS1ch1 = CombinationSubmits::join('student', 'combination_submits.student_id', '=', 'student.student_id')
->select('combination_submits.student_id', 'student.initials', 'student.last_name')
->where(['choice_1' => 'PS1'])
->get();
}
return $StuPS1ch1;
}





share|improve this answer














When your view is rendered, php has already finished compilind all the code. By the time your javascript function runs (client side), your URL (generated by PHP on the server side) is already a string, so you won't have the effect you want.



If you try to generate a url with dinamic segments, like this:



URL::action('CombinationController@readData',array('choice'=> $choice))


You'll probably get an error, because PHP's URL facade requires the $choice variable to be available on render.



I sugest changing the variable to a query string, so you can do something like this:



function myFunction(choice) {
document.getElementById("demo").innerHTML = "You selected: " + choice;

$.get("{{ URL::action('CombinationController@readData') }}?choice=" + choice, function (data) {
$.each(data, function (i, value) {
var tr = $("<tr/>");
tr.append($("<td/>", {
text: value.student_id
})).append($("<td/>", {
text: value.initials + " " + value.last_name
})).append($("<input />", {
type: 'checkbox', value: value.student_id
}));

$('#student').append(tr);
});
});
}


Also you'll have to change your controller:



public function readData()
{
$choice = Request::input('choice');
if($choice==0) {
$StuPS1ch1 = CombinationSubmits::join('student', 'combination_submits.student_id', '=', 'student.student_id')
->select('combination_submits.student_id', 'student.initials', 'student.last_name')
->where(['choice_1' => 'PS1'])
->get();
}
return $StuPS1ch1;
}






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 23 at 18:09

























answered Nov 21 at 23:43









Arthur Samarcos

2,3551420




2,3551420












  • Arthur Samarcos, There is a error in curly brackets in your answer.I couldn't identify it.can you correct it.Thank you
    – wow
    Nov 23 at 11:51










  • @wow Fixed the code.
    – Arthur Samarcos
    Nov 23 at 15:41










  • The have changed the code according to ur code. bt its not working. could you explain the all changes that i should done in the code
    – wow
    Nov 23 at 16:25










  • What exactly is not working. Please give some details.
    – Arthur Samarcos
    Nov 23 at 16:27










  • The value for choice is assigning when selecting the dropdown list. Though that value is printing in demo,the table is not creating. should i change the code in controller as well as route file.
    – wow
    Nov 23 at 17:46


















  • Arthur Samarcos, There is a error in curly brackets in your answer.I couldn't identify it.can you correct it.Thank you
    – wow
    Nov 23 at 11:51










  • @wow Fixed the code.
    – Arthur Samarcos
    Nov 23 at 15:41










  • The have changed the code according to ur code. bt its not working. could you explain the all changes that i should done in the code
    – wow
    Nov 23 at 16:25










  • What exactly is not working. Please give some details.
    – Arthur Samarcos
    Nov 23 at 16:27










  • The value for choice is assigning when selecting the dropdown list. Though that value is printing in demo,the table is not creating. should i change the code in controller as well as route file.
    – wow
    Nov 23 at 17:46
















Arthur Samarcos, There is a error in curly brackets in your answer.I couldn't identify it.can you correct it.Thank you
– wow
Nov 23 at 11:51




Arthur Samarcos, There is a error in curly brackets in your answer.I couldn't identify it.can you correct it.Thank you
– wow
Nov 23 at 11:51












@wow Fixed the code.
– Arthur Samarcos
Nov 23 at 15:41




@wow Fixed the code.
– Arthur Samarcos
Nov 23 at 15:41












The have changed the code according to ur code. bt its not working. could you explain the all changes that i should done in the code
– wow
Nov 23 at 16:25




The have changed the code according to ur code. bt its not working. could you explain the all changes that i should done in the code
– wow
Nov 23 at 16:25












What exactly is not working. Please give some details.
– Arthur Samarcos
Nov 23 at 16:27




What exactly is not working. Please give some details.
– Arthur Samarcos
Nov 23 at 16:27












The value for choice is assigning when selecting the dropdown list. Though that value is printing in demo,the table is not creating. should i change the code in controller as well as route file.
– wow
Nov 23 at 17:46




The value for choice is assigning when selecting the dropdown list. Though that value is printing in demo,the table is not creating. should i change the code in controller as well as route file.
– wow
Nov 23 at 17:46


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53404761%2fhow-to-pass-a-variable-view-to-controller-using-javascript-function-using-urla%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

Refactoring coordinates for Minecraft Pi buildings written in Python