When a link is clicked, I want to clear the php session before redirecting to the destination page.
The way I thought of doing it is by loading a php script that clears the session with ajax in the origin page and once that script has loaded, to redirect to the destination page.
Here is the code I have:
$(document).ready(function() {
$("a").click(function(){
var clearSession = "clearsession.php";
$("#content").load(clearSession,"");
});
});
<a href="destination.html">
Currently it seems to follow the link before the php script completes loading.
A few guidelines :
You can't make the link point to any other page than "destination.html"
You can't add variables to the destination page either (destination.html?clear)
Use the load callback so you can execute the redirection after you receive a response.
$(document).ready(function() {
$("a").click(function(){
var clearSession = "clearsession.php";
$("#content").load(clearSession,"",function(){
window.location = $(this).attr("href");
return false;
});
});
});
<a href="destination.html">
Why not just redirect to clearsession.php?link=destination.html, which clears the session and then re-redirects to the desired URL?
$(document).ready(function() {
$("a").click(function(){
var clearSession = "clearsession.php";
$("#content").load(clearSession,"");
return false;
});
});
With "return false;", the browser won't follow the link.
Related
Need help..
I have an index.php page. On that page, I have a link to page-2.php.
On page-2.php,
i also have a link to other page. May I know how to load the link on page-2.php in a same div on index.php page.
I'm using jQuery load method do to this. I'm able to load page-2.php inside a div on index.php. Below are the codes that I made.
$(document).ready(function() {
$('a#staff_info').click(function(e){ //link to page-2.php
e.preventDefault();
var page = $(this).attr('href');
$('#content').load(page);
$('a#upd_staff').click(function(){ //link on page2.php
var page_info = $('a#upd_staff').attr('href');
$('#content').load(page_info);
});
});
});
the above code will direct me to a new page not on a same div
Ajax is asynchronous.
$('#content').load(page);
The above starts loading page two
$('a#upd_staff').click(function(){ //link on page2.php
Then you try to bind your event handler to content from page 2 before it is has finished loading.
So when you click on the link, there is no JS bound to it, so it acts as a normal link.
Use deferred event handlers (so they listen on the document and check what element was clicked when the click happens).
$(document).ready(function() {
function ajaxLoad(event) {
event.preventDefault();
var page = $(this).attr('href');
$('#content').load(page);
}
$(document).on("click", "a#staff_info, a#upd_staff", ajaxLoad);
});
when you click on $('a#staff_info') this -- I can see that you use e.preventDefault(); for preventing the normal action of the anchor tag but when you click on $('a#upd_staff') this you did not use e.preventDefault(); -- that's why this redirect to page2.php. Use e.preventDefault(); - this for next one click may be this will help you
You can use get() for add directly on your div:
$(document).ready(function() {
$('a#staff_info').click(function(e){ //link to page-2.php
e.preventDefault();
var page = $(this).attr('href');
$.get(page, function(data, status){
//action on page loaded, data is your page result
$('#content').html(data)
});
$('a#upd_staff').click(function(){ //link on page2.php
var page_info = $('a#upd_staff').attr('href');
$('#content').load(page_info);
});
});
});
A have following code:
if (isset($_SESSION['login'])) {
print("<a href=\"add-to-cart.php?uuid=". $Item->PartId ."&price=". $Item->Price."\" >Add to cart</a>");
}
I heard about Ajax(sending form without page reload, e.t.c)
So as you see I have link a href
to add-to-cart.php?uuid=someID&price=someprice
This code works, and PHP script adds to DataBase record, and navigates to this url. But I dont need to navigate to this url.
So How could I do this, executing PHP with these dynamics parameters and prevent navigating to this URL?
But how could I
You have to add a javascript click handler to the link so that users send AJAX-request instead of navigating to the URL.
Here's a snippet using jQuery (assuming that your links have lnk-ajax class):
<script>
$(function() {
// Send AJAX request instead of navigating to the link's URL
$('.lnk-ajax').click(function(e) {
var linkUrl = this.href;
// Send AJAX request
$.ajax(linkUrl).done(function(data) {
// process the result
// ... put your code here ...
alert('Success');
});
// Prevent default link behaviour
e.preventDefault();
});
});
</script>
The Problem
I can't seem to get the javascript to redirect after the php has echoed "Success".
Im basically just trying to make it where the javascript post my form data and then the usesr is redirected to their account page. I did have this being done via php, but I wanted the luxury of being able to stay on the same page while logging in.
The Javascript
<script>
$(document).ready(function() {
//When the form with id="myform" is submitted...
$("#login_form").submit(function() {
//Send the serialized data to formProcessor.php.
$.post("login.php", $("#login_form").serialize(),
//Take our repsonse, and replace whatever is in the "formResponse"
//div with it.
function(data) {
if(data == "Success"){
location.href="home";
}
}
);
return false;
});
});
</script>
You redirect using document.location or window.location not location.href
window.location = "home.html";
// or
document.location="home.html"; //Will not work on older IE Version
Note:
document.location was originally a read-only property which Gecko & Webkit browsers allowed to edit. For cross browser compatibility use window.location as it a read/write property.
You can try any one of this
Method 1:
window.location.replace("home.html");
Method 2:
window.location.href = "home.html";
Method 3:
$(location).attr('href',"home.html");
Hope this will help you.
I have the page (index.php) that contain one link with id="t1" and one div id="container".
<a id="t1" title="Users" href="utenti.php">Users</a>
<div id="container">
<!--Where does the contained-->
</div>
When I click the link I get a built-in external page (utenti.php)
This is code of Ajax:
$(document).ready(function(){
$("#t1").click(function(){
//$("#container").hide();
$("#container").load($(this).attr("href"), function(){
$("#container").show();
event.preventDefault();
});
return false;
});
});
and so far all is well, but if I click a link in the page that is included (utenti.php) the page is not incorporated in the same div but is opened in a direct manner.
I tried to put in the file .JS the code Ajax, to recognize the ID of the links transforming into:
$(document).ready(function(){
$("#t1").click(function(){
//$("#container").hide();
$("#container").load($(this).attr("href"), function(){
$("#container").show();
event.preventDefault();
});
return false;
});
$("#click").click(function(){
//$("#container").hide();
$("#container").load($(this).attr("href"), function(){
$("#container").show();
event.preventDefault();
});
return false;
});
});
Where #click is id of the link contained in the page Utenti.php,but even so
I do not incorporate the page in the div.
I also tried to include directly the page with the script inside the page utenti.php but I recognize the first link (which is fine) but everyone else does!(not all other!)
If I understand correctly this system does not include actually the page as would the classic include of PHP.
He gives him also the values already included in the index is more a kind of visualization,only that I haven't understand how to pass the value of the links contained in utenti.php inside the main page index.php.
It should be
$(document).on("click","#t1",function(e){
e.preventDefault();
var href = $(this).attr("href");
$("#container").load(href);
});
If you want links on the page that is opened in the div to also be targeted to the div you can try a delegated click handler on the div itself.
$(document).ready(function () {
$("#t1").click(openLinksInDiv);
$("#container").on("click", "a", openLinksInDiv);
function openLinksInDiv(event) {
//$("#container").hide();
$("#container").load($(this).attr("href"), function () {
$("#container").show();
});
event.preventDefault();
}
});
Demo: http://jsfiddle.net/AN56C/
Or if you want all links on the page to open in that div, use:
$(document).on("click", "a", openLinksInDiv);
Noting that loading pages via Ajax won't work for any and all pages from external domains, but will work for pages from your own domain.
This whole thing seems to be the perfect case for an iframe rather than a div, in which case you could have links to external domains.
I am attempting the following:
When a user clicks a link, I want to update a specific column of a specific table in the database via ajax. BUT I still want the user to be redirected to the href="<url>" of the link.
I tried jQuery without return false; but then the ajax doesn't work.
I tried with return false; but then the page obviously doesn't redirect to the url as I wanted.
Thanks in advance for your help.
Do your AJAX call, then set document.location when done.
$('a').click(function(e){
var href = this.href; // get href from link
e.preventDefault(); // don't follow the link
$.ajax({
url: '/path/to/site',
data: {some: data},
success: function(){
document.location = href; // redirect browser to link
}
});
});
You need to serialize these actions yourself. In the event handler first run your AJAX request, then lookup the href from the tag and use a location redirect to take the client to that URL.
Put the redirect code in the success callback:
$.ajax({
...
success: function(){
$(location).attr('href',url); // You URL inserted
}
});