Posting screen width using jQuery
Using this开发者_如何学编程 code
var sw = window.screen.width;
$.post("http://www.example.com/track.php", {result: sw
}, "html");
and $_SERVER['result']; in the server I'm trying to get the sreen width but it does not work. It is something wrong with the "result". I'm new in Javascript and jQuery...
http://api.jquery.com/jQuery.post/
$_SERVER
contains server variables, that is, things like the operating system, the referrer URL, paths to various folders on the server.
What you're looking for instead is either the $_POST
array, the $_GET
array, or the $_REQUEST
array. I might be stating the obvious here, but here's what they contain:
$_POST
contains a list of all variables POSTed to the script.$_GET
contains a list of all variables in the query string (eg:someScript.php?x=1&y=2
)$_REQUEST
contains a merge of$_POST
,$_GET
and$_COOKIE
(usually in that order). I don't recommend using this: you should know the methods you're using to get variables into your script and use that array specifically.
In your case, you need to take a look at the $_POST
array. It's always handy to run this once:
print_r($_POST);
This will show you everything posted to that page.
The jQuery $.post function sends a post request to the server, which means to access the value of "result", you need to get it from the $_POST superglobal.
Try $_POST['result'] instead of $_SERVER['result'].
These descriptions may help (source: http://www.nusphere.com/php/php_superglobals.htm):
- $_POST- The $_POST Superglobal represents data sent to the PHP script via HTTP POST. This is normally a form with a method of POST.
- $_SERVER- The $_SERVER Superglobal represents data available to a PHP script from the Web server itself (not what you're looking for)
- $_REQUEST- The $_REQUEST Superglobal is a combination of $_GET, $_POST, and $_COOKIE (would work, but why search GET and COOKIE when you know the value is in POST?)
Try doing:
echo $_POST['result'];
Use $_REQUEST['result']
$_SERVER
is an array containing information such as headers, paths, and script locations. The entries in this array are created by the web server. There is no guarantee that every web server will provide any of these; servers may omit some, or provide others not listed here.
$_REQUEST
is an associative array that by default contains the contents of$_GET
,$_POST
and$_COOKIE
.
The correct way is to use $_POST['result']
, as suggested by others here.
精彩评论