php - 回声中的 PHP $_POST 函数 x()
问题描述
有人可以告诉我我以这种方式做错了什么吗?有三个不同的单选按钮,带有选择和不同的输入。在示例 2 中选择单选按钮时,然后输入文本,提交表单 - 重新加载,正确生成输出,显示最后选择的单选按钮,但显示第一个按钮字段。附上截图以便更好地理解。
代码:
<form name="form" id="form" method="POST" action=""
enctype="multipart/form-data" >
<div class="form-row">
<div>
<div class="f hide" id="div1">
Option nr. 1
<label></label> <input type="text" style="text-transform: uppercase;" pattern="[A-Za-z]{3}" placeholder="Letters" class="form-control" name="txt_input" value="<?php echo isset($_POST['txt_input']) ? $_POST['txt_input'] : '' ?>" maxlength="3" >
<label></label> <input type="text" placeholder="Digits" pattern="\d{3}" style="text-transform: uppercase;" class="form-control" name="num_input" value="<?php echo isset($_POST['num_input']) ? $_POST['num_input'] : '' ?>" maxlength="3" >
</div>
<div class="sl box hide" id="div2">
Option nr. 2
<label></label> <input type="text" style="text-transform: uppercase;" pattern="[A-Za-z]{3}" placeholder="Letters" class="form-control" name="txt_inputsl" value="<?php echo isset($_POST['txt_inputsl']) ? $_POST['txt_inputsl'] : '' ?>" maxlength="3" >
<label></label> <input type="text" style="text-transform: uppercase;" pattern="\d{3}" placeholder="Digits" class="form-control" name="num_inputsl" value="<?php echo isset($_POST['num_inputsl']) ? $_POST['num_inputsl'] : '' ?>" maxlength="3" >
</div>
<div class="s box hide" id="div3">
Option nr. 3
<label></label> <input type="text" style="text-transform: uppercase;" placeholder="Digits" pattern="[A-Za-z0-9]+" class="form-control" name="txt_input_" value="<?php echo isset($_POST['txt_input_']) ? $_POST['txt_input_'] : '' ?>" maxlength="6" >
</div>
<br>
<label><input type="radio" name="check" onclick="show1();" value="f" <?php if($_POST['check'] == 'f') { print ' checked="checked"'; } ?> > Option nr. 1</label>
<label><input type="radio" name="check" onclick="show2();" value="sl" <?php if($_POST['check'] == 'sl') { print ' checked="checked"'; } ?> >Option nr. 2</label>
<label><input type="radio" name="check" onclick="show3();" value="s" <?php if($_POST['check'] == 's') { print ' checked="checked"'; } ?> >Option nr. 3</label>
</div>
</div>
<div class="button-row">
<input type="submit" id="submit" name="submit" class="btn btn-info" value="Generate">
</div>
</form>
<script>
function show1(){
document.getElementById('div1').style.display ='block';
document.getElementById('div2').style.display ='none';
document.getElementById('div3').style.display ='none';
}
function show2(){
document.getElementById('div1').style.display ='none';
document.getElementById('div2').style.display ='block';
document.getElementById('div3').style.display ='none';
}
function show3(){
document.getElementById('div1').style.display ='none';
document.getElementById('div2').style.display ='none';
document.getElementById('div3').style.display ='block';
}
</script>
似乎在 submint 之后 - 检查右单选按钮,但按功能 - 停留在第一个选项上。试图在回声值中使用 $_POST['show1()'] 并检查值 - 但没有奏效。显然我错过了一些东西,但搜索了类似的解决方案 - 没有找到正确的方法。感谢任何回应!
解决方案
解决了我的问题并添加了一些我用过的代码。
对于我的单选按钮,添加了 id,然后添加了一个 .js 函数,该函数检查单选按钮,如果它是真的 - 就行了,如果不是 - 做其他事情。
在这段代码中:
<label><input type="radio" name="check" onclick="show1();" value="f" <?php if($_POST['check'] == 'f') { print ' checked="checked"'; } ?> > Option nr. 1</label>
<label><input type="radio" name="check" onclick="show2();" value="sl" <?php if($_POST['check'] == 'sl') { print ' checked="checked"'; } ?> >Option nr. 2</label>
<label><input type="radio" name="check" onclick="show3();" value="s" <?php if($_POST['check'] == 's') { print ' checked="checked"'; } ?> >Option nr. 3</label>
我改为:
<label><input type="radio" id="one" name="check" onclick="show1();" value="f" <?php if($_POST['check'] == 'f') { print ' checked="checked"'; } ?> > Option nr. 1</label>
<label><input type="radio" id="two" name="check" onclick="show2();" value="sl" <?php if($_POST['check'] == 'sl') { print ' checked="checked"'; } ?> >Option nr. 2</label>
<label><input type="radio" id="three" name="check" onclick="show3();" value="s" <?php if($_POST['check'] == 's') { print ' checked="checked"'; } ?> >Option nr. 3</label>
然后通过选中的单选按钮添加新功能:
if(document.getElementById('one').checked) {
document.getElementById('div1').style.display ='block';
document.getElementById('div2').style.display ='none';
document.getElementById('div3').style.display ='none';
}else if(document.getElementById('two').checked) {
document.getElementById('div1').style.display ='none';
document.getElementById('div2').style.display ='block';
document.getElementById('div3').style.display ='none';
} else if(document.getElementById('three').checked) {
document.getElementById('div1').style.display ='none';
document.getElementById('div2').style.display ='none';
document.getElementById('div3').style.display ='block';
}
如果没有这个 js 函数,它只会检查单选按钮,但只显示第一个字段,直到您单击任何其他单选按钮。如果它是真的=某种功能,则此功能检查是检查其中的任何一个。PS<?php if($_POST['name'] == 'value') { print ' checked="checked"'; } ?>
在单选按钮参数中是必需的。
推荐阅读
- python - 为 Python 中的自定义类型添加序列化和反序列化支持
- reactjs - React Navigation 和流式
- c# - 从我的 .Net Core 类设计器中添加新类时,Visual Studio 添加了一个空文件
- c++ - 如何从类中返回自定义数组项并操作其属性?C++
- python - Python - 测试文件是否已经打开
- sql - 迄今为止,Oracle VARCHAR2 字段与 substr
- python - 获取 data.count("and") 为 2 的原因是什么?
- python - Python ThreadPoolExecutor 抑制异常
- arrays - 使用结构存储动态数据
- amazon-web-services - 参数类型如何操作字符串