I sent a ajax request to get data from controller with json format. something like this-
return response()->json($data);
I received the response from laravel view with a variable like this-
var rpr_data_id = response.id;
Now I want to pass this variable to one of my data. Button is written in this format-
<button>send</button>
Question: how to pass the js variable in laravel blade anchor tag?
You can use the attr() method for that.
Assuming your link can be easily selected:
<a id="your-anchor-id" href="{{ url('rpr/') }}">send</a>
$('#your-anchor-id').attr('href', $('#your-anchor-id').attr('href') + response.id)
On a side note, you shouldn't put a <button> inside an <a>.
Related
I'm working on a laravel project and am trying to pass an array from a controller to javascript. The following code is from my controller.
$dicomfilearray = $dicom->pluck('filename')->toArray();
return view('xray.view')->withDicomfilearray($dicomfilearray);
And below is the Javascript in that's in the blade file I'm trying to pass it to.
var dicomarray = '{{ json_encode($dicomfilearray) }}';
console.log(dicomarray);
And the following is a log result from the Javascript.
["storage/uploads/storeid1/27/10/dicom/c4p4Oco3rU.dcm","storage/uploads/storeid1/27/10/dicom/RNil0NPPzQ.dcm"]
I would like to get a list from this array. Any advice or guidance on this would be greatly appreciated, Thanks.
You can make ajax call in frotend, and backend do like this
$dicomfilearray = json_encode($dicom->pluck('filename'))->toArray());
return view('xray.view')->withDicomfilearray($dicomfilearray);
when you working in javascript and need data in javascript then why you need view part. Actually, I just read your comment.
If in Ajax
so I suggest send array with json_encode and populate that data into view with javascript.
simply right below in controller
response()->json(['status'=>200,'data'=>$dicomfilearray])
Update
So ,you not sending ajax request so, simply. do like below.
controller:-
$data = json_encode($dicomfilearray);
return view('your-view',compact('data'));
javascript
var dicomarray = '{{ $data }}';
You can do something like this and this even works if you want to pass the variable to external javascript file. All you have to do is to call the init function with passed parameters.
<script>
$(function () {
init({{ json_encode($dicomfilearray) }} });
function init(dicomfilearray){
//use your variable here
}
</script>
I'm working with symfony2 and I need to send data from javascript function to the controller.
This works when there is no data to send in the url
document.location.href="{{path("modifMalade")}}"
But I don't know how to do to put parameter on it?
All you need is just to read the docs:
http://symfony.com/doc/current/reference/twig_reference.html#path
In your case, it could look like this:
document.location.href="{{ path("modifMalade", {'param1': 'value1'})}}"
I know how to pass data with a jinja template from python into javascript, but I want to pass a javascript variable into python. I'd like to do it without reloading the page. Is that possible?
Yes, like monkut said--I believe you want to use JSON and Javascript/jQuery.
This will let allow communication from client to server and back again.
The most applicable example I found was in the Flask snippets/patterns: http://flask.pocoo.org/docs/patterns/jquery/
I did a similar kind of work in my project and would like to share my code here. I need to find out which post is selected and I was setting the selected post as a global variable at server side, so that I may use it for later comparison. This is how I pass my selected post into Javascript.
<a class="label label-primary" onclick="myFunction({{very.id}})" > Compare</a>
Now from Javascript to Flask.
function myFunction(x) {
$.getJSON($SCRIPT_ROOT + '/check_selected', {
post: x
}, function(data) {
var response = data.result;
console.log(response);
}
});
}
This is how I return the result from flask by using JSON.
import json
#main.route('/check_selected', methods=['GET','POST'])
def check_selected():
global selected
post = request.args.get('post', 0, type=int)
return json.dumps({'selected post': str(post)});
As mentioned here, we need to include Google AJAX API in order to load jquery:
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script>window.jQuery || document.write('<script src="{{
url_for('static', filename='jquery.js') }}">\x3C/script>')</script>
Create a JSON string from your view code say, jsonData and in your Jinja Template, write something like
<script type="text/javascript">
var data = {{ jsonData }};
</script>
I'm planning on server's cherrypy with programming of python and mako.
Now i have one problem because i know to pass value of Mako template to Javascript with
<script type="text/javascript">
var contapara=${input_nparams};
</script>
and viceversa? It 's possible apply viceversa Mako<---JAvascript?
Thanks
Edit:
Because I have one variable of Mako (kwargs) that contains the data of all forms sent.
The user enters a word that is the "clave" and is stored in Javascript.
After this I pass in Mako to search in variable (kwargs).
Call the function's MAko.
After exist this I open one windows with the textarea (new form) and I have to write data.
AFter the user change data and send the form.
Make a view that takes in the arguments that you would want to send to mako, then in your javascript use ajax. for example:
jQuery.ajax(
{
url : url_of_the_mako_view,
data : {foo:bar,foo2:bar2},
type : 'POST',
success : function(data)
{
/*
when the mako template is rendered by your view then the result will
be passed to this function in the variable data
*/
}
}
)
If you are after something a little more like a page redirect then you can do something more like this:
make a form that redirects to the right place
put in a hidden field
populate the hidden field with a json string representing the arguments you want to pass to mako
submit the form
Then in the view that gets the form data you need to turn the json into a dictionary and pass that to mako
var search= document.getElementById('appMenu').value
document.location.href= '${createLink(controller: 'application' , action:'ajaxAppSearch', params: ['query': search])}'
The element appMenu is a text field, so I am getting the value that the user enters into the text box to pass into a search controller. However, it keeps telling me that the params query is null. It seems that search isn't being passed into the create link method. Anyone have a suggestion?
Grails (controllers, GSP and tags, etc) are working on server side. JavaScript on client side. And this link is prepared before sending data to browser, and before JavaScript can pass its variable into GSP tag.
But you can prepare base link on server side, and add extra parameter on client side, by using javascript, like:
var search= document.getElementById('appMenu').value;
document.location.href= '${createLink(controller: 'application' , action:'ajaxAppSearch')}?query=' + escape(search);