gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-10-31 22:55:41 26 4
gpt4 key购买 nike

我在 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 FIDDLE

最佳答案

您在“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']); }?>" />

关于php - 使用php提交后将表单数据保留在字段内,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18437048/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com