jquery mobile page fluctuating on ajax reponce load - php

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

Related

newly appended elements disappear after reloading the page (jQuery)

I used jQuery's .append() to add new li elements to a ul on success in an AJAX call. The problem is, whenever I refresh the page, the newly appended list items disappear.
I've read this has to do with refreshing the original page, and that some options are to use cookies (PHP or jQuery). I also saw something about localStorage. I'm not using HTML5, if that matters.
What is the best way to go about this? If I use PHP, can I embed the script inside of jQuery? Is there a more elegant way of going about this?
Thanks
Edit: included code
AJAX
$.ajax ({
type: 'post',
url: '/classes/resources/addResource.php',
data: {
recName: $('#recName').val(),
recURL: $('#recURL').val()
},
success: function(data) {
if (data === 'added') {
var h3ID = $('#categoryList option:selected').val();
var title = $('#recName').val();
var url = $('#recURL').val();
var newListItem = '<li>' + title + '</li>';
$('ul#' + h3ID).append(newListItem);
$('span#resError').fadeIn(400).text('Resource added successfully. Add another?');
}
else
$('span#resError').fadeIn(400).text('There was an error adding this resource');
}
});
HTML
<h3 class="category" id="1">
Category 1
</h3>
<ul id="1">
<li>List item 1</li>
<li>List item 2</li>
<li>List item 3</li>
<li>List item 4</li>
</ul>
When I add List item 5 and so on, I want those new list items to remain upon refreshing the page.
If you build your page dynamically using ajax / javascript, you need to store the current state so that you can build it exactly the same way when the page is reloaded.
There are several ways to do that like sessions (server-side) or cookies / localStorage if the information you need to build the page is stored on the client side.
If you are not using html5 but you are using ajax to build the page, using sessions and doing everything server-side is probably the easiest option. Just store the current / updated state of the page every time an ajax request is made.
An additional consideration is how long the information has to persist. If you use the standard php session mechanism, your session / state will be lost pretty fast (about an hour without reloading / refreshing I think with the default settings).

jquery and php update using modal dialog

I'm trying to display html form with specific information based on the article id using modal window but I'm struggling to pass value/id to my custom php function.
So, here's the html/php part
while(...) :
Edit
endwhile;
Now js,
$("a.button").on("click", function() {
$("#modal").reveal();
return false;
});
html and php function
<div id="modal">
<?php echo showForm($needThisIDbasedOnClick); ?>
</div>
Hope all this makes sense for you, I'm struggling of getting the certain id and passing to php function
I tried removing the return false; and following the href attribute <a href="?id=17"> ... and than getting the value using $_GET['id'] showForm($_GET['id']) but this solution just don't work the way I wanted, it reloads the page ...
The page with you PHP code is execute on the server side. PHP is interpreted, and then the content is sent to your browser. After receiving the data, your JS code is executed on the client side (thanks to you JS machine of your browser).
If you want to show information without reloading anything, you have 2 solutions:
Embedding all information in the page during the PHP processing and keep it hidden, showing the good one with JS code depend on the link clicked. (Bad solution)
Use an AJAX request with the ID in parameter that will call to a new short PHP script returning the information of the specified row.
If I resume the process could be:
1) The first request is on your main script main.php
2) The page display all your item (embedding only the ID) and contain the information container (which is hidden and empty)
example
<!-- Your list of link with article ID -->
<div>
<a class="articleLink" id="123" href="#">View</a>
<a class="articleLink" id="124" href="#">View</a>
<a class="articleLink" id="125" href="#">View</a>
</div>
<!-- The bloc that will contain info and will be shown in a popup later (hidden) -->
<div id="divInfo">
<input type="text" name="name" value=""/>
<input type="text" name="description" value=""/>
</div>
<script>
$(document).ready(function() {
// add listener to all link
$(".articleLink").click(function(mouseEvent) {
// Retrieve the article ID
var myId = $(this).attr('id');
// Ajax call with the id of the link
// onSuccess, fill the divInfo and show it
$.ajax('getInfo.php', {
dataType: 'json',
type: 'POST',
data: {
articleId: myId
},
success: function(data, status, xhrObj) {
// The data is received, update the info container
$("#divInfo input[name=name]").val(data.name);
$("#divInfo input[name=description]").val(data.desc);
// Show the div in a popup
//...
}
});
return false;
});
});
</script>
3) You are clicking on one link, it will run an ajax call to a second PHP script : getInfo.php, giving the specified ID
4) The script retrieve data in your Database and finally return the information (in JSON for example)
Assuming your PHP getInfo.php will return JSON like
{"name":"an article name","description":"the article description"}
Note: you can produce easily JSON in PHP from array with the function
json_encode()
5) the method onSuccess of your Ajax call is called when the data is received, and you can use the data to fill your form (which is unique and already present in the page - and is hidden).
good luck

php javascript partial page reload

I have a page set-up, with several divs.
For now all we need is
<div id="main">...</div> & <div id="sidebar">...</div>
Each div has code such as:
<?php include("page.php") ?>
The main div does all the work, and includes a JavaScript function. E.g. at the moment the user can click a button to remember an item displayed in a table.
Am I able to only reload the sidebar instead of the whole page when the user calls this function?
I am posting the function here, and all I need now is to be able to refresh the sidepanel and its included php files if that is possible? I assume something along the lines of this could do the job? or am I wrong? load("#sidebar")
function saveToFavorites(code)
{
$.ajax({
async:false,
type: "POST",
url: 'formPostsUser.php?reqtype=addToFavorite',
data:'coursecode='+ code,
success: function(data)
{
$('.result').html(data);
if(data != "")
{
alert(data);
load("#sidebar")
}
}
});
}
Kind regards
Alex
Happy about any and every reply and hint ;)
First thing
<div="sidebar">..</div>
The above markup is wrong HTML. You should give the sidebar as the value of your properties such as id or class
<div id="sidebar">..</div>
Loading the Sidebar content
You can use jQuery ajax to load content of this div using jQuery load method like this
$(function(){
$("#sidebar").load("yourPHPPageToReturnSideBarContent.php");
});
Assuming yourPHPPageToReturnSideBarContent.php is the PHP page which renders the HTML Markkup for the sidebar. Note that this will load the content on the document ready event.
Loading the side bar content on an event
If you want to load it on a purticular event like a button click you can do it like this
$(function(){
$(document).on("click","yourButtonId",function(){
$("#sidebar").load("yourPHPPageToReturnSideBarContent.php");
});
});
The above script will load the side bar content on a button click. The button's id is e "yourButtonId" in this example.
Note that i used jQuery on here to bind the function because it will take care of current element and future element in case if you want to load the markup which contains the button dynamically.

Show tab without refreshing whole page

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.

load part of page with ajax and jquery but show GET variable in url

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.

Categories