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

提交表单时,变量会更新,但不会在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>

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>

Simple logic. First make changes to DB

DELETE FROM milan
INSERT INTO milan

then load rows from DB

SELECT * FROM milan

You have to process your data before you output stuff. This means that you have to follow a certain procedure:

  • load core configuration: database connection etc.
  • insert / update tables and fill / preset variables
  • select stuff from the (now updated) database
  • output (most likely html)