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

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

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?