简体   繁体   中英

Retrieving ID from Javascript - Laravel

In my javascript code, i am able to output the id of a the selected item. I want to pass the id to my controller. I appended the input(created in the JS code) to my html form so as to get the id of the selected item but it wouldn't do the trick.

*HTML**

 <form>

// inputs for form in here

</form>

JS

 $(".myItem").append(
        '<p  name="item_id" id="item_id">Item ID: ' + item.id + '</p>').appendTo('form');

Controller

$getID = Input::get('item_id');
$fetchItem = Item::all()->where('id',$getID)->first();

Try to append an input element instead of a p :

$(".myItem").append(
        '<input  name="item_id" id="item_id" value=' + item.id + ' />).appendTo('form');

You can even have both; 1 to store the value ( input ) and the other to show it on screen ( p ).

$(".myItem").append('
  <input type="hidden"  name="item_id" id="item_id" value=' + item.id + ' />
  <p>Item ID: ' + item.id + '</p>').appendTo('form');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM