2015-04-01 77 views
-1

当我在文本框中创建了一些输入(例如:test123)并且我提交页面时,它只显示空白输入。重新加载(页面刷新)文本“test123”在窗体中显示后。当我尝试编辑以前输入的文本时,也会发生这种情况,例如:将“test123”输入编辑到“test12345”,点击提交它将显示“test123”,页面刷新后显示“test12345”。PHP文本框显示提交后的最后一个输入

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); 
    } 
} 
+0

由于这是一个直接的FTP编辑表单,因此数组('overwrite'=> true)必须站在那里,编辑文件中根本没有任何更改。 你可能有一些快速解决方案吗? – Tuna 2015-04-01 23:32:10

回答

0

新值仅在刷新后显示,因为您正在从文件读取旧值,然后保存新值。反向该订单:

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应用程序中使用这样的文件非常容易出错。

另请注意,通过此代码,如果其他人以某种方式在其保存时间和读取文件之间潜入另一篇文章,您将获得它们的价值。

+0

完成&它的工作! :)谢谢你,欠你一杯啤酒 – Tuna 2015-04-01 23:34:54

相关问题