使用php提交后,将表单数据保留在字段内

时间:2013-08-26 05:11:54

标签: php forms textarea

我在下面的代码中使用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 = Johnplace : UK并选择毕业状态为graduate,则会在我的output textarea

中为我提供第一个表单输出
My name is John and I am from UK, and I am Graduate

我有两个单独的提交按钮用于第二种形式,使用我在output textarea的帮助下做其他功能 如果我按下任何第二个按钮,我可以保留条目名称和放置区域,但不保留下拉选择。所以它只会在提交submit1submit2

后显示
My name is John and I am from UK, and I am

下面,

  1. 如何使用输出文本区域
  2. 保留下拉选项
  3. 我可以在第二次表单提交后只显示output_textarea内容而不保留第一个表单数据吗?
  4. PHP FIDDLE

1 个答案:

答案 0 :(得分:3)

“graduate”元素的隐藏输入中的逻辑错误 这就是你在第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']); }?>" />