0

I have this working script yet when I change it to retrieve(supposedly) the value inside $_SESSION["username"], it doesn't retrieve anything. The whole page is saved as .php as I am using some codes that uses PHP.

Code:

echo "<script type=text/javascript>";
echo "var hidden = false;";

echo "function actiondb1() {";
echo "if(!hidden) {";
echo "document.getElementById(\'clickdb1\').style.visibility = \'hidden\';";
echo "document.getElementById(\'db1\').style.visibility = \'visible\';"; 
echo "document.getElementById(\'db1\').disabled = false;";
echo "document.getElementById(\'db1\').value =".$_SESSION["username"];.";";

echo "}";
echo "}";
echo "</script>";

How can I make the script to properly retrieve the data inside $_SESSION["username"];?

3 Answers 3

2

Observe that, for instance, if the value of $_SESSION["username"] is John, your echo will be generating this result:

document.getElementById('db1').value = John;

But John is supposed to be a string and should be wrapped in quotation marks, otherwise JavaScript will understand it as a variable name (which value will be probably undefined).

Sign up to request clarification or add additional context in comments.

Comments

0

As Havenard mentioned, this line is missing Javascript single quotes to properly denote a string variable:

echo "document.getElementById(\'db1\').value ='".$_SESSION["username"];."';";

However, you really shouldn't print JS out with PHP if you can help it. Though iatboy's answer answer won't ultimately fix your bug, it is a much cleaner way of doing things.

?>
<script type=text/javascript>;
var hidden = false;
function actiondb1() {
  if(!hidden) {
      document.getElementById('clickdb1').style.visibility = 'hidden';
      document.getElementById('db1').style.visibility = 'visible'; 
      document.getElementById('db1').disabled = false;
      document.getElementById('db1').value ='<?php echo $_SESSION["username"];?>';
    }
}
</script>;
<?php

2 Comments

The idea of closing the php tags before this sequence and opening it again after the code block works, Thanks!
Welcome! You could also look into the heredoc PHP syntax.
0

Did you start session in this page?If you didn't,use the follow code to start session.

    session_start();

Then change your code to

echo "<script type=text/javascript>";
echo "var hidden = false;\n";
echo "function actiondb1() {\n";
echo "alert('".$_SESSION['username']."')\n";  //test code
echo "if(!hidden) {\n";
echo "document.getElementById('clickdb1').style.visibility = 'hidden';\n";
echo "document.getElementById('db1').style.visibility = 'visible';\n"; 
echo "document.getElementById('db1').disabled = false;\n";
echo "document.getElementById('db1').value ='".$_SESSION["username"]."';\n";
echo "}\n";
echo "}\n";
echo "</script>";

it will be ok.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.