getJSON not really calling php page - php

Anyone can give me a hint why this getJSON not really calling the PHP page. I am trying to understand some existing code which uses getJSON.
"getfolders.php" page would write a message to a log first as the first step.
My javascript is as below:
$.getJSON('api/getfolders.php', {});
//window.location="api/getfolders.php";
If I use getJSON, it is only working sort of first time entering this javascript, if I click CTRL+F5, it doesn't trigger the "getfolders.php" multiple times.
However if comment out getJSON and use window.location instead, every CTRL+F5 will trigger the "getfolders.php" for every time.
Is it some behavior in ajax causing this issue?
Thanks

GET requests are cached by the browser.
To check it change a request for following
'api/getfolders.php' + Date.now()
but it is not good way.
Look at discussion of this subject
Perhaps you should use $ .post () function.

Related

Checking for variable changes based on time intervals (PHP+HTML)

so i am just wondering if i could do this:
a basic site, nothing on it but 4 different photos that have different priority levels which indicate the time-span of actually displaying an image ( lvl1=1min, lvl2=3min, lvl3=10min...and so on)
How could i do this with html and php.. I am not sure if answer is really basic but i cant seem to get my head around it.
Is html code running parallel to php code or does an infinite while loop in php stop the whole html code in process?
I was thinking of creating an infinite while loop in but i am worried it would eventualy somehow crash the site?
I guess i dont understand how html code runs..
Thanks to anyone who helps.
As OP mentioned. You have to use JavaScript. PHP is for server side processing, once it send final output to web browser you have to request again from the web server. You can use AJAX for your purpose easily.
In the context of the WWW (and simplifying a bit):
PHP runs
The output of PHP is received by the web server
The web server sends the output to the browser
The browser displays the page
You can look at the current time and use that to decide what <img> tag to send to the browser.
You can't change an image already displaying in the browser using server side code. The code has already run. New code won't run unless the browser makes a new HTTP request.
If you want to change the image displaying in the browser you need client side code. For all practical purposes that means JavaScript.
You can use the timer functions to run code after time has passed and DOM to change the HTML elements already on the page.
Understanding bits from your question this may be what you need
PHP code will send all four images to the browser in img tags. You can add priotity to one of the attributes to each tag for example <img data-priority="1" src...
When this code is fully loaded in browser you start a timer in javascript which then looks at current time and subtract it from time it loaded and get number of miliseconds. You can get all this using date and time functions in javascript.
At each time difference you check if that requires an image to be hidden or shown and then show or hide it. Look at jquery show and hide functions for html nodes.
If you want to show one image just hide them all in beginning and then show the one you want according to whatever rule you have either server side or client side. PHP will help you narrow down rules on server and send only required data to client however you can use simple html to hide and show if its not critical and you will avoid making multiple calls to server for images.
If this is the scenario you are looking for then try it in jsfiddle or code with sample images. You can get 4 sample images from http://lorempixel.com/ and use javascript to play with it.
For you purpose you can use AJAX which is update your code on given time interval.
If you want to set something on a timer, you can use JavaScript's setTimeout or setInterval methods:
setTimeout ( expression, timeout );
setInterval ( expression, interval );
Here expression is a normal function and timeout and interval are integers in milliseconds. setTimeout runs the timer once and runs the expression once whereas setInterval will run the expression every time the interval passes.
So in your case it would work something like this:
setInterval(function() {
//call $.ajax here
}, 5000); //5 seconds
As far as the Ajax goes, see jQuery's ajax() method. If you run an interval, there is nothing stopping you from calling the same ajax() from other places in your code.
Anymore expression need please let me know.

Reloading a div without calling an external page

I have a dashboard set up to automatically refresh the page and data. I want to move away from that and only refresh divs. The only issue is that the data i am calling is from the back end and not a separate file. I have the following code:
<script> var auto_refresh =setInterval(function({$('#test_refresh').fadeOut('slow').fadeIn("slow");}, 10000);
Year to Date Sales<span id="test_refresh"> $<?=$this->YTDsales?></span>
Obviously the data is not refreshing, it's more or less of an effect. Is there a way to just refresh single php data? Thanks for any help.
I wouldn't recomend to do a refresh via javascript, I would rather go through a simpler and cleaner solution try to do a polling or use websockets and update/get the information almost in real time.
look at this answer and you may get a clearer concept of what I'm suggesting and this is a practical example of a long polling in php
I hope that helps,
cheers!

Including a php file dynamically with javascript and jquery

I want to include a php file later(dynamically), rather than at the top. All this file does is get some contents from server and stores it in a javascript variable that i later use in jquery.
basically when you click the link, i get the info, then display it to save resources because there are many links that may not be used.
Should i just do $("body").load("phpfile.php"); ? this should work but I am trying to find a more proper way because it has nothing to do with html tag like body.
You are approaching the problem in an odd way. Not to say it wouldn't work, just that you would have a hard time getting to. I would recommend using jquery's ajax with json.
Something like:
<div id="output"></div>
$(function ()
{
//$.json('urltoRequestfrom?variable1=value1');
$.post('/echo/json/',
{json: '{"name":"test"}'},
function (data)
{
$('#output').html(data["name"]); //first json object
},'json');
});
Why would you want to do this?
jQuery does indeed have a load function where you can fetch a page fragment, which is an ajax call, just use AJAX to fetch the data dynamically whenever you want, javascript can be configured to handle the data fetched however and whenever you want.
Also include a better description of your objective, as what you have described is very unclear.
Thanks and good luck,
h
Always expect the worst from your visitors. If it where possible to include a php file with javascript, it would be a huge risk.
PHP is a server side language, and is not present in the browser. Javascript is a clientside language, and does not know anything about the PHP, only about the outputted HTML.
Use an AJAX call instead, check this page for more info: http://api.jquery.com/jQuery.ajax/
Simply, you can't. Php is ran server-side. But you could use AJAX or Jquery AJAX

Prototype.js Interferes with Javascript

Goal: Get javascript to work concurrently with Ajax.Updater (prototype.js)
Purpose: I am creating a website that displays a list of servers, and updates server status's every 10 seconds (without refreshing the page). So I use the Ajax.Updater to call a function every 10 seconds to update every server's status.
(Please refer to my other post)
Get every UL element's ID for a specific class
Problem: It seems to be that the Ajax.Updater interferes with any javascript I am using on that same page (ex. drop down menu will not drop down anymore, fancy pop up windows wont pop up either, etc). When I comment out the Ajax.Updater script, my javascript works great without any problem. The following is my ajax.updater code:
<script type="text/javascript">
function startUpdateTimer(item) {
setInterval(function () { new Ajax.Updater(item.id, 'update.php?url=' + item.id); }, 10000);
}
$$('ul.SBUpdater').each(startUpdateTimer);
</script>
Would anyone know why this is happening or how to fix this? Also, if you would suggest doing this a different way, I am open to ideas!
Also, I would suggest using the Ajax.PeriodicalUpdater instead of Ajax.Updater. It will handle the timing for you and can be done with less code.
Sounds like you already have a Javascript library(probably jQuery) running on your site already. If that's true you should remove PrototypeJS and use whatever library that's already on there for you ajax updater. Using multiple js libraries isn't the best solution.

need a button that calls a php function

I need to have a button that calls a php function to resort displayed data from the db. How the heck do I do this? I just found out that because php is server side this sucks. So any help please?
You can't directly call a PHP function pressing a button for the reason you stated yourself. In order to execute PHP code you need to make a new request to the server. The request could be made to another script or to the same that produced your page by calling it again with some parameter to control its behavior.
Alternatively, you can call a PHP script via Javascript (AJAX) so that you can handle its output and update the page without a full reload.
The second option is neater but more complex, the first one might look less pleasing to the eye, but works regardless of the user's browser having Javascript enabled or not.
It should probably sit inside a form field, something like this:
<form action="YOUR_PHP_SCRIPT.php">
<input type="submit" />
</form>
When the submit button is pressed, the action for the form is triggered.
There may be a swathe of other things you'll need to take into consideration from this point onward, but this is a start.
Yeah because PHP is server-side, you have two options. One is to make a button that calls the PHP script and renders a completely new page. The other is to use AJAX (asynchronous javascript and XML) on the page, see jquery.com for a good way to do that, and only re-render the table that is displaying data.
This is a job for ajax, as others mentioned. If I may elaborate, if you're starting out, I HIGHLY recommend using a javascript library to make your life easier. With prototype, here's what your button might look like:
<input type="button" id="button_foo">Button</input>
Here's what your javascript might look like:
$('button_foo').observe('mousedown',function(e){
new Ajax.Request('handler.php',{
method:'post',
onSuccess:function(t){
$('table_bar').update(t.responseText);
}
});
});
This may seem a little daunting at first, but I think basic js has a pretty manageable learning curve when using a library. The above code would take whatever handler.php outputs, and replace the contents of an element with and id of "table_bar" with the returned html.
if you do decide to use prototype, the docs are really helpful and easy to understand, and there is a really excellent book by pragmatic press on the subject that'll have you understanding it very quickly.
Hope that helps!
what Yacoby said, you'll need to use AJAX to make the call to the server, or something like this: http://www.ajaxdaddy.com/demo-sorted-table.html

Categories