PHP erase form values if successful

1

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 are any errors but if there are no values continue there, I think it is a logic error. p>

    <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;
    }
    
asked by anonymous 31.07.2014 / 13:09

2 answers

1

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

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

But in any case, the only effective and safe solution is to resubmit the page, or, better yet, to execute 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 query strings to signal the problem, retrieve values in the form, etc.

    
03.08.2014 / 08:27
0

Dear Miguel, I think it is important to eliminate the logic of Html, separating it into different 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 within 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 correct, it will delete the variables from the post, and when you return to form, the conditional assignments will set the values with " " .

    
31.07.2014 / 13:50