4

我正在将以下代码用于 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. 我能否在提交第二个表单后仅显示 output_textarea 内容而不保留第一个表单数据?

PHP 小提琴

4

1 回答 1

6

您在“毕业生”元素的隐藏输入中存在逻辑错误。
这就是第 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']); }?>" />
于 2013-08-26T05:28:53.123 回答