这里有几个问题...
首先 - 您正在加载页面并尝试回显一个不存在的会话。这会给你带来错误。
要解决此问题,请将$_SESSION['start'] = ''; 放在if (isset($_POST['start'])) 上方并将$_SESSION['response'] = ''; 放在if (isset($_POST['response'])) 上方,这定义了您的会话,但值为空。
第二 - 将每个表格行放在自己的表格和表格中。
<form name="form1" method="post" action="">
<table>
<tr>
<th>
<p>Vendor was contacted at:</p>
<button name="start" >Start</button><br>
<?php
if (!isset($_SESSION)) {
session_start ();
}
/* these five lines ask if there is an existing session & if there is an existing session... is it empty
if there is an existing session & it's not empty... we use it
otherwise... we create an empty session to prevent errors
this also maintains the session data when the other button is clicked like you asked in your comments */
if(isset($_SESSION['start']) && $_SESSION['start'] != '') {
$_SESSION['start'] = $_SESSION['start'];
} else {
$_SESSION['start'] = '';
}
if (isset($_POST['start']))
{
$date_start = date('m-d-Y H:i:s');
$_SESSION['start'] = $date_start;
}
?>
<br>
<input name="start" type="text" class="textfield" value="<?php echo $_SESSION['start']; ?>">
<br>
</th>
</tr>
</table>
</form>
<form name="form2" method="post" action="">
<table>
<tr>
<th>
<p>Vendor responded at:</p>
<button name="response" >Response</button><br>
<?php
if(isset($_SESSION['response']) && $_SESSION['response'] != '') {
$_SESSION['response'] = $_SESSION['response'];
} else {
$_SESSION['response'] = '';
}
if (isset($_POST['response']))
{
$date_response = date('m-d-Y H:i:s');
$_SESSION['response'] = $date_response;
}
?>
<br>
<input name="response" type="text" class="textfield" value="<?php echo $_SESSION['response']; ?>">
<br>
</th>
</tr>
</table>
</form>
更新:
如果你想保留数据(如果你想让你的数据持久化)......改变......
<?php
session_start ();
$_SESSION['start'] = '';
if (isset($_POST['start'])) {
$date_start = date('m-d-Y H:i:s');
$_SESSION['start'] = $date_start;
}
?>
到...
<?php
if (!isset($_SESSION)) {
session_start ();
}
if(isset($_SESSION['start']) && $_SESSION['start'] != '') {
$_SESSION['start'] = $_SESSION['start'];
} else {
$_SESSION['start'] = '';
}
if (isset($_POST['start']))
{
$date_start = date('m-d-Y H:i:s');
$_SESSION['start'] = $date_start;
}
?>
然后改变...
<?php
$_SESSION['response'] = '';
if (isset($_POST['response'])) {
$date_response = date('m-d-Y H:i:s');
$_SESSION['response'] = $date_response;
}
?>
到...
<?php
if(isset($_SESSION['response']) && $_SESSION['response'] != '') {
$_SESSION['response'] = $_SESSION['response'];
} else {
$_SESSION['response'] = '';
}
if (isset($_POST['response']))
{
$date_response = date('m-d-Y H:i:s');
$_SESSION['response'] = $date_response;
}
?>