如何将注意力设置在'name'变量上?以下代码无效
if(empty($_SESSION['name']))
{
<script type='text/javascript'>document.getElementById('name').focus();</script>
$_SESSION['nameErr'] = "Please enter name";
$_SESSION['result'] = false;
//form page location
header('location:stu_form.php');
exit;
}
答案 0 :(得分:0)
设置焦点是在HTML元素上完成,而不是在Php变量上完成。
如果你把名字放在一些html输入元素中,你可以这样做:
<?php
if(isset($_SESSION['name'])) {
?>
<input type="text" id="name" value="<?php echo $_SESSION['name']; ?>">
<?php
}
?>
然后从Javascript开始集中注意力:
<script type='text/javascript'>document.getElementById('name').focus();</script>
答案 1 :(得分:-1)
尝试:
if(empty($_SESSION['name']))
{
echo "<script type='text/javascript'>document.getElementById('name').focus();</script>";
$_SESSION['nameErr'] = "Please enter name";
$_SESSION['result'] = false;
//form page location
header('location:stu_form.php');
exit;
}