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 求TYPCE母转母转接头24PIN线路板图
  • ¥100 国外网络搭建,有偿交流
  • ¥15 高价求中通快递查询接口
  • ¥15 解决一个加好友限制问题 或者有好的方案
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型