Php和javascript无法重新加载页面并跳转到位置

时间:2017-03-01 15:56:40

标签: javascript

我一直在努力处理页面重新加载

我有一个提交数据的表单,无论更新的表上的行如何。

当表单重新加载时,我可以让页面重新加载整个页面或跳转到锚点,但不是两者都可以。

第一部分是
形成
隐藏价值(有锚)这个工作

按钮点击通话功能

函数在php中:

// this code will show editWonLost.php#123456 but does not reload
echo "<script type='text/javascript'>location.hash='$NeedToUpdate'; document.location = 'editWonLost.php'+location.hash</script>"; 

// this code will show editWonLost.php#$NeedToUpdate
echo '<script type="text/javascript">location.hash="$NeedToUpdate"; document.location = "editWonLost.php"+location.hash</script>'; 

// this code will refresh the page but not got to anchor location
echo '<script type="text/javascript">document.location = "editWonLost.php"</script>';

目标是尝试让页面重新加载然后跳转到位置

2 个答案:

答案 0 :(得分:0)

设置哈希值后调用window.location.reload()应该有效。那么你的代码就是:

echo "<script type='text/javascript'>location.hash='$NeedToUpdate'; location.reload();</script>";

更新

如果您的目标是在提交表单后将用户重定向到其他页面,请尝试以下内容(未经测试!):

header('Location: editWonLost.php' . $NeedToUpdate);

显然,您必须检查表单是否已提交,并且仅在用户重定向时才重定向。

答案 1 :(得分:0)

查看location.reload()

的javascript文档

https://www.w3schools.com/jsref/met_loc_reload.asp

所以,你的PHP看起来像:

echo "<script type='text/javascript'>location.hash='$NeedToUpdate'; document.location = 'editWonLost.php'+location.hash;  window.location.reload();</script>"; // this code will show editWonLost.php#123456

你也可以看到这个相关的问题:

How to reload a page using JavaScript?

相关问题