PHP prevent form from submitting WITHOUT redirect

  mysqli, php

I tried to prevent a form from resubmitting by setting a session variable to see if variable isset yet, if it is set then I unset it but it dosent prevent form from submitting. I dont want form to resubmit since it would insert into my table as many times as I refresh. Note as said in title I cannot use any sort of redirects.

    // checks if form is already submitted, so as to not insert values into tables multipe times
    if(isset($_SESSION['indicator'])) 
        unset($_SESSION['indicator']);
        
    else{
        $_SESSION['indicator'] = "processed"; 

    // open connection
    if(!$conn)
        exit("Error: could not connect");

    // check if form is submitted
    if(isset($_POST['submit'])){
    
        if (!empty($_POST['name']) && !empty($_POST['number']) && !empty($_POST['desc']) && !empty($_POST['grade'])){
            $sql = "INSERT into courses (name,number,descrption,grade) values ('$_POST[name]','$_POST[number]','$_POST[desc]','$_POST[grade]')";
            $insert = mysqli_query($conn,$sql);

            if(!$insert)
            echo "ERROR: Failed to insert " . mysqli_error($conn);
        }
        else{
            echo 'Must enter all input fields';
        }
    }
}

Source: Ask PHP

LEAVE A COMMENT