简体   繁体   中英

How to concatenate a variable in a document.location.href path?

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM