1

I have a simple form

<!DOCTYPE html>
<html>
    <head>
        <script>
            //prevents website to send another POST request when user refreshes website
            if ( window.history.replaceState ) {
                window.history.replaceState( null, null, window.location.href );
            }
        </script>

    </head>


    <body>      
        <div class="content">
            <form method="post">
                <label>StudentID : </label>
                <input type="text" name="studentid">
                <p id="errorMessage"></p>
                <input type="submit" value="Book Meeting">
            </form>
        </div>
    </body>
</html>

And a short snippet of my php code:

<?php
    //must be connected to the database before running the website
    require_once("connection.php");

    //if the form is submitted(to search for student id)
    if($_SERVER['REQUEST_METHOD'] == "POST")
    {

        $studentid = $_POST['studentid'];

        //if the input is not empty
        if(!empty($studentid))
        {

Whenever I try to submit the form with any input, the if($_SERVER['REQUEST_METHOD'] == "POST") does not detect POST method, however it works when I submit the form with an empty input. I was debugging the whole night and still could'nt figure out the problem. Any help will be appreciated!!

Boom
  • 95
  • 6
  • This doesn't make any sense. Whether you submit data or not doesn't affect what http method the form submits with. It would still send a POST, even if the fields are empty. How exactly have you checked this behaviour? It would only send a GET when you first load the form, not when you submit it – ADyson Nov 10 '21 at 06:42
  • @ADyson I've tried to insert echo statements before and after the `if($_SERVER['REQUEST_METHOD'] == "POST") ` to find out that if i submit the field with input, php does not enter the if statement. – Boom Nov 10 '21 at 06:50
  • 2
    Do a `var_dump($_SERVER);` (before the first if-statement) and check what it says. Also, it's better to solve the _"prevents website to send another POST request when user refreshes website"_ by doing the [Post-Redirect-Get](https://stackoverflow.com/questions/4142809/simple-php-post-redirect-get-code-example) method on the server instead of handling it in the client. – M. Eriksson Nov 10 '21 at 07:31
  • 2
    First of all, try and remove that JavaScript that is meddling with the history. Does the problem still persist after that? If so, keep an eye on what is happening in the network panel of your browser dev tools. – CBroe Nov 10 '21 at 07:56
  • thanks MagnusEriksson and CBroe it seems to work after removing the javascript snippet, I'll try to implement the PRG on the server side instead. – Boom Nov 10 '21 at 16:25

2 Answers2

1

Seems like the javascript code

//prevents website to send another POST request when user refreshes website
if ( window.history.replaceState ) {
   window.history.replaceState( null, null, window.location.href );
}

is interfering with my POST requests from the HTML form, as suggested by CBroe and Magnus.

PRG method will be implemented on the server side instead of client side as suggested by Magnus.

Boom
  • 95
  • 6
  • 1
    This has the features of a helpful answer, @Boom. It references the offending fragment clearly and provides a link to another thread that clarifies the theory. Remember, your answers today will be seen in the future beyond your immediate question/issue, and the more concrete and helpful they are the more they help future developers. Keep up the good work! – quinny Nov 13 '21 at 07:04
-1

The $_SERVER['REQUEST_METHOD'] returns GET because you are requesting the web page via the browser. The easiest way to check your input data without trouble is:

<?php
    //must be connected to the database before running the website
    require_once("connection.php");

    //if the form is submitted(to search for student id)
    if( isset( $_POST['studentid'] ) and !empty( $_POST['studentid'] ) )
    {

        $studentid = $_POST['studentid'];
ouflak
  • 2,458
  • 10
  • 44
  • 49