使用php提交后将表单数据保留在字段内
Posted
技术标签:
【中文标题】使用php提交后将表单数据保留在字段内【英文标题】:Keep form data inside the field after submission using php 【发布时间】:2013-08-28 12:35:53 【问题描述】:我正在为一个 html 表单使用下面的代码。(它有两个表单)我能够在第一个和第二个表单提交后保留 textarea 字段。但我在这里面临的问题是下拉菜单选择。
代码:
<html>
<body>
<div class="emcsaninfo-symcli-main">
<form id="form1" name="form1" action=" " method="post" >
<div class="input">Your Name</div>
<div class="response"><span><input class="textbox" id="myname" name="myname" type="text" value="<?php if(isset($_POST['myname'])) echo htmlentities ($_POST['myname']); ?>" /></span> </div>
<div class="input">Your Place</div>
<div class="response"><span><input class="textbox" id="myplace" name="myplace" type="text" value="<?php if(isset($_POST['myplace'])) echo htmlentities ($_POST['myplace']); ?>" /></span> </div>
<div class="input-quest">Graduation Status</div>
<div class="input-resp"><select id="graduation" name="graduation" OnChange="CMT();"><option class="dropdown-options">Graduate</option><option class="dropdown-options">Non Graduate</option></select></div>
<div class="submit">
<input id="first_submit" type="submit" name="first_submit" value="first_submit" />
</div>
</form>
<?php
if(!empty($_POST['myname']) && !empty($_POST['myplace']) || !empty($_POST['output_textarea'] ) )
$myname = $_POST['myname'];
$myplace = $_POST['myplace'];
$graduation = $_POST['graduation'];
?>
<form id="form2" name="form2" action=" " method="post" >
<textarea onclick="this.select()" name="output_textarea" id="output_textarea" cols="100" rows="25" readonly value="<?php if(isset($_POST['output_textarea'])) echo htmlentities ($_POST['output_textarea']); ?>">
<?php
echo "My name is $myname and I am from $myplace, and I am $graduation";
?>
</textarea>
<input id="submit1" type="submit" name="name_field" value="submit1" />
<input id="submit2" type="submit" name="place_field" value="submit2" />
<input id="myname_hidden" name="myname" type="hidden" value="<?php if(isset($_POST['myname'])) echo htmlentities ($_POST['myname']); ?>"/>
<input id="myplace_hidden" name="myplace" type="hidden" value="<?php if(isset($_POST['myplace'])) echo htmlentities ($_POST['myplace']); ?>" />
<input id="graduation_hidden" name="graduation" type="hidden" value="<?php if(isset($_POST['graduation'])) $graduation = $_POST['graduation']; ?>" />
</form>
<?php
function name()
echo $_POST["output_textarea"];
if(isset($_POST['name_field']))
name();
function place()
echo $_POST["output_textarea"];
if(isset($_POST['place_field']))
place();
?>
</div>
</html>
</body>
例如,如果我输入name = John
、place : UK
并选择毕业状态为graduate
,它将给我在output textarea
中的第一个表单输出
My name is John and I am from UK, and I am Graduate
我有第二个表单的两个单独的提交按钮,我在output textarea
的帮助下使用它做一些其他功能
如果我按下第二个按钮中的任何一个,我可以保留我的姓名和地点区域的条目,但它不会保留下拉选择。所以它只会在提交submit1
或submit2
后显示
My name is John and I am from UK, and I am
这里,
-
如何让下拉选择也与输出文本区域保持一致
我能否在提交第二个表单后只显示 output_textarea 内容而不保留第一个表单数据?
PHP FIDDLE
【问题讨论】:
【参考方案1】:“毕业生”元素的隐藏输入中存在逻辑错误。
这就是第 53-55 行的内容。第 55 行没有回显,而是有一个 $graduation = $_POST['graduation'];
,这对您没有帮助:
<input id="myname_hidden" name="myname" type="hidden" value="<?php if(isset($_POST['myname'])) echo htmlentities ($_POST['myname']); ?>"/>
<input id="myplace_hidden" name="myplace" type="hidden" value="<?php if(isset($_POST['myplace'])) echo htmlentities ($_POST['myplace']); ?>" />
<input id="graduation_hidden" name="graduation" type="hidden" value="<?php if(isset($_POST['graduation'])) $graduation = $_POST['graduation']; ?>" />
除此之外,这段代码应该可以工作:
<input id="myname_hidden" name="myname" type="hidden" value="<?php if(isset($_POST['myname'])) echo htmlentities ($_POST['myname']); ?>"/>
<input id="myplace_hidden" name="myplace" type="hidden" value="<?php if(isset($_POST['myplace'])) echo htmlentities ($_POST['myplace']); ?>" />
<input id="graduation_hidden" name="graduation" type="hidden" value="<?php if(isset($_POST['graduation'])) echo htmlentities($_POST['graduation']); ?>" />
【讨论】:
以上是关于使用php提交后将表单数据保留在字段内的主要内容,如果未能解决你的问题,请参考以下文章