I've been learning Ajax and now I'm wondering how it allows a string from Javascript to be passed to php.
It was said before that the problem with passing Javascript to PHP is that the PHP code gets run first, and then the Javascript gets run. So when Javascript generates a string it's already too late.
Does this mean that Ajax allows PHP code to be run after Javascript?
I think this is what they're getting at:
Before Ajax -- specifically, before XMLHttpRequest came along -- a single web page was served as a single page load. If it was a PHP-generated page incorporating Javascript, the browser would request the page, PHP would generate the page (including Javascript code, includes, fragments of script on the page, etc.), would send the page to the browser, and the browser would display it. So, the PHP happened up-front. Until the next page load -- when the entire page was refreshed from scratch -- PHP wasn't involved again.
After the advent of XMLHttpRequest, which helped put the "X" in "AJAX", as it was back then, you had another option. Once the page was loaded, your Javascript could make requests "behind the scenes" of the page, to request more information from the server, without reloading the page. In effect, the loaded page could cause more PHP to be run on the server, and display the results.
So, if you're considering a single page load from a PHP-based website, that is (sort of) what Ajax means; without it, you get a single PHP page-build that's delivered and then your Javascript has to run on that result alone. With Ajax, you can make further requests to your server and throw the results out onto the existing page without a full page load.
The php interpreter sitting on the server is basically interprets whatever php script into (usually) HTML page.
That's why you can never "pass" javascript variable into php as to the interpreter, your javascript is just yet another string, without any special meaning. Your javascript is run by your browser and doesn't even aware that it was being produced by PHP.
I believe that's what it means by "too late".
You should know, that Javascript is always (except Node.js) Client-Side.
PHP is a Server-Side language.
You can't pass Javascript variables to PHP - at least not at the Pageload.
What you can do is doing a AJAX-Request after the Page is laoded to send something to PHP.
With the Response of that call you can replace some other things on the current requested Page.
No it does not mean that "Ajax allows PHP code to be run after Javascript". AJAX is a new request to the server and it allows you to manipulate with the server's response. You can observe this by coping the url to which AJAX request is being made and pasting it into the browser.
So basically when you open a web site with the browser, you send a request and any AJAX call is also a request, but done in a background, so you cannot see it directly. You can use for example firebug or other developer's tool to see what happens behind the scenes. It has nothing to do with the scripts executions orders.
Related
I'm confused about this question. Actually I'm trying to get a JavaScript value to PHP variable using Ajax, but I'm getting error. I think that PHP is parssing first before I could put the value to it via JavaScript
PHP is serverside means it runs on the server, when you get your page php already run through is finished and made his output and then JS starts to work on your computer (clientside). ;)
The PHP is interpreted first on the server and the result is rendered to HTML and JavaScript as appropriate. When the client browser receives this result it will run any embedded scripts, including JavaScript. You also mentioned AJAX, which is a specific usage of JavaScript that makes a connection back to the server from the browser. An AJAX call is sometimes used to bring the value of a server side (possibly PHP) variable into JavaScript after the initial page contents have been sent to the browser.
PHP is parsed on server side, JS is parsed in browser. So, when client asks for a page, at first server runs PHP script to the end, generates HTML and puts it into browser, where JS starts runing.
I have a table with a button which onClick calls an external javascript file, which calculates some values and returns them to the table. I need to then write that value back to a file using PHP without leaving the current page. Is there any way to do this? I thought about trying onChange, but I wasn't sure how ot make that work with php since it's usually a javascript call.
PHP is executed on your server
the javascript file, even if external, is run on the client machine.
You can't use PHP on the client machine! You must use javascript to call a php script, stored in your server, that do what you need to.
You're on the good track, you need to use AJAX calls in your javascript.
don't use onclick, search for unobtrusive javascript;
you'd be using jQuery, so you can use its ajax handler;
find out a good reading about the whole topic, before going heads down on writing code.
This sounds like something you could solve by using AJAX. Basically you would communicate with your server in the background, send the data to it so it can process it. You send a separate request in the background which happens without reloading the page.
Please be aware of the distinction between the client (where the JavaScript is run) and the server (where PHP generated the page run by the client).
If you want to use jQuery, that library has some decent functions that makes it easier to implement cross-browser AJAX requests. However I would suggest first researching the topic.
So, I'm a bit confused here.
I'm slowly progressing into ajax or HTML5 SSE, because I have list that, in the long run, I want to update without the user having to refresh. But, baby steps here, I'm starting with a simple function that should tell me when the file the list is reading from here, has changed, from another computer plugging in an entry.
CODE:
<script type='text/javascript'>
function CheckForChange(){
alert("<?echo (count($listArray)) . ' and ' . count(file($filename_noformat))?>");
}
setInterval("CheckForChange()", 7000);
</script>
listArray is the PHP variable that keeps the list seen on the page when the page refreshes, read line by line from a text file (I'm working on moving to a database, later)
Since PHP variables can only execute when the page is loaded, the PHP would only reflect what was in the text file when the PHP executed.
But with Javascript, and the setInterval function, shouldn't it be able to execute the PHP to check what's in the text file at the time that function is executed (every 7 seconds)? Because it's not, and I don't understand why not.
I try plugging in a 4th post to the list on my phone, and the phone's alert changes to 4 and 4, but the computer still says 3 and 3. I want it to say 3 and 4.
Thanks
setInterval() executes local javascript in your page, not server-based PHP. If you want it to send something to your server without reloading the page, you would have to use an ajax call to actually send something to the server which could cause some PHP to execute.
Hint - look at View/Source in your browser and see what is actually in your page. There's no PHP in your page. The PHP runs on the server once when the page is created.
No.
The PHP runs on the server and outputs some text.
The server sends that text to the client (the browser).
The browser then interprets that text as HTML and JavaScript.
The PHP runs only once. The JavaScript function it generates then is run repeatedly, but from JavaScript's perspective, that function has a string literal in it, not executable server side code.
If you want to run PHP then you have to issue a new HTTP request to the server. This requires either reloading the page or using Ajax.
I have been working on parsing some of the data from the wow armory and have come into a bit of a snag. When it comes to the site serving up the achievements that players have received, it uses javascript to intemperate a string such as #73:1283 to display the requested information. (I made this number up but the data for the requests are formated like this).
Is it possible to pull data from a page that requires javascript to display its data with php?
How do you parse data from a site that has been loaded after the dom is ready or complete using php?
By using Firebug, I was able to look at the HTTP headers to see what AJAX calls were being made to generate the content on these pages: http://us.battle.net/wow/en/character/black-dragonflight/glitchshot/achievement#96:14861 and http://us.battle.net/wow/en/character/black-dragonflight/glitchshot/achievement#96
It looks the page is making an asynchronous call to load this page: http://us.battle.net/wow/en/character/black-dragonflight/glitchshot/achievement/14861 when the part after the hash is 96:14861, and a call to http://us.battle.net/wow/en/character/black-dragonflight/glitchshot/achievement/96 when the part after the hash is just 96. Both of those pages return XML that can be parsed to render HTML.
So generally speaking, if there's just one number after the hash, just put http://.../achievement/<number here> as the URL. If there are two numbers, put the second number at the end of the URL instead.
What you'll need to do, rather than pulling the Javascript and interpreting it, is make HTTP requests to those URLs by yourself in PHP (using cURL, for example) and parse the data on your own.
I would really recommend learning JavaScript and jQuery, since it will be very hard for you to really build a good site that pulls information from the WoW Armory without understanding all the AJAX loads that are going on in the background.
I would recommend seeing if you can replicate the query sent by JavaScript in PHP. While I don't believe there is a way to process JavaScript in PHP, there definitely isn't a simple or scalable way.
I would attempt to scan the first page's source that you downloaded with PHP for strings of that format you mention. Then if the JS on their site is querying something like http://www.wow.com/armory.php?id=#72:1284 you can just download the source of that next. You can find out how the JS is querying the server with something like FireBug or the Inspector in Chrome or Safari.
So in summary:
Check to find the JS URL format and if you can replicate it.
Create PHP to get main page and extract all strings.
Create PHP to loop through these strings and get these pages (with URL that JS requests).
Do whatever you wanted to with that information.
You can try jquery's $(document).onready function which helps
to run java script code when the web page loads up.
ex
<div id="wowoData">#4325325</div>
<script>
$(document).ready(
function(){
$("#wowoData").css("border","1px solid red");
}
)
</script>
I am trying to found out how to see if a php file has changed and then show a div with saying Page changed in JQUERY
You'd better do that in PHP using filemtime, no need for JQuery here.
You only need jQuery for this task if you're trying to detect the page change without waiting for the user to request a new page. If not, do as the other responder suggests and use PHP.
But if you need to do it without a page reload, use one of the $.ajax() methods in jQuery in combination with a JavaScript timer. You'll have to poll the server periodically (thus the timer) to ask if the page has been altered.
You would also need to set up something on the server that can tell your page about changes. Perhaps a very simple service that provides the timestamp of the last edit in JSON format. Use $.ajax() to poll for the timestamp, then compare it with the last edit the page knows about. If the timestamp from JSON is more recent, display your div.
Javascript cannot access the server, you will have to use some sort of server side technology. Like PHP that was suggested by Pekka.
In short, javascript is client side, which means it interacts with the user on their side, while php is server side, meaning it interacts with the server. Checking the file modified date is a server side issue, your client isn't serving the pages (unless you're on freenet)
Or you could output a <meta> tag for when the page was updated with PHP or whatever framework or language you are using. Then create a cookie with your JS and compare the cookie with the meta tags content.
Ugly solution but it would work. I wouldn't want to resort that this however.