我需要一些帮助,我制作了此代码,当有人填写表单时,他们填写的名称必须写入表格。
因此,如果有人在名称字段中键入:" Jay Adamsen" 。并选中" Bar" ,然后选择" 10:00 - 11:00" ,然后表单会将其写入右侧地点。这非常有效:)
但是当另一个人输入:&#34; Jack Carlson&#34; 在名称字段中并选中&#34; Bar&#34; 然后选择< strong>&#34; 11:00-12:00&#34; 然后代码将删除&#34; Jay Adamsen&#34; 并将新名称写入右侧框。
这是我的代码:
<?php
$navn = $_REQUEST['navn'];
$bar = $_POST['barorindgang'];
$tidspunkt = $_POST['klokken'];
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<style type="text/css">
body {
width: 950px;
margin-left: auto;
margin-right: auto;
}
#Banner {
width: 950px;
height: 200px;
margin-left: auto;
margin-right: auto;
margin-top: 5px;
margin-bottom: 10px;
}
.tekst {
text-align: center;
}
.link1 {
height: 50px;
width: 237px;
float: left;
}
.link2 {
height: 50px;
width: 237px;
float: left;
}
.link3 {
height: 50px;
width: 237px;
float: left;
}
.link4 {
height: 50px;
width: 237px;
float: left;
}
#vagt {
float: left;
margin-top: 10px;
background-color: #C3C;
width: 950px;
}
.text {
text-align: center;
font-size: 38px;
}
table,th,td {
margin-left: auto;
margin-right: auto;
border: 1px solid black;
background-color: #FF6;
margin-bottom: 10px;
}
.form {
margin-left: 35px;
width: 100%;
}
</style>
<title>Ghetto Bash</title>
</head>
<body>
<div id="Banner">
<a href="index.html"><img src="pics/ghettobashbanner.jpg"></a>
</div>
<div id="bar">
<div class="link1"><a href="index.html"><img src="pics/forside.jpg"></a> </div>
<div class="link2"><a href="vagtplan.html"><img src="pics/vagtplan.jpg"></a> </div>
<div class="link3"><a href="billetter.html"><img src="pics/biletter.jpg"></a></div>
<div class="link4"><a href="billeder.html"><img src="pics/billeder.jpg"></a></div>
</div>
<div id="vagt">
<p class="text">Vagtplan</p>
<table width="93%" height="354" cellpadding="1">
<tr>
<th width="25%" scope="col">TIDER</th>
<th width="38%" scope="col">BAR</th>
<th width="37%" scope="col">INDGANG</th>
</tr>
<tr>
<th scope="row">10:00 - 11:00</th>
<td>
<center>
<?php
if ($bar == "bar" && $tidspunkt == "10:00") {
print $navn;
}
?>
</center>
</td>
<td>
<center>
<?php
if ($bar == "indgang" && $tidspunkt == "10:00") {
print $navn;
}
?></center>
</td>
</tr>
<tr>
<th scope="row">11:00 - 12:00</th>
<td> <center><?php
if ($bar == "bar" && $tidspunkt == "11:00") {
echo $navn;
}
?></center></td>
<td> <center><?php
if ($bar == "indgang" && $tidspunkt == "11:00") {
echo $navn;
}
?></center></td>
</tr>
<tr>
<th scope="row">12:00 - 13:00</th>
<td> <center><?php
if ($bar == "bar" && $tidspunkt == "12:00") {
echo $navn;
}
?></center></td>
<td> <center><?php
if ($bar == "indgang" && $tidspunkt == "12:00") {
echo $navn;
}
?></center></td>
</tr>
<tr>
<th scope="row">13:00 - 14:00</th>
<td> <center><?php
if ($bar == "bar" && $tidspunkt == "13:00") {
echo $navn;
}
?></center></td>
<td> <center><?php
if ($bar == "indgang" && $tidspunkt == "13:00") {
echo $navn;
}
?></center></td>
</tr>
<tr>
<th scope="row">14:00 - 15:00</th>
<td> <center><?php
if ($bar == "bar" && $tidspunkt == "14:00") {
echo $navn;
}
?></center></td>
<td> <center><?php
if ($bar == "indgang" && $tidspunkt == "14:00") {
echo $navn;
}
?></center></td>
</tr>
</table>
<center>
<form method="post" action="vagtplan.html">
Navn: <input type="text" name="navn" id="navn" required="required"/><br />
Bar: <input type="radio" name="barorindgang" value="bar" id="bar" required="required" /> Indgang: <input type="radio" name="barorindgang" value="indgang" id="indgang" required="required" /><br />
Klokken: <select name="klokken">
<option value="10:00">10:00 - 11:00</option>
<option value="11:00">11:00 - 12:00</option>
<option value="12:00">12:00 - 13:00</option>
<option value="13:00">13:00 - 14:00</option>
<option value="14:00">14:00 - 15:00</option>
</select>
<br />
<input type="submit" value="Send" />
</form>
</center>
</div>
</body>
</html>
答案 0 :(得分:1)
这是标准功能。如果您希望保存以前的条目,则必须将它们存储在服务器上的数据库或文本文件中。
答案 1 :(得分:1)
当您重新输入名称时,它会刷新您在该页面上丢失之前值的页面。您可以使用数据库来存储值或创建xml或json文件,也可以在会话中存储值。
另一个选择是使用AJAX,然后您可以提交而不会丢失以前的值。
答案 2 :(得分:0)
保留一个临时变量来打印表格中的值,因为它每次都在变化。
**Here is my Code:**
<?php
$navn = $_REQUEST['navn'];
$bar = $_POST['barorindgang'];
$tidspunkt = $_POST['klokken'];
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<style type="text/css">
body {
width: 950px;
margin-left: auto;
margin-right: auto;
}
#Banner {
width: 950px;
height: 200px;
margin-left: auto;
margin-right: auto;
margin-top: 5px;
margin-bottom: 10px;
}
.tekst {
text-align: center;
}
.link1 {
height: 50px;
width: 237px;
float: left;
}
.link2 {
height: 50px;
width: 237px;
float: left;
}
.link3 {
height: 50px;
width: 237px;
float: left;
}
.link4 {
height: 50px;
width: 237px;
float: left;
}
#vagt {
float: left;
margin-top: 10px;
background-color: #C3C;
width: 950px;
}
.text {
text-align: center;
font-size: 38px;
}
table,th,td {
margin-left: auto;
margin-right: auto;
border: 1px solid black;
background-color: #FF6;
margin-bottom: 10px;
}
.form {
margin-left: 35px;
width: 100%;
}
</style>
<title>Ghetto Bash</title>
</head>
<body>
<div id="Banner">
<a href="index.html"><img src="pics/ghettobashbanner.jpg"></a>
</div>
<div id="bar">
<div class="link1"><a href="index.html"><img src="pics/forside.jpg"></a> </div>
<div class="link2"><a href="vagtplan.html"><img src="pics/vagtplan.jpg"></a> </div>
<div class="link3"><a href="billetter.html"><img src="pics/biletter.jpg"></a></div>
<div class="link4"><a href="billeder.html"><img src="pics/billeder.jpg"></a></div>
</div>
<div id="vagt">
<p class="text">Vagtplan</p>
<table width="93%" height="354" cellpadding="1">
<tr>
<th width="25%" scope="col">TIDER</th>
<th width="38%" scope="col">BAR</th>
<th width="37%" scope="col">INDGANG</th>
</tr>
<tr>
<th scope="row">10:00 - 11:00</th>
<td>
<center>
<?php
if ($bar == "bar" && $tidspunkt == "10:00") {
$navn_bar_10 = $navn;
print $navn_bar_10;
}
?>
</center>
</td>
<td>
<center>
<?php
if ($bar == "indgang" && $tidspunkt == "10:00") {
$navn_indgang_10 = $navn;
print $navn_indgang_10;
}
?></center>
</td>
</tr>
<tr>
<th scope="row">11:00 - 12:00</th>
<td> <center><?php
if ($bar == "bar" && $tidspunkt == "11:00") {
$navn_bar_11 = $navn;
echo $navn_bar_11;
}
?></center></td>
<td> <center><?php
if ($bar == "indgang" && $tidspunkt == "11:00") {
$navn_indgang_11 = $navn;
echo $navn_indgang_11;
}
?></center></td>
</tr>
<tr>
<th scope="row">12:00 - 13:00</th>
<td> <center><?php
if ($bar == "bar" && $tidspunkt == "12:00") {
$navn_bar_12 = $navn;
echo $navn_bar_12;
}
?></center></td>
<td> <center><?php
if ($bar == "indgang" && $tidspunkt == "12:00") {
$navn_indgang_12 = $navn;
echo $navn_indgang_12;
}
?></center></td>
</tr>
<tr>
<th scope="row">13:00 - 14:00</th>
<td> <center><?php
if ($bar == "bar" && $tidspunkt == "13:00") {
$navn_bar_13 = $navn;
echo $navn_bar_13;
}
?></center></td>
<td> <center><?php
if ($bar == "indgang" && $tidspunkt == "13:00") {
$navn_indgang_13 = $navn;
echo $navn_indgang_13;
}
?></center></td>
</tr>
<tr>
<th scope="row">14:00 - 15:00</th>
<td> <center><?php
if ($bar == "bar" && $tidspunkt == "14:00") {
$navn_bar_14 = $navn;
echo $navn_bar_14;
}
?></center></td>
<td> <center><?php
if ($bar == "indgang" && $tidspunkt == "14:00") {
$navn_indgang_14 = $navn;
echo $navn_indgang_14;
}
?></center></td>
</tr>
</table>
<center>
<form method="post" action="vagtplan.html">
Navn: <input type="text" name="navn" id="navn" required="required"/><br />
Bar: <input type="radio" name="barorindgang" value="bar" id="bar" required="required" /> Indgang: <input type="radio" name="barorindgang" value="indgang" id="indgang" required="required" /><br />
Klokken: <select name="klokken">
<option value="10:00">10:00 - 11:00</option>
<option value="11:00">11:00 - 12:00</option>
<option value="12:00">12:00 - 13:00</option>
<option value="13:00">13:00 - 14:00</option>
<option value="14:00">14:00 - 15:00</option>
</select>
<br />
<input type="submit" value="Send" />
</form>
</center>
</div>
</body>
</html>
另外,为了更好的方法,您可以维护db并从db中检索值。
答案 3 :(得分:0)
HTTP是无状态的。这意味着,当用户提交表单时,表单提交不知道之前可能发生的任何表单提交 - 除非您采取措施保存该数据。在您的示例中,您的表单仅显示用户刚刚提交的数据;它不会将数据存储在任何地方,也不会检索其他人提交的数据并预先填充表格。作为该服务器的管理员,您将不知道人们提交了什么,因为它没有被存储在任何地方。
至少,您的表单处理程序应该保存提交的数据(可能是保存到数据库中,但如果您真的需要,可以使用平面文件来处理解决方案)。然后您的表单显示逻辑将需要检索已保存的数据并显示它。
另外,我强烈建议您考虑使用foreach
循环播放时间段,而不是重复复制代码。它会使代码更小,更易于维护。