Laravel route after post in same page "" - laravel

We are using Laravel, we have a form post on the "/",
$router->get('/', 'AppController#show');
The form is a script that appear through a modal window, once we post the form it uses route:
$router->post('/', 'AppController#store');
and function
public function store()
{
return view('app');
}
How we can stay on page without any return action after the post ?
With the current method it reloads the "/" page,
Are there a way to keep the "/" page as it is without reloading after the submit button?
Apologise for my english, i hope you understand our issue.
Thanks in advance,

You will need to make an ajax call to that resource. You can do with with plain javascript or you can use jQuery for this. $.post('/').
Dependant on your version, version 5.4 has a covenant axios.post method that you can use with Vue to send your ajax requests.
For a simple app jQuery may be the best simple option for you.

Related

Get Page URL after Ajax call has been made

I am using a ajax call to update page content and update the URL accordingly. I have share buttons on the page and when I want to share the whole page I only receive the previous loaded URL.
So as an example -
http://localhost/labs/category/best-of-the-best-campaign/
is my current loaded URL. When I do a Ajax call
http://localhost/labs/tag/ecommerce/?catid=2
This is the new URL. But when I share the page I still get the previous loaded URL. which is
http://localhost/labs/category/best-of-the-best-campaign
Could anyone point to me what might be going wrong?
<?php echo $url="http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";?>
This is what I'm using to get the current URL of the page. I have a doubt that it might be due to the server request but I am not entirely sure about it and how I might change it.
Any help will be greatly appreciated.
Thanks
I think you should reinitialize the $url variable after ajax call
You can write php code in your ajax sucess function like,
<script>
......// your code
success:function()
{
// your code
<?php
$url="http://localhost/labs/tag/ecommerce/?catid=2";
and use it.
?>
}
......// your code
</script>
Why don't you use location.href to get the url of the current page in JavaScript?

Handling redirects with Codeigniter and jQuery mobile?

I'm building a mobile web app with jQuery mobile and Codeigniter & TankAuth. When a user logs in or out, they are redirected from the TankAuth login/logout controller to a page of my choice. The problem is that while the ajax navigation loads the page in question, the URL to the TankAuth controller stays the same.
There is quite a lot of code involved, so I will describe it the best I can:
Consider the variable $this->session->flashdata('prev_page') is page1.
The TankAuth login controller is called first time via auth/login, loads login form view.
The form is submitted to the same controller which then processes it and redirects to necessary page:
if ($this->tank_auth->login(
$this->form_validation->set_value('login'),
$this->form_validation->set_value('password'),
$this->form_validation->set_value('remember'),
$data['login_by_username'],
$data['login_by_email'])) {// success
redirect($this->session->flashdata('prev_page'));
}
jQuery mobile loads page1 via AJAX but the URL remains as auth/login.
How do I fix this? Please let me know if you need any more info.
If my understanding about your problem is correct, you are using ajax for authenticating mobile users and normal redirect method for big screen users.
If that's the situation, then this will do the magic:
if ($this->tank_auth->login(
$this->form_validation->set_value('login'),
$this->form_validation->set_value('password'),
$this->form_validation->set_value('remember'),
$data['login_by_username'],
$data['login_by_email']))
{// success
$this->load->library('user_agent');
if($this->agent->is_mobile()) {
echo $this->session->flashdata('prev_page');
//Now this value 'll reach ajax when authentication is success.
//Inside Ajax:
//success: function(data) {
// validate data for checking if user is logged in or not.
// if(login_is_success)
// window.location.href = data;
//}
}
else
redirect($this->session->flashdata('prev_page'));
}
I added data-url="true" to the the form in opening <form> tag in question, everything worked fine from there.

CakePHP redirect entire page instead of div

When I'm logging in with my CakePHP login function in the Users controller using ajax. The errors will be displayed in an Div above the login form. But when I have a successful login, I want to redirect the page to the homepage. When I'm using $this->redirect('home'); in the Users controller, the Div will only be updated.
How can I make sure that the entire page will be reloaded? I rather fix this problem with PHP than Javascript 'cause I'm using the Writebuffer method with the JSHelper.
Thanks!
This should be solved in client side. Because client browser gets the AJAX response and puts the output inside the DIV.
Although you can embed javascript code inside the AJAX response. For example, if login is incorrect, then put a window.location javascript code inside the output. But this is not a good solution.
Return an HTTP status code within AJAX response. If for example code is 401, then redirect.
if (data == '401') {
document.location.href="http://example.com";
}

how to make href linkable with ajax without reload the page

I have this link:
follow
when the user click on its just refresh the page and do the follow function.
it there is to do the follow without reload the page?
sorry I am so week in Ajax, and really need help
You do not need Ajax to stop the browser from reloading.
Just change return true to return false, and you will cancel the default browser behaviour of following links.
If the followUser function makes an Ajax request or not won't make a difference.

ASP.NET MVC3 - Action without reload site

I have MVC3 app with form for edit data. This for include only dropdownlist.
It is any posibility to execute Controller method (HttpPost) without reload?
If not - how Can I return current site (because I have the same form in different sites).
Yes, try XmlHttpRequest better known as AJAX to execute controllers. It's like sending a request from the browser to the server on a background thread which won't cause a page reload.
have a read of these blogs:
http://www.nikhilk.net/Ajax-MVC.aspx
http://dotnetslackers.com/articles/aspnet/ASP-NET-MVC-2-0-and-AJAX-Part-1.aspx
If you can't use ajax, to get back to the same page you can redirect the user back to the referrer url:
public ActionResult Submit()
{
// do something
return Redirect(Request.UrlReferrer.ToString());
}

Resources