【问题标题】:PHP text box shows last input after submitPHP文本框显示提交后的最后输入
【发布时间】:2015-04-01 23:22:14
【问题描述】:

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

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

【问题讨论】:

  • 因为这是直接FTP编辑的形式,array('overwrite' => true) 必须像没有一样站在那里,编辑后的文件不会有任何变化。您是否有一些快速修复的解决方案?

标签: php forms input textbox


【解决方案1】:

新值仅在刷新后显示,因为您正在从文件中读取旧值,打印它,然后保存新值。颠倒顺序:

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

另请注意,使用此代码,如果其他人以某种方式在保存和读取文件之间偷偷发布另一个帖子,您将获得他们的价值。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-01-10
    • 1970-01-01
    • 2018-01-22
    • 2013-10-16
    • 2013-07-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多