我正在尝试一切我能想到的将PHP变量传递给Javascript并返回PHP,因为代码有望显示。脚本的输出始终是	'我无法确定原因。
<script>
function changeContent3(func){
<?php if (isset($_POST['userid'])) {
$userid=$_POST['userid'];
} else {
$userid=9;
} ?>
if (func=='12') {message = "<?php SignUpForm($userid); ?>";}
document.getElementById('content2').innerHTML = message;
}
</script>
<link rel="stylesheet" href="include/layout.css" type="text/css" media="all" />
<div id="floater"></div>
<div id="centeredinv">
<div id="centered1">
<div id="content2">
<div class='FMblock'>
<div class="FMcells">
<CENTER><form method="post"><input type='hidden' name='userid' value='1'><a class="udlinks" onclick="changeContent3('12');"><i>one</i></CENTER></a></form>
</div>
<div class="FMcells">
<CENTER><form method="post"><input type='hidden' name='userid' value='2'><a class="udlinks" onclick="changeContent3('12');"><i>two</i></CENTER></a></form>
</div>
<div class="FMcells">
<CENTER><form method="post"><input type='hidden' name='userid' value='3'><a class="udlinks" onclick="changeContent3('12');"><i>three</i></CENTER></a></form>
</div>
<div class="FMcells">
<CENTER><form method="post"><input type='hidden' name='userid' value='4'><a class="udlinks" onclick="changeContent3('12');"><i>four</i></CENTER></a></form>
</div>
<div class="FMcells">
<CENTER><form method="post"><input type='hidden' name='userid' value='5'><a class="udlinks" onclick="changeContent3('12');"><i>five</i></CENTER></a></form>
</div>
</div>
</div>
</div>
</div>
<?php
function SignUpForm($userid) {
echo"YAY ".$userid;
}
任何帮助将不胜感激
答案 0 :(得分:0)
不是很优雅,但你可以试试下面的东西。 在我的示例中,“one”没有用户ID,因此需要9个。其他人都有用户ID。
编辑:我之前的代码中有一个错误
<script>
function changeContent3(func,func2){
userid = func2;
if(!userid) {
userid = 9;
}
if (func=='12') {message = "YAY " + userid;}
document.getElementById('content2').innerHTML = message;
}
</script>
<link rel="stylesheet" href="include/layout.css" type="text/css" media="all" />
<div id="floater"></div>
<div id="centeredinv">
<div id="centered1">
<div id="content2">
<div class='FMblock'>
<div class="FMcells">
<CENTER><form method="post"><input type='hidden' name='userid' value='' id="a"><a class="udlinks" onclick="changeContent3('12', + document.getElementById('a').value);"><i>one</i></CENTER></a></form>
</div>
<div class="FMcells">
<CENTER><form method="post"><input type='hidden' name='userid' value='2' id="b"><a class="udlinks" onclick="changeContent3('12', + document.getElementById('b').value);"><i>two</i></CENTER></a></form>
</div>
<div class="FMcells">
<CENTER><form method="post"><input type='hidden' name='userid' value='3' id="c"><a class="udlinks" onclick="changeContent3('12', + document.getElementById('c').value);"><i>three</i></CENTER></a></form>
</div>
<div class="FMcells">
<CENTER><form method="post"><input type='hidden' name='userid' value='4' id="d"><a class="udlinks" onclick="changeContent3('12' , + document.getElementById('d').value);"><i>four</i></CENTER></a></form>
</div>
<div class="FMcells">
<CENTER><form method="post"><input type='hidden' name='userid' value='5' id="e"><a class="udlinks" onclick="changeContent3('12' , + document.getElementById('e').value);"><i>five</i></CENTER></a></form>
</div>
</div>
</div>
</div>
</div>
答案 1 :(得分:0)
这是一个指导您的示例代码。 三个没有用户ID,其他有。 所有这些代码都位于名为test.php的单个页面中。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>stack</title>
</head>
<?php
if (isset($_POST['userid']) && $_POST['userid'] != "") {
$userid=$_POST['userid'];
} else {
$userid = 9;
}
?>
<body>
<form id="form1" name="form1" method="post" action="test.php"><input type='hidden' name='userid' value='1'><a href="#" onclick="document.forms['form1'].submit();">one</a></form>
<form id="form2" name="form2" method="post" action="test.php"><input type='hidden' name='userid' value='2'><a href="#" onclick="document.forms['form2'].submit();">two</a></form>
<form id="form3" name="form3" method="post" action="test.php"><input type='hidden' name='userid' value=''><a href="#" onclick="document.forms['form3'].submit();">three</a></form>
<div id="content2"><?php echo $userid; ?></div>
</body>
</html>