gpt4 book ai didi

javascript - 多表单场景下如何保存之前输入的内容?

转载 作者:行者123 更新时间:2023-11-28 01:07:30 25 4
gpt4 key购买 nike

这就是我想要做的:我想保存所有表单的输入,而不仅仅是一个表单,因为下面的代码将保存第二个表单的输入,但是当我转到第一个表单时,选择从值中获取一些内容并点击提交按钮,它现在会记住第一个表单输入并忘记第二个表单输入。我想要一个解决方案,它将帮助我保留其他表单的先前值,直到我决定用另一个选定的值更改它的那一刻。你们中的一些人会说只需将它们放入一种表单并使用一个提交按钮,问题就会得到解决,但这不是我试图找到的解决方案,因为我正在一个项目中工作,我需要一种解决方案而不将它们合并在一起。

<?php
if (($_SERVER['REQUEST_METHOD'] == 'POST') && (!empty($_POST['action']))):

if (isset($_POST['semester1'])) { $semester1 = $_POST['semester1']; }
if (isset($_POST['semester2'])) { $semester2 = $_POST['semester2']; }
if (isset($_POST['semester3'])) { $semester3 = $_POST['semester3']; }
if (isset($_POST['semester4'])) { $semester4 = $_POST['semester4']; }
if (isset($_POST['semester5'])) { $semester5 = $_POST['semester5']; }
if (isset($_POST['semester6'])) { $semester6 = $_POST['semester6']; }
?>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form class="form-horizontal" role="form" action='' method='POST' name="theform"><!--==========Start of the Form========-->

Semester: <select class="form-control" name="semester1">
<option><?php if (isset($semester1)) { echo $semester1; } ?></option>
<option>Fall</option>
<option>Spring</option>
<option>Summer</option>
</select>
<button type="submit" value="Submit" name="action">Submit The Data</button>
</form>

<form class="form-horizontal" role="form" action='' method='POST' name="theform"><!--==========Start of the Form========-->

Semester: <select class="form-control" name="semester2">
<option><?php if (isset($semester2)) { echo $semester2; } ?></option>
<option>Fall</option>
<option>Spring</option>
<option>Summer</option>
</select>
<button type="submit" value="Submit" name="action">Submit The Data</button>
</form>


<form class="form-horizontal" role="form" action='' method='POST' name="theform"><!--==========Start of the Form========-->

Semester: <select class="form-control" name="semester3">
<option><?php if (isset($semester3)) { echo $semester3; } ?></option>
<option>Fall</option>
<option>Spring</option>
<option>Summer</option>
</select>
<button type="submit" value="Submit" name="action">Submit The Data</button>
</form>

</body>
</html>

最佳答案

看来我找到了一个解决方案,而且似乎有效,我需要感谢#DarkBee 的 session 想法。

<?php
session_start();
if (($_SERVER['REQUEST_METHOD'] == 'POST') && (!empty($_POST['action']))):

if (isset($_POST['semester1'])) { $_SESSION['1'] = $_POST['semester1']; }else{$_SESSION['1'];}
if (isset($_POST['semester2'])) { $_SESSION['2'] = $_POST['semester2']; }else{$_SESSION['2'];}
if (isset($_POST['semester3'])) { $_SESSION['3'] = $_POST['semester3']; }else{$_SESSION['3'];}
if (isset($_POST['semester4'])) { $semester4 = $_POST['semester4']; }
if (isset($_POST['semester5'])) { $semester5 = $_POST['semester5']; }
if (isset($_POST['semester6'])) { $semester6 = $_POST['semester6']; }
?>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form class="form-horizontal" role="form" action='' method='POST' name="theform"><!--==========Start of the Form========-->

Semester: <select class="form-control" name="semester1">
<option><?php if (isset($_SESSION['1'])) { echo $_SESSION['1']; } ?></option>
<option>Fall</option>
<option>Spring</option>
<option>Summer</option>
</select>
<button type="submit" value="Submit" name="action">Submit The Data</button>
</form>

<form class="form-horizontal" role="form" action='' method='POST' name="theform"><!--==========Start of the Form========-->

Semester: <select class="form-control" name="semester2">
<option><?php if (isset($_SESSION['2'])) { echo $_SESSION['2']; } ?></option>
<option>Fall</option>
<option>Spring</option>
<option>Summer</option>
</select>
<button type="submit" value="Submit" name="action">Submit The Data</button>
</form>


<form class="form-horizontal" role="form" action='' method='POST' name="theform"><!--==========Start of the Form========-->

Semester: <select class="form-control" name="semester3">
<option><?php if (isset($_SESSION['3'])) { echo $_SESSION['3']; } ?></option>
<option>Fall</option>
<option>Spring</option>
<option>Summer</option>
</select>
<button type="submit" value="Submit" name="action">Submit The Data</button>
</form>

</body>
</html>

关于javascript - 多表单场景下如何保存之前输入的内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24912986/

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