Check if form has been submitted to itself once do one thing if back again do another?

StackOverflow https://stackoverflow.com/questions/23411719

  •  13-07-2023
  •  | 
  •  

Frage

I have a form that submits to itself. When I want to edit something I take the data and place it in place of empty fields. I am trying to make a check to see if the the form has already been submitted once so that it knows that if it has come back again and put data in the blank fields on the next submit it runs an update query.

I thought what I would do is make a variable after the query is run the first time called $submitted and set that to true then when the form goes back to itself and see that true it can set another variable to $submitted_twice which will then let me run the edit query.

This approach doesn't seem to be working and I can not figure out why. Thank you for your help!

War es hilfreich?

Lösung

That wouldn't work because that variable is not persistent. Some alternatives to store this var across requests could be:

  1. Use the $_SESSION global var to store the number of times your form has been submitted:

    (Here I'm assuming your form uses POST as requesting method, and your submit button id is "submit")

    if( $SERVER['REQUEST_METHOD'] == 'POST' && !empty( $_POST['submit'] ) ){
    
          //
          //deal with your form action here
          //
    
          //Here's the bit where you store/update your counter
          if( !isset( $_SESSION['submitted'] ){
              $_SESSION['submitted'] = 1;
          else {
              $_SESSION['submitted']++;
          }
    
          //Echo your counter
          echo "This form has been submitted " . $_SESSION['submitted'] . " time(s)";
    
    }
    
  2. Use a hidden field in the form to store your counter. Check this answer hidden field in php

Hope it helps

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top