0

由于我想确定链接中是否有 #,并且您只能在 Javascript 中执行此操作,因此我想将 JS 变量传输到 PHP。

所以如果我有:

if(location.hash){
var hash = location.hash; 

hash var 需要变成 php $hash var..

如果不可能的话,我也尝试过,在帖子中发送变量,

$.post('photo.php?mode=grab', { hash: hash }, function(result) { 
// ..but then i got stuck, how should i transfer to php var from here?
4

2 回答 2

3
$hashVar = $_POST['hash'];

这就是你所追求的?

$("#trigger").click(function(){
var hash = location.hash; 

 $.ajax
  ({
  type: "POST",
  url: "file.php",
  data: hash,
  //cache: false,
  success: function(html)
   {
    alert(html);
   }
  });

 return false;
});
于 2011-01-17T13:04:00.317 回答
0

$_POST发布的字符串将在变量中可供 PHP 使用。

由于您使用 JQuery 发布 Javascript 对象,因此 PHP 应该将其作为 JSON 字符串接收。

您可以使用 PHP 函数将 JSON 字符串转换为 PHP 数组json_decode()

同样,如果您需要将数组从 PHP 发送回 Javascript,请json_encode()在​​ PHP 中使用来反转该过程并创建一个 JSON 对象。

于 2011-01-17T13:09:01.120 回答