如何使用$_POST
方法制作“逐步”屏幕,内置可选级别?这是我的代码:
<?php
$next = @$_POST['next'];
$prev = @$_POST['prev'];
if( !isset($next) || isset($prev) ) {
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="checkbox" name="optional-checkbox" id="oc" />
<label for="oc">If you'll check me, you'll see an optional level</label>
<button name="prev" type="submit">Previous</button>
<button name="next" type="submit">Next</button>
</form>
<?php
}
else {
// if isset next
$oc = @$_POST['oc'];
if( isset($oc) ) {
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
You've checked the optional checkbox. now you can continue, or - go back.
<button name="prev" type="submit">Previous</button>
<button name="next" type="submit">Next</button>
</form>
<?php
}
elseif {
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<?php
// check if we came from the "oc" level, if true - show special message, else - just continue
if( $oc == true ) {
echo 'You\'ve checked the optional checkbox. now you\'re on the 3rd level.';
}
else {
echo 'now you\'re on the 3rd level.';
}
?>
<button name="prev" type="submit">Previous</button>
<button name="next" type="submit">Next</button>
</form>
}
}
当我测试它时,它没有正确显示水平,我无法从可选级别继续到下一级别。我确实做错了什么,但我不知道...还有,我不能回去 - 如果我要取消$_POST['next']
,它会把我送到第一级,从每个级别。有什么建议/想法吗?
谢谢大家。
答案 0 :(得分:1)
首先,您的代码存在一些语法错误。
错误1和重要错误
<input type="checkbox" name="optional-checkbox" id="oc" />
您的复选框的名称必须为oc
而不是&#39;可选 - 复选框&#39;因为此处$oc = @$_POST['oc'];
,您正在寻找名为oc
的输入。
错误2
<?php
}
elseif {
您的elseif
没有条件。它应该只是else
而你在?>
之后错过了elseif{
。
错误3
</form>
}
}
您错过了<?php
和</form>
}
标记
工作代码
<?php
$next = @$_POST['next'];
$prev = @$_POST['prev'];
if( !isset($next) || isset($prev) ) {
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="checkbox" name="oc" id="oc" />
<label for="oc">If you'll check me, you'll see an optional level</label>
<button name="prev" type="submit">Previous</button>
<button name="next" type="submit">Next</button>
</form>
<?php
}
else {
// if isset next
$oc = @$_POST['oc'];
if( isset($oc) ) {
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
You've checked the optional checkbox. now you can continue, or - go back.
<button name="prev" type="submit">Previous</button>
<button name="next" type="submit">Next</button>
</form>
<?php
}
else{
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<?php
// check if we came from the "oc" level, if true - show special message, else - just continue
if( $oc == true ) {
echo 'You\'ve checked the optional checkbox. now you\'re on the 3rd level.';
}
else {
echo 'now you\'re on the 3rd level.';
}
?>
<button name="prev" type="submit">Previous</button>
<button name="next" type="submit">Next</button>
</form>
<?php
}
}
?>