PHP delete values from form if successful

I would like the values of this form to be deleted if the form is submitted successfully, everything is ok (does not delete) if there is any error but if there is no values remain there, I believe it is a logic error.

    <form action="" method="POST" enctype="multipart/form-data">
    <label>Image<br><input type="file" name="file"></label><br><br>
    <label>Name for the image<br><input type="nameImg" name="imgName" value ="<?php if (!empty($_POST['imgName'])) { echo $_POST['imgName']; } else if (empty($database->errors())) { echo ''; } ?>"></label><br><br>
    <label>Link you wish it to have<br><input type="text" name="imgLink" value ="<?php if (!empty($_POST['imgLink'])) { echo $_POST['imgLink']; } else if (empty($database->errors())) { echo ''; } ?>"></label>
    <br>
    <input type="submit" value ="Upload Image">
</form>
<?php
if (isset($_POST['imgLink'], $_POST['imgName'], $_FILES['file']['name'])) {

    $imgName = htmlentities('\'' .$_POST['imgName']. '\''); //Because of this $imgName will never be empty, we have to pass the pure input to imageInputCheck($_POST['imgName'])
    $link = htmlentities($_POST['imgLink']);

    $name = htmlentities($_FILES['file']['name']);

    $temp = explode('.', $name);
    $file_extn = strtolower(end($temp));

    $tmp_name = $_FILES['file']['tmp_name'];
    $path = "images/" .uniqid('', true). '.' .$file_extn;



    if ($database->imageInputCheck($link, $_POST['imgName'], $name, $file_extn)) {
        $database->insertFolder($tmp_name, $path);
        $database->insertDB($path, $link, $imgName);
        echo '<br>Image upload successful.';
    }
    else {
        foreach ($database->errors() as $error) {
            echo '<div id="errors">' .$error. '</div>';
        }
    }
}

DB.php

protected $_errors = [];

public function imageInputCheck($link, $imgName, $name, $file_extn) {

        if (empty($link) && empty($name) && empty($_POST['imgName'])) {
            $this->addError('<br>Fill all<br>Don\'t be stupid');
        }
        else if (empty($link) || empty($name) || empty($_POST['imgName'])) {
            $this->addError('<br>You forgot to:<br><br>');
            if (empty($name)) {
                $this->addError('Upload an image');
            }
            if (empty($_POST['imgName'])) {
                $this->addError('Give a name to your image');
            }
            if (empty($link)) {
                $this->addError('Give a link to your image');
            }
        }

        else if (($file_extn == 'jpg' || $file_extn == 'png' || $file_extn == 'tif' || $file_extn == 'gif' || $file_extn == 'jpeg') && strlen($imgName) <= 60) {
            return true;
        }
        else {
            $this->addError('<br>Couldn\'t upload file to database or the folder, make sure it\'s an image.<br>Make sure it\'s name is under 60 characters');
        }
        return false;
    }

private function addError($error) {
        $this->_errors[] = $error;
    }

    public function errors() {
        return $this->_errors;
    }
Author: Icaro Martins, 2014-07-31

2 answers

In PHP, there is no effective way to give an unset on $_POST as a whole. You can even give an unset directly to the keys you used, for example:

unset($_POST['imgName']);
unset($_POST['imgLink']);
...

But, in any case, the only effective and safe solution is to make a page forwarding, or, better yet, perform the processing in a separate file (indicated in the "action" parameter of the form tag) and redirect back to the page at the end....

header('location:caminho/pagina-de-volta.php');

In case of errors, you can use Sessions or queries strings to signal the problem, retrieve values in the form, etc.

 1
Author: Blau, 2014-08-03 06:27:36

Dear Miguel, I think it is important to eliminate the logic of Html, separating into distinct things:

<?php
$imgName = isset($_POST['imgName']) ? $_POST['imgName'] : ""; 
$imgLink = isset($_POST['imgLink']) ? $_POST['imgLink'] : "";
?>

<form action="" method="POST" enctype="multipart/form-data">
    <label for="file">Image<br></label>
    <input type="file" name="file">
    <br><br>
    <label for='nameImg'>Name for the image</label>
    <input type="nameImg" name="imgName" value ="<?php echo $imgName ?>">
    <br><br>
    <label for="imgLink">Link you wish it to have</label><br>
    <input type="text" name="imgLink" value ="<?php echo $imgLink ?>">
    <br>
    <input type="submit" value ="Upload Image">
</form>

And inside the loop that validates and saves:

if ($database->imageInputCheck($link, $_POST['imgName'], $name, $file_extn)) {
    $database->insertFolder($tmp_name, $path);
    $database->insertDB($path, $link, $imgName);
    unset($_POST);
    echo '<br>Image upload successful.';
}

If everything is right, it will eliminate the variables of the post, and when you return to the form, the conditional assignments will set the values with " ".

 0
Author: Marcelo Aymone, 2014-07-31 12:58:08