This seems like a relatively simple question, and I am pretty new to using ajax, but I saw a post about being able to call a PHP script when an OnClick event has been triggered. I am trying to destroy a session upon a user clicking the "Log Out" link, so I want to call the logout.php file when the OnClick event has occurred. My problem is that nothing happens when the link is clicked.
Here's my code:
$(document).ready(function() {
// Expand Panel
$("#open").click(function(){
//when the user clicks the logout button
$.ajax({
url: "logout.php"
)};
});
});
<div class="tab">
<ul class="login">
<li class="left"> </li>
<li><?php if(isset($_SESSION['username'])){echo $_SESSION['username'];}else{echo 'Hello Guest!';}?></li>
<li class="sep">|</li>
<li id="toggle">
<a id="open" class="open" href="#">Log Out</a>
</li>
<li class="right"> </li>
</ul>
</div> <!-- / top -->
I suppose that you expect the output of the logout.php file to show. I think that your code might be working but that you forgot to tell the ajax where to put the data that it receives.
Try something like this:
$.ajax({
url: 'logout.php',
success: function(data) {
$('.result').html(data);
alert('Logout completed.');
}
});
Where "result" points to the div you want to load the response into.
Using $.ajax isn't the same as clicking regular links. $.ajax doesn't "follow" the link, it loads the data from that link into the document without refreshing the page.
What you need to do is do what you want in your logout.php file and then you sent information on whether logout succeeded or not. You do that by outputting HTML, XML or JSON, which you check in your javascript code and take appropriate action (redirect user, lock down the UI or whatever).
Try doing this:
$(document).ready(function() {
alert('loaded!')
// Expand Panel
$("#open").click(function(){
//when the user clicks the logout button
$.ajax({
url: "logout.php"
)};
alert('clicked!')
});
});
If this displays a popup saying clicked! then the event is being triggered. Tell me what happens so we can narrow it down to the $.ajax failing or the click event not being triggered.
If you are using firebug then you can go to the console panel and inspect XmlHttpRequests to closely look at any responses coming back from the remote script, if you have sent any back from the script that is.
Related
I am working with a mobile application in which
first page has
Html
<ul>
<li>121212</li>
<li>123233</li>
<li>232323</li>
<li>4323423</li>
<ul>
when user click on "li" then he/she entered on next page which will retrieve data related to selected "li" via Ajax.
this is almost going good..
But when Ajax response come page is fluctuating 2 times.
Means one time page loading, next time page totally white and then again showing page with Ajax response.
Why ???
J query
$("clickOnLi").click(function(){
var id= $(this).val(); //get the selected li value
$('.loadingGif').css({ 'display':'block' });
$("#ulShowContent").html(''); // to remove old inner HTML to show new result html
var dataString = 'selectedid='+id;
$.ajax({
type: "POST",
url: remoteUrl+"handler.php",
data : dataString,
cache: true,
success: function(response) {
if(response){
$('.loadingGif').css({ 'display':'none' });
$("#ulShowContent").html(response);
}
}
});
})
**and the result will show in this html**
<ul id="ulShowContent" data-role="listview">
<li class="comment chatsend">
<div class="comment-meta">
data 1
</div>
</li>
<li class="comment chatsend">
<div class="comment-meta">
data 2
</div>
</li>
</ul>
You will need to change how you deal a page change and AJAX call.
What I have understand from your question, after click on LI element, page change is initialized and AJAX call is sent to a PHP server.
You will need to change this logic. Page fluctuations are cause by AJAX call which is executed during the transition from one page to an another.
This can be fixed like this:
On a first page remove HREF attribute from a list element
Add a click event to every list element
AJAX call should be executed on click event, at a same time show your custom loader (or use a default one)
When server side data is retrieved you need to store it so it can be accessed from an another page. Here you will find my other ANSWER where you can find various methods of storing data during page transitions, or find it HERE, just search for a chapter called: Data/Parameters manipulation between page transitions (your best bet is a localstorage).
Initialize a page change with changePage function
During a pagebeforeshow event (page is not yet displayed) append new data to the new container
When second page is finally shown everything will be there and
I have a form that when I sumbit loads a new section of the form, but what happens is the page loads and sends me back up to the top of my page so I have to scroll all the way back down to where the form is.
Is there a way I can keep the page in the same section it is in or maybe have it load next to one of my sub-header text title I show?
This is the button I have showing:
echo $this->Form->submit("View Now");
But how could I get this to load next to this sub-header text below instead of going back to the top of the page?
<h2><?php __("Information Results");?></h2>
Thank you!
try this code:
echo $this->Form->submit("View Now",array('id'=>'contact_btn'));
<div id='result'><?php __("Information Results");?></div>
<script>
$.ajax({data:$("#contact_btn").closest("form").serialize(),
dataType:"html",
success:function (data, textStatus)
{$("#result").html(data);},
type:"post",
url:"\/yourproject\/controller\/action"
});
</script>
The Ajax is the proper way, but you could also use anchors. For example you can make your form action to be this way:
<?php echo $this->Form->create(
'YourModel',
array(array('#'=>'subform')
);?>
Then above the h2, you should put:
<a name="subform"></a>
<h2><?php __("Information Results");?></h2>
And when submit the form it will be positioned exactly where you wanted. But it's an alternative of the Ajax. I would do it with Ajax.
The bonus (if it could be counted as such) your logic is in the same action.
I have following code in my php page. These are two tabs Inbox and sInbox. When user clicks on them it refreshes whole page to go to other tab. Is there a way to make these tabs so that when user click on one of the tab there is no page refresh? When answering please provide full code example as i am a beginner.
<ul id="topTabs">
<li class="selected"> Inbox </li>
<li> sInbox </li>
</ul>
One of the simplest ways would be using jQuery UI tabs. You can find a nice detailed beginner-friendly tutorial here.
I wouldn't say that you need ajax to do this. Ajax is only needed when you want to load content of these tabs dynamically.
Do realize your tab-switching you can use jQuery or simply the style-property "display:block" resp. "display:none".
It's very hard to provide some code for your question (because you need to change your page structure) but as a hint, you need to use jQuery ajax or php ajax update panel (if there is something like this - I know there is one for ASP.NET) to refresh some parts of your page (instead of whole).
In the earlier case (jQuery) your links will not be redirection links and will have a click action associated to them which requests for page update using ajax.
see the link for examples of using ajax using jQuery.
You can do it easly using ajax:
$(document).ready(function() {
$("#topTabs li").click(function() {
var mode = $(this).find("a").attr("href");
$.ajax({
url: 'contentprovider.php',
data: {mode:mode},
success: function(data){
$('#tab1').html(data);
}
});
});
});
Where tab1 is the ID of the element wich may receive the content from contentprovider.php
More info on jQuery Ajax at jQuery Page.
gl
Paulo Bueno.
In order to show you what I want to do you just have to visit gmail. When you click on the inbox, the url refreshes to this ?tab=mm#inbox and the only part of the page that refreshes is the big part where your e-mails are which google calls div.l.m . How is that possible? Are they using cache a lot or they are using a javascript command I'm not aware of?
What I want to do is, I have a page with two different tabs.
<div id="navcontainer">
<ul id="navlist">
<li id="active">Products</li>
<li><a id="prreq" onclick="show()" >Requests</a></li>
</ul>
</div>
<div class="container"></div>
When users go on eg. cart.php they are going to the first tab. When users click on the second tab a js function is triggered which calls the file cart.php?rq=r and the results are shown in the container div. (I know that at the moment I have post)
function show(){
var prstr= ".container";
var data= {
rq: "r"
};
$.ajax({
type: 'POST',
url: "cart.php",
data: data,
success: function(data1)
{
$(prstr).html(data1);
}
});
}
What I want is when the user refreshes the page to still be in the cart.php?rq=r and not having to click on the tab again.
I'd appreciate any help. if you need any more information please let me know
Thank you in advance.
They are simply accessing the hash component of the url via location.hash. When the hash changes, they must have some logic that determines which part of the page to refresh.
I want to know how to get different login interface for my web without change to the URL name. Example
<ul>
<li>Login</li>
</ul>
If user click on the Login I want to make it display on Ajax popup. Just show the login & password form. I'm using ColorBox. Example Outside HTML (Ajax)
If user type http://domain.com/login/ that page will include header, footer and etc.
Let me know what to put in my login.php to make it to be different.
You don't need to put any changes to your http://domain.com/login (potentially). You just have to attach the colorBox plugin to the link. It handles the popup and the e.preventDefault() call so people won't get the popup and the page. It's usually best to give the links an Id or class (like I have below).
<ul>
<li><a class="loginlink" href="http://domain.com/login/">Login</a></li>
</ul>
$('a#loginlink').colorbox({href:"/yourLogin.html"});
Since you're using the Outside HTML example, you can do the above (replacing the filename with your actual HTML).
However, since it's not an incredibly huge piece of code to display a login form, you can include it in your page (but hidden) and pass this to the ColorBox plugin using the Inline Html example:
$('.loginlink').colorbox({
inline: true,
width: "50%",
href: "#loginBox"
});
An example of the inline HTML working: http://jsfiddle.net/jonathon/MHhNX/
Try to use URL parameters: http://domain.com/login/?ajax=1 for ajax, and http://domain.com/login/ for full page.
HTML:
Login
<script type="text/javascript">
function ajaxLogin () {
$.ajax({url: 'http://domain.com/login/?ajax=1', ... });
}
</script>
PHP:
if (isset($_GET['ajax']) && $_GET['ajax'] == 1) {
// ajax form
} else {
// full page
}
blur ,
You have to change your html a bit assuming that you are using jquery
give a class name for this
<a href="http://domain.com/login/" class="loginpopup">Login</a
$('.loginpopup').click(function() {
// construct your html here and show as popup , or you can use existing dialog boxes
return false;
});
Nothing would need to change in login.php. You'd need to add an onclick handler to your link however.
Take a look at jquery to help you out with this: http://api.jquery.com/click/