Laravel blade pass Javascript variable in php -
how can pass javascript variable variable in php loop:
something this(obviously not work):
var myjsvar = 100; @for ($i = 0; $i<myjsvar; $i++) ... code @endfor
further tried solving ajax:
/** * slider value */ $.ajax({ type: 'get', url: myurl, data: myjsvar, success: function (option) { console.log(myjsvar); } });
it returns me success function,
further did in controller:
public function prod(request $request) { if ($request->ajax()) { $ajax = "ajax"; dd($ajax); } else { $ajaxn = "no ajax"; dd($ajaxn); } }
it did not work.
i not sure how proceed, hope help.
php has finished doing work before page hits browser, passing variable javascript php without doing request impossible. consider
a) moving loop javascript. consider using ui library vue.js, angular or react.
b) move contents of myjsvar
php. if depends on user input or browser rendering, impossible.
c) performing rendering logic through ajax-request
$.ajax({ type: 'get', url: myurl, headers: {'x-requested-with': 'xmlhttprequest'}, data: {value: myjsvar}, success: function (response) { $(somecontainer).html(response); } });
and in controller:
public function prod() { $value = request::get('value'); return view('view-with-a-loop')->with('value', $value); }
be careful latter method xss-wise.
Comments
Post a Comment