当我在文本框中创建一些输入(例如:test123)并提交页面时,它只显示空白输入。在重新加载(页面刷新)后,文本"test123"显示在表单中。当我尝试编辑以前输入的文本时也会发生这种情况,例如:将"test123"输入编辑到"test12345"中,然后单击提交,它将显示"test123),在页面刷新后显示"test1234"。
echo '<form action="" method="post">';
$content = file_get_contents($file);
echo '<textarea style="width: 99.3%; height: 700px; margin-left:-1px" name="cfgtekst">'.htmlspecialchars($content).'</textarea>';
echo '<center><input type="submit" class="btn btn-primary confirm_t btn btn-sucess" value="'.$usavechange.'" />';
echo '<a href="serverdetalji.php?sid='.$serverid.'" class="btn btn-primary confirm_t btn btn-warning">'.$uotkazi.'</a></center>';
echo '</form>';
if(isset($_POST))
{
$cfgtekst = $_POST['cfgtekst'];
$stream_options = array('ftp' => array('overwrite' => true));
$stream_context = stream_context_create($stream_options);
if ($fh = fopen($file, 'w', 0, $stream_context))
{
fputs($fh, $cfgtekst);
fclose($fh);
}
}
新值只有在刷新后才会显示,因为您要从文件中读取旧值,打印它,然后保存新值。颠倒顺序:
if(isset($_POST))
{
$cfgtekst = $_POST['cfgtekst'];
$stream_options = array('ftp' => array('overwrite' => true));
$stream_context = stream_context_create($stream_options);
if ($fh = fopen($file, 'w', 0, $stream_context))
{
fputs($fh, $cfgtekst);
fclose($fh);
}
}
echo '<form action="" method="post">';
$content = file_get_contents($file);
echo '<textarea style="width: 99.3%; height: 700px; margin-left:-1px" name="cfgtekst">'.htmlspecialchars($content).'</textarea>';
echo '<center><input type="submit" class="btn btn-primary confirm_t btn btn-sucess" value="'.$usavechange.'" />';
echo '<a href="serverdetalji.php?sid='.$serverid.'" class="btn btn-primary confirm_t btn btn-warning">'.$uotkazi.'</a></center>';
echo '</form>';
顺便说一句,我建议你节省一些时间,使用数据库;在web应用程序中使用这样的文件非常容易出错。
还要注意,使用此代码,如果其他人在保存和读取文件的时间之间偷偷地发布了另一篇帖子,你就会得到它们的价值。