I want to make the html element
<p id="userInfo"></p>
have the value of the username that was user to login in the session. I have the following variable set to contain the variable that holds the username and works this works for logging in.
$_SESSION['globalUser'] = $_POST['resultUser'];
I currently am attempting to set the userInfo element with
<script>document.getElementById(userInfo).value='<?php echo $_SESSION['globalUser']?>'</script>
This script is causing the error "Uncaught SyntaxError: Invalid or unexpected token"
When I open the developer tools and click the error it shows
<script>document.getElementById(userInfo).value='<br />
I also using the following statement to transfer the session across pages
<?php $session_value=(isset($_SESSION['id']))?$_SESSION['id']:''; ?>
What am I doing wrong here?
EDIT:
This is currently my query
$sqlUser = "SELECT `teacher_username` FROM `teacher` WHERE `teacher_username`='$user'";
$sqlPass = "SELECT `password` FROM `teacher` WHERE `password`='$pass'";
$resultUser = mysqli_query($connection, $sqlUser);
$resultPass = mysqli_query($connection, $sqlPass);
$textUser = $resultUser->fetch_assoc();
$textPass = $resultPass->fetch_assoc();
$_SESSION['globalUser'] = $_POST[$textUser['teacher_username']];
You need to quote the ID userinfo
, otherwise it will be used as a variable rather than a literal.
You should assign to innerText
rather than value
; only user input elements have a value.
To convert PHP values to JavaScript literals, the safest way is to use json_encode()
. It will add quotes and any necessary escaping.
<script>document.getElementById('userInfo').innerText=<?php echo json_encode($_SESSION['globalUser']); ?>;</script>
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.