douji0588 2012-08-22 21:02
浏览 44
已采纳

提交表单时,变量会更新,但不会在textarea中续订

Excuse me for the title, it was a bit hard to explain my problem. So here it is. On http://appsolute.vacau.com/cms I have a variable in the textarea. You see the output of the variable from start. Updating the text en submitting the form IS updating the variable. Thus far everything is right, right? On submitting the form, the page is reloaded, but all the content is from the previous content of the variable. But when checking, the variable WAS updated. Even if you refresh the page. So why don't I get to see the actual value of the variable after submitting the form? Hope it makes sense...

<html>
    <head>
        <title>Milan CMS</title><?php
$host     = "x";
$dbname   = "x";
$username = "x";
$password = "x";
$dbh      = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
$sth      = $dbh->prepare("SELECT * FROM milan   ");
$sth->execute();
$result   = $sth->fetchAll();
?>
    </head>

    <body>
        <?php
        var_dump($result);
        $myFile   = "trainingen.json";
        $fh       = fopen($myFile, 'w') or die("can't open file");
        fwrite($fh, json_encode($result));
        fclose($fh);
        ?>
        <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="trainingen">
            <h3>Trainingen:</h3><p><textarea name="nametrainingen" rows="10" cols="60"><?php echo $result[0]['value']; ?></textarea></p>
            <input name="submit" type="submit" value="Verzenden"><input type="reset" value="Veld leeg maken">
        </form>
        <?php
        $invoer   = $_POST['nametrainingen'];
        if (isset($_POST['submit'])) { //test if submit button is clicked
            $sql = "DELETE FROM milan WHERE tag = 'trainingen'";
            $sth = $dbh->prepare($sql);
            $sth->execute();
            $sql = "INSERT INTO milan (tag, value) VALUES ('trainingen', '$invoer')";
            $sth = $dbh->prepare($sql);
            $sth->execute();
        }
        ?>
    </body>
</html>
  • 写回答

3条回答 默认 最新

  • drduh44480 2012-08-22 21:10
    关注

    re-arrange the logic like so:

    <?php
            $invoer   = $_POST['nametrainingen'];
            if (isset($_POST['submit'])) { //test if submit button is clicked
                $sql = "DELETE FROM milan WHERE tag = 'trainingen'";
                $sth = $dbh->prepare($sql);
                $sth->execute();
                $sql = "INSERT INTO milan (tag, value) VALUES ('trainingen', '$invoer')";
                $sth = $dbh->prepare($sql);
                $sth->execute();
            }
    
            $sth      = $dbh->prepare("SELECT * FROM milan   ");
            $sth->execute();
            $result   = $sth->fetchAll();
        ?>
           <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="trainingen">
                <h3>Trainingen:</h3><p><textarea name="nametrainingen" rows="10" cols="60"><?php echo $result[0]['value']; ?></textarea></p>
                <input name="submit" type="submit" value="Verzenden"><input type="reset" value="Veld leeg maken">
            </form>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 C# datagridview 栏位进度
  • ¥15 vue3页面el-table页面数据过多
  • ¥100 vue3中融入gRPC-web
  • ¥15 kali环境运行volatility分析android内存文件,缺profile
  • ¥15 写uniapp时遇到的问题
  • ¥15 vs 2008 安装遇到问题
  • ¥15 matlab有限元法求解梁带有若干弹簧质量系统的固有频率
  • ¥15 找一个网络防御专家,外包的
  • ¥100 能不能让两张不同的图片md5值一样,(有尝)
  • ¥15 informer代码训练自己的数据集,改参数怎么改