PHP delete values from the form if successful

Asked

Viewed 547 times

1

I would like the values of this form to be deleted if the form is successfully submitted, everything is ok (does not erase) if there is any error but if there are no values continue there, I believe it is a logical 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;
    }
  • You have to, after recording in the comic, release the variables.

  • How? excuse my ignorance

2 answers

1

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

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

But anyway, the only effective and secure solution is to resend the page, or better yet, perform the processing in a separate file (indicated in the "action" parameter of the tag form) 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.

0

Dear Miguel, I think it is relevant to eliminate the logic of Html, separating 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 bond 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 all is right, it will delete the post variables, and when you return to the form, the conditional assignments will set the values with " ".

  • I edited the answer.

  • Isn’t browser Autofill enabled? Have browsers that save forms...

  • Nop, I just tested

  • Funny, I faked it here, and it worked, is it saving and in case it comes down to this point of unset? Put some echo's with messages like saved, error, etc at some points, and see where logic is going.

  • Yes, I’ve tried everything you’ve told me... but setting the vars '$imgName' and '$imgLink' as you said gives me the error: "undifined var...." so I left the conditions in the value field even tried inside: <?php if (!Empty($_POST['imgName'])) { echo $_POST['imgName']; } Else if (Empty($database->errors())) { unset($_POST[imgName]); }

  • Only one thing when you say "Autofill" is in html, "autocomplete" in the right input"?

  • Auto-fill... some browsers have this function, but I do not know if it should be activated, because the $_POST empty would be an empty string in the field. So I think something is not being validated, have you checked if it is actually saving and coming in unset? It displays the echo you have after the unset?

  • True, I will update the answer, replacing Empty for isset, the error occurs, because the unset eliminates the $_POST, I only notice something else, if it occurs the user presses F5 the form resends, and for this not to occur, would have to apply session in your app.

  • I do not advise validating inside the HTML by pulling the object $database, this does not belong to the view, it should be checked only within the functions in the file db.php. So I eliminated, and I reshaped the form as I put it in the answer, dealing only with the global $_POST.

  • echo 'image upload Success' happens

  • Tried with the alteration of the !empty() for isset()?

Show 6 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.