Skip to main content
2 de 3
adicionou 1051 caracteres ao conteúdo
Miguel
  • 29,5mil
  • 5
  • 43
  • 80

PHP apagar values do form se for bem sucedido

Eu gostaria que os values deste form fossem apagados se o form for submetido com sucesso, está tudo ok (não apaga) se houver algum erro mas se não houver o values continuam lá, creio que seja um erro de logica.

    <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;
    }
Miguel
  • 29,5mil
  • 5
  • 43
  • 80