[英]Making HTML Element populate with query from PHP
I want to make the html element我想制作 html 元素
<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我目前正在尝试设置 userInfo 元素
<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.您需要引用 ID
userinfo
,否则它将被用作变量而不是文字。
You should assign to innerText
rather than value
;您应该分配给
innerText
而不是value
; only user input elements have a value.只有用户输入元素才有值。
To convert PHP values to JavaScript literals, the safest way is to use json_encode()
.要将 PHP 值转换为 JavaScript 文字,最安全的方法是使用
json_encode()
。 It will add quotes and any necessary escaping.它将添加引号和任何必要的转义。
<script>document.getElementById('userInfo').innerText=<?php echo json_encode($_SESSION['globalUser']); ?>;</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.