dsf323233323332 2019-04-23 18:33
浏览 231
已采纳

如何连接document.location.href路径中的变量?

I have a php $_SESSION passing the name of a target directory, that is different from situation to situation. I have a javascript function to execute a file with the same name but in several different directories, depending on the string passed with $_SESSION. My code is:

<?PHP
$where = $_SESSION["where"];
?>

<script>
var where = "<?php echo $where;?>";
function goToThere() {
    document.location.href = where + "/file_to_execute.php";
}
</script>

<body>
<button class="buttongreen" onclick="goToThere()">proceed</button>
</body>

Say the content of $where is "dir_a". Then clicking on buttongreen might launch function goToThere, thus going to page "dir_a/file_to_execute.php". The problem is that the goToThere function simply does not do anything. I've tried different sequences to concatenate the variable and the string, with various combinations of quotation marks, without success. What am I doing wrong?

  • 写回答

1条回答 默认 最新

  • dongluoheng3324 2019-04-23 18:47
    关注

    As stated, your code is applicable to what you are trying to do. The issue lies in the "$_SESSION['where']"

    Either...

    (1) You have a forward slash at the end of $_SESSION['where'] and you are adding another forward slash when concatenating.

    (2) You are not doing "session_start();"

    (3) The script code is not being incorporated into the body nor header (I'm not quite sure about this, but as I see it, the script code really is in no man's land so maybe???)

    (4) The $_SESSION['where'] is simply not being saved

    (5) The $_SESSION['where'] is simply empty

    As it stands though, your code is valid as a proof-of-concept for what you are aiming to do

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 android报错 brut.common.BrutException: could not exec (exit code = 1)
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥15 复杂网络,变滞后传递熵,FDA
  • ¥20 csv格式数据集预处理及模型选择
  • ¥15 部分网页页面无法显示!