Jump to content

Prevent Double Form Submission + Error Message


Foxie

Recommended Posts

Hello! I am new here, so i hope i dont make any mistakes when it comes to rules here on the forums :(

But i am really devastated at the moment. For an assignment of mine, i already wrote a normform. But now, the task is that i prevent the double submit when the page is refreshed, and show an error message that the uploaded file (for thats the normform for) was already submitted. 

I simply am not able to do it!

 

So far i managed to prevent the double submission. I can now refresh the page and it does not upload the file again. I made this possible by redirecting to my own page after the process form function is called, so that the POST is transformed into a GET request. But i simply cannot make the message show up.

 

This is my code i have so far for this:

 protected function business()
        {
            if (isset($_SESSION['number']) && isset($_POST['number'])) {
                if ($_POST['number'] == $_SESSION['number']) {

                    unset($_SESSION['token']);

                    if ($this->addImage()) {

                        header("Location: " . $_SERVER['REQUEST_URI']);
                        //exit();

                        $this->statusMessage = "Your file has been uploaded successfully";
                        $this->currentView->setParameter(new GenericParameter("statusMessage", $this->statusMessage));
                        $this->currentView->setParameter(new GenericParameter("images", $this->getImages()));
                        $this->currentView->setParameter(new PostParameter(IMAR::IMAGE_TITLE, true));
                        $this->currentView->setParameter(new PostParameter(IMAR::IMAGE_AUTHOR, true));

                        $_SESSION["number"] = 0;

                        unset($_POST);

                        echo "Test";

                        // redirecting to the same page - makes a get out of the post and prevents
                        // the double upload / the window to confirm to upload it again





                    } else {

                        echo "Test2";
                        $this->errorMessages ["addImage"] = "Error adding image. Please try again";
                    }

                } else {

                    echo "Test 3";
                    $_SESSION["number"] = 2;

                }
            }

And this is my vaidation method. Here the error messages should be generated:

  if ($_SESSION["number"] == 2) {
           echo "Hallo";
           $this->errorMessages[self::IMAGE_UPLOAD] = "File already submitted!";
       }

       if (!isset($_POST)) {
           echo "Hallo2";
           $this->errorMessages[self::IMAGE_UPLOAD] = "File already submitted!";
       }

        // ----------------------------------

        $this->currentView->setParameter(new GenericParameter("errorMessages", $this->errorMessages));

        return (count($this->errorMessages) === 0); 

Can somebody PLEASE tell me what i am doing wrong?? I am sitting at this for three days now. I have only been learning php for 3 months now, and i am simply on the edge of pure frustration :(

 

Love, Foxie

Link to comment
Share on other sites

I'm not even sure what you are trying to do.

 

If you don't want people to upload the same file, then you obviously need a duplicate check for those files -- based on the filename, a checksum of the content or whatever. Playing with sessions doesn't help, because the user could completely log out and then come back with the same file again -- or does that make it acceptable?

 

The solution to your problem (as I understand it) is to come up with unique identifiers for the files (name, checksum, ...), store those identifiers in a database and show an error message when you encounter a duplicate. The easiest and safest uniqueness check is to use a real database system (PostgreSQL, MySQL, SQLite, whatever) which can perform the check by itself. Then you just have to catch the error and show your own message.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.