Javascript: capture value to use in form action field of Laravel app -
i have laravel app javascript function captures data i'm using dynamically fill modal box.
$(function() { $('#edit-auction-modal').on("show.bs.modal", function (e) { var auctionid = $(e.relatedtarget).data('id'); console.log(auctionid); //auctionid has correct value selected auction. needs used in view file }); });
in view file, have modal box form (to update fields).
<div id="edit-auction-modal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="mymodallabel" aria-hidden="true"> <div class="modal-dialog"> <div class="modal-content"> <div class="panel panel-info"> <div class="panel-heading"> <div class="panel-title" id="auctionlabel"></div> </div> </div> <div class="modal-body edit-content" style="padding-top:10px"> <form method="post" action="{!! route('admin_auctions_update', ['id' => auctionid ]) !!}"> {{ csrf_field() }} {{ method_field('put') }} .... </form>
using above code, nothing gets updated tries send 'auctions/auctionid/update' instead of 'auctions/1/update' or 'auctions/2/update'
how can change can use value of auction0id (from js file) in view?
should pretty trivial think js skills below average...
you try this:
$(function() { $('#edit-auction-modal').on("show.bs.modal", function (e) { var auctionid = $(e.relatedtarget).data('id'); var pattern = /(auctions\/)(.+)(\/update)/; var action = $('form').attr('action'); $('form').attr('action', action.replace(pattern, "$1" + auctionid + "$3")); }); });
explanation:
the view handled server-side , defines html sent browser. don't run php code past point. since auctionid
exists result of client-side events, tool modifying action
attribute javascript.
so, in view include js script, assume are. also, write original action {!! route('admin_auctions_update', ['id' => 0 ]) !!}
(0
default placeholder value).
finally regular expressions explanation: parentheses define capture groups, can access number. so, first 1 contains string contents before id replaced, second 1 id itself, , third 1 rest of string.
Comments
Post a Comment