same page post request in php isset check not working?

Trying to get a simple isset form submit to check if the post request had been clicked from within the same page, but I can’t get it working?

<?php
    if(isset($_POST['btnSubmit'])){
        echo "ok";
    }
?>
<form id="form" name="form" action="">
   <fieldset>
      <legend>
         <label for="fullName">Name</label>
      </legend>
      <input placeholder="Your name" type="text" name="fullName" id="fullName" autofocus>
    </fieldset>
    <fieldset>
       <legend>
          <label for="query">Message</label>
       </legend>
       <textarea placeholder="Type your Message Here...." name="query" id="query"></textarea>
    </fieldset>
    <input name="btnSubmit" type="submit" id="api-submit" value="Submit" />
</form>

I would expect this just to echo “ok” out onto the page? I’ve also cut out a lot of unessicary code from my actual work, as im just testing the principle here. But I think I musn’t understand how it works, as it is sending the form contents to the URL, but just I don’t know how to run seperate php code after the submit button had been clicked.

Answer

In your form you haven’t specified that it is a POST request and by default, the method will be GET. You’ll need to add the method="POST" to your form

<form id="form" name="form" action="" method="POST">
   <fieldset>
      <legend>
         <label for="fullName">Name</label>
      </legend>
      <input placeholder="Your name" type="text" name="fullName" id="fullName" autofocus>
    </fieldset>
    <fieldset>
       <legend>
          <label for="query">Message</label>
       </legend>
       <textarea placeholder="Type your Message Here...." name="query" id="query"> 
       </textarea>
    </fieldset>
    <input name="btnSubmit" type="submit" id="api-submit" value="Submit" />
</form>