-1

我有一个获取数据数组的 PHP foreach 循环。一个特定的数组是href。在我的 echo 声明中,我将特定的 href 附加到我的下一页,如下所示:

echo '<a href="nextpage.php?url='.stats.'">Stats</a>'

它重定向到我的下一页,我可以通过 $_GET 获取 URL。问题是我想获取附加 URL 中 # 之后的值。例如,下一页上的 URL 如下所示:

stats.php?url=basket-planet.com/ru/results/ukraine/?date=2013-03-17#game-2919

我想要做的是能够在第一页上用 javascript 或 jQuery 获取#game-2919,将其附加到 URL 并转到 stats.php 页面。这甚至可能吗?我知道我无法在 PHP 中获得 # 之后的值,因为它没有发送到服务器端。有解决方法吗?

这就是我的想法:

echo '<a href="#" onclick="stats('.$stats.');">Stats</a>';

<script type="text/javascript">
  function stats(url){
    var hash = window.location.hash.replace("#", "");
    alert (hash);
  }

但这不起作用,我没有收到任何警报,所以我什至无法尝试 AJAX 并重定向到下一页。提前致谢。

更新:这是我的整个 index.php 页面。

<?php
include_once ('simple_html_dom.php');
$html = file_get_html('http://basket-planet.com/ru/');
foreach ($html->find('div[class=games] div[class=games-1] div[class=game]') as $games){
  $stats = $games->children(5)->href;
  echo '<table?
          <tr><td>
            <a href="stats.php?url=http://www.basket-planet.com'.$stats.'">Stats</a>
          </td></tr>
        </table>';
        }
?>

我的 stats.php 页面:

<?php include_once ('simple_html_dom.php');
$url = $_GET['url'];
//$hash = $_GET['hash'];
$html = file_get_html(''.$url.'');
$stats = $html->find('div[class=fullStats]', 3);
    //$stats = $html->find('div[class='.$hash.']');
echo $stats;
?>

我想要做的是将哈希添加到传递给 stats.php 的 URL 中。没有太多代码,因为我使用的是简单的 HTML DOM 解析器。我希望能够使用来自 stats.php URL 的哈希来查看传递的 URL。希望有帮助...

4

2 回答 2

0

生成 HREF 时在 PHP 中使用 urlencode,这样当用户单击链接时,浏览器不会丢弃哈希部分:

索引.php:

<?php
include_once ('simple_html_dom.php');
$html = file_get_html('http://basket-planet.com/ru/');

echo '<table>';
foreach ($html->find('div[class=games] div[class=games-1] div[class=game]') as $games){
  $stats = $games->children(5)->href;
  echo '<tr><td>
            <a href="stats.php?url=http://www.basket-planet.com'.urlencode($stats).'">Stats</a>
          </td></tr>';
}
echo '</table>';
?>

然后在第二页上,从 url 中解析出哈希部分。

stats.php:

<?php
include_once ('simple_html_dom.php');

$url = $_GET['url'];
$parsed_url = parse_url($url);
$hash = $parsed_url['fragment'];

$html = file_get_html(''.$url.'');
//$stats = $html->find('div[class=fullStats]', 3);
$stats = $html->find('div[class='.$hash.']');
echo $stats;
?>
于 2013-03-26T21:17:19.257 回答
0

这是你要找的吗?

function stats(url)
{

  window.location.hash = url.substring(url.indexOf("#") + 1)

  document.location.href = window.location

}

如果您当前的 URL 是index.php#test并且您调用stats('test.php#index')它会将您重定向到index.php#index.

或者,如果您想将当前 URL 的哈希添加到自定义 URL:

function stats(url)
{
  document.location.href = url + window.location.hash
}

如果您当前的 URL 是index.php#test并且您调用stats('stats.php')它会将您重定向到stats.php#test.

对您的评论:

function stats(url)
{

  var parts = url.split('#')

  return parts[0] + (-1 === parts[0].indexOf('?') ? '?' : '&') + 'hash=' + parts[1] 

}

// stats.php?hash=test
alert(stats('stats.php#test'))

// stats.php?example&hash=test
alert(stats('stats.php?example#test'))
于 2013-03-26T19:56:31.927 回答