我在下面的代码中有一个错误,其中说UNTERMINATED STRING LITERAL请帮助我,并告诉我PHP代码是在正确的位置,因为它是在脚本标记之前,所以如何可以设置为真?
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
</head>
<body>
<?php
if(isset($_REQUEST['page']))
{
$page=$_REQUEST['page'];
}
?>
<form action="yea.php" method="post">
<select name="page" onchange="hitme('<?php echo $page;?>');">
<option value="home">home</option>
<option value="about">about</option>
<option value="menu">menu</option>
<option value="download">download</option>
</select>
</form>
<script>
function hitme(page)
{
switch(page)
{
case "home":
document.write("you selected home");
break;
case "menu":
document.write("you selected menu");
break;
case "about":
document.write("you selected about");
break;
default:
document.write("invalid selection!");
}
}
document.write("<br>it will print no matter what!");
</script>
</body>
</html>
答案 0 :(得分:1)
我有问题,你在评论中提到为什么你需要php来做到这一点
<form action="yea.php" method="post">
<select name="page" onchange="hitme(this);">
<option value="home">home</option>
<option value="about">about</option>
<option value="menu">menu</option>
<option value="download">download</option>
</select>
<span id="pagename"></span>
</form>
<script>
function hitme(page)
{
switch(page.value)
{
case "home":
document.getElementById("pagename").innerHTML = "you selected home";
break;
case "menu":
document.getElementById("pagename").innerHTML = "you selected menu";
break;
case "about":
document.getElementById("pagename").innerHTML = "you selected about";
break;
default:
document.getElementById("pagename").innerHTML = "invalid selection!";
}
}
alert("<br>it will print no matter what!");
</script>
</body>
答案 1 :(得分:0)
将您的PHP代码更改为
<?php
if(isset($_REQUEST['page']))
{
$page=$_REQUEST['page'];
}
else
$page="";
?>
答案 2 :(得分:0)
在PHP中,如果阻止你使用logal $ page,你需要使用全局。因此,您必须将$ page变量声明为if block。