I'm writing Analytical software that uses javascript but mainly PHP to log things
The javascript posts some result to $_GET variable like this
document.location.href = "?screenwidth=" + screen.width + "&screenheight=" + screen.height; but this requires the page to reload for PHP to process it.
However the page CANNOT reload every time someone visits, because it disrupts the user experience too much.
Is there a way, in javascript or PHP, of opening dynamically the PHP file, not in an iframe, cos that messes up the formatting, so PHP can operate on it and write it to a MYSQL database.
I don't mind if this requires some complicated code - I'm hapy to give it a bash.
You could send all this data via an asynchronous post request using javascript (AJAX). I'd recommend using one the frameworks such as jquery, they make this very simple.
so, ajax is powerfull, but easy to hack, so i do not recommand ajax for such things.
What an absolutely nonsense statement. AJAX is just a technique used to send a request to the server, just the same as if you typed the url and hit enter yourself. Your solution of opening another window with a form which the user then has to submit anyway is far, far less graceful than sending the request automatically, and precisely no more secure.
You could send all this data via an asynchronous post request using javascript (AJAX). I'd recommend using one the frameworks such as jquery, they make this very simple.
How would I do this? Is it possible without reloading the page?
$.post(
"stats.php", // script which saves stats
{
// the data to send
foo: "someValue",
bar: 123,
baz: "some other value" // etc
},
function(response) {
// callback function from your php script
alert(response.message);
},
"json" // response format
);
stats.php:
PHP Code:
<?php
if (!isset($_POST)) die();
// save the stats or whatever here, they are all in $_POST, eg $_POST['foo'] == "someValue"
// send a response if you want to echo json_encode(array('message' => 'Hooray! You sent: ' . print_r($_POST, true)));
All well and good until you want to use something other than a transitional doctype, not to mention the annoying click every time the iframe refreshes. Plus this will resend the stats every 5 mins, surely you want stats once per request? There is really no need to be using iframes in this day and age!
What an absolutely nonsense statement. AJAX is just a technique used to send a request to the server, just the same as if you typed the url and hit enter yourself. Your solution of opening another window with a form which the user then has to submit anyway is far, far less graceful than sending the request automatically, and precisely no more secure.
hi there =)
first, i want to scream out, that my daughter was born on 2009-06-02 23:15:00 and we named her 'michelle' =) and yes, i'm proud bout this little girl XD
so to this quote you should read something more on some official sites =)
Saying AJAX is responsible for security holes is utter rubbish. If the code requested has security holes, it makes no difference whether AJAX is involved or not. AJAX is just a technique, not a technology in itself. But you carry on using JS popups like it's 1998, it's no skin off my nose!
You are really not going about this the right way. There is a technique designed for doing just what you are trying to do already. Please don't be put off by some of the rubbish posted in this thread.
If you wrote your html and css properly you wouldn't need to worry about the client's screen dimensions. What are you going to do for people with javascript disabled.
Bookmarks