I'm trying to show a template in an AJAX route like this:
ajax '/login' => sub {
my $user = params->{uname};
my $password = params->{upass};
my $db_inst = WebApp::Persistency::SQLiteDB->instance();
if ($db_inst->is_user_registered($user,$password) == 1) {
template "main_page";
} else {
return { res => 'Wrong' };
}
};
The user indeed validates but the template never shows.
When I use the template from a different route (non-ajax), it works.
Am I missing something here?
AJAX does not reload the whole page. How can you use a template if you are not reloading a page?
A rendered template could be returned to the AJAX request and injected into the page.
Related
can you help me please?
-- I've making ajax call to partial and sending the data ( productId )
$.request('onProductLoad', {
data: {'productId': 22},
update: { 'Product/card': #result}
})
how i can access this data in function onProductLoad() in the php code tab on a page that partial is placed in.
Thank you!
Add a function called onProductLoad to the page and you can access the posted data as you would normally do in a component.
title = "My Page"
url = "/"
layout = "default"
==
<?
function onProductLoad()
{
$productId = post('productId');
}
?>
==
I have a URL with a defined route which can be accessed through the browser by entering the url or through a view. I need to check if that URL was accessed using a browser(entering a url) or through a view triggered by an action of the user.
Is that a way to do that in Laravel ?
You can check for the existence of a previous url
Route::get('test', function () {
if (url()->previous() === config('app.url')) {
dd('accessed from browser');
} else {
dd('accessed from view');
}
});
Hope this helps
I have a controller that I call with Ajax.
Inside this controller I have a condition, like
if($a == 10){
return $this->redirectToRoute('form_finish');
}
But when the condition is satisfited, I don't have the redirect
You call the controller in ajax but do you manage the response.
You have to redirect from the browser if the condition is satisfied or handle the ajax response and load the content in the DOM.
I solved it by doing something like this :
In controller i put the redirect handler in Jsonresponse :
//return $this->redirectToRoute('fos_user_security_logout');
$response = new JsonResponse(array(
'route' => $this->get('router')->generate('fos_user_security_logout'),
'message' => 'You may choose on option'));
return $response;
And in ajax
success: function(response,xhr){
//This is for the redrection in front page
if(response.route) { window.location.href=response.route;}
Hope it helps someone
I'm struggling with the render() method in Laravel 5.
When $whatever->render() is runned, it takes the controller method name as the route by default.
Example:
When i run this command in DelasController#updateFilter, the pagination route is set to whatever.com/marketplace/updateFiler?page=2, which does not make a sense to me.
Problem:
I want to keep the route as simple as whatever.com/marketplace?page=2.
Question:
Can anybody gives me a hint on how to solve this?
Thank you for your time and a discussion.
Looking forward for a reply.
I have an application in which various paginated lists are displayed in "windows" on the page and are updated via AJAX calls to the server. Here's how I did it:
Set up a route to render the whole page, something like this:
Route::get('/marketplace', function ($arguments) {
....
});
Set up a route which will return the current page of the list. For example, it might be something like this:
Route::get('/marketplace/updateFiler', function ($arguments) {
....
});
In your Javascript code for the page, you need to change the pagination links so that, instead of loading the new page with the URL for the link, it makes the AJAX request to the second route. The Javascript could look something like this:
$('ul.pagination a').on('click', function (event) {
// stop the default action
event.stopPropagation();
event.preventDefault();
// get the URL from the link
var url = $(event.currentTarget).attr('href');
// get the page number from the URL
var page = getURLParameterByName(url, 'page');
$.get('marketplace/updateFiler', { page: page }, function (data){
// do something with the response from the server
});
});
The getURLParameterByName function is simply a helper that extracts the named parameter from a URL:
var getURLParameterByName = function (url, name, defaultValue) {
// is defaultValue undefined? if so, set it to false
//
if (typeof defaultValue === "undefined") {
defaultValue = false;
}
name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
results = regex.exec(url);
return results === null ?
defaultValue :
decodeURIComponent(results[1].replace(/\+/g, " "));
};
I adapted this code from an answer I found here on Stack Overflow: https://stackoverflow.com/a/901144/2008384.
sorry for asking this question ..i am working on a Cakephp 2.x ... i have a view page in my controller name folder e.g Controller/index.ctp ... and i have ajaxfiles are stored in app/webroot/ajax/ajaxfile.html
now on my index.php file i am acessing the ajax page like this
<a href="ajax-demo/ajaxfile.html" class="file-link">
<span class="icon file-png"></span>
Simple gallery</a>
Controller
public function index(){
}
now the problem is i want to send the variables to both of my pages ... index.ctp and ajaxfile ... how can i do this ??what is the best approach to tackle these things ....
do i have to move the ajaxfiles from webroot and paste under the controller name folder?
if is it so then how can i send variables to ajax files which has no model and controller
please if any one know the solution then please advice me. and give an example too
There are different way to achieve this, here I'm writing the simplest one
First you need to move your "index.ctp" file to your "View/YOUR CONTROLLER NAME/" folder.
1) To access the variable in view you need to set it from your controller's method like this
public index(){
$this->set('yourVariable', 'Your Value');
}
2) To access the value in your view file (index.ctp), you need to call this variable like this
$yourVariable;//If you want to print this then you can write like this
echo $yourVariable;
3) To call a ajax file from your index.ctp the simplest method is to call a onclick event on this anchor, the onclick event will call a JAVASCRIPT method which will further make a ajax call and will place the output in an element in your index.ctp, The ajax call will further call your controller method (implement your html related logic here)
For example,
<span class="icon file-png"></span>Simple gallery
<div id="yourAjaxFileOutputReplaceMentDiv"></div>
4) create a javascript method in your JS file, this JS file must be loaded in your layout file.
function yourAjaxCallMethod(BaseURL,yourVarible)
{
//Initialize Ajax Method
var req = getXMLHTTP();//Let's this method Initialize your Ajax
if (req)
{
req.onreadystatechange = function() {
if (req.readyState == 4)
{
if (req.status == 200)
{
document.getElementById('yourAjaxFileOutputReplaceMentDiv').innerHTML=req.responseText;
} else {
alert("There was a problem while using XMLHTTP:\n" + req.statusText);
}
}
}
var URL = BaseURL+yourVarible+'/'+Math.random();
req.open("GET", URL, true);
req.send(null);
}
}
5) Your AJAX file related method in your controller "yourController". Set autoRender to False
public function ajaxMethod(){
$this->autoRender = false;
//Check $this->request['pass'] for arguments send from ajax call
$retreivedVariable = $this->request['pass'][0];
echo 'I retrieved variable'.$retreivedVariable;
}
However instead of writing core javascript and ajax method you can call the inbuild Ajax Helper for same.