Post + form + action + results on the same page

I'm trying to put on a post a custom form and, on the same page I'd like to visualize the results of an action on the form.

But I'm missing the basics.

Essentially I'd like to do something like this. Is it possible?

?php
    if(isset($_REQUEST['submit_btn']))
    {
       echo "div";
       $name = $_POST["names"];
       echo "/div";
    }
?

form action="" method="POST"
   input type="text" name="names" id="names"
   input type="submit" value="submit" name="submit_btn"
/form

ADDITION (after the answer from Tejas gajjar):

Thank for the answer. Actually I don't need to interact with the DB so I removed some of the suggested lines.

If I use this:

Question: Do you know why it renders like this?:

ADDITION (after the comment from Milo):

OK it worked. I was even able to access the form field.

form action="" method="POST"
   input type="text" name="names" id="names"
   input type="submit" value="submit" name="submit_btn"
/form

[insert_php]
    if(isset($_REQUEST['submit_btn']))
    {
       echo "div";
       $name = $_POST["names"];
       echo "/br";
       echo "ANSWER:/br/br", $name;
       echo "/div";
    }
[/insert_php]

Topic php forms posts Wordpress

Category Web


Use this code

<?php
    if(isset($_REQUEST['submit_btn']))
    {
       echo "<div>";
       $name = $_POST["names"];
       echo "</div>";
       $ins="insert into tablename (fieldname)values('$name') ";                
       $conn->query($ins);
       ?>
       <script>
       alert('inserted successfully');
       </script>
       <?php
          $sel="select * from tablename";
          $r=$conn->query($sel);
          while($u=$r->fetch_object())
          {
      echo $u->fieldname;
          }
    }
?>

<form action="" method="POST">
   <input type="text" name="names" id="names">
   <input type="submit" value="submit" name="submit_btn">
</form>

Hope this will help you

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.