admin管理员组

文章数量:1122846

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]

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]
Share Improve this question edited Aug 10, 2017 at 14:04 Mario Stefanutti asked Aug 10, 2017 at 10:11 Mario StefanuttiMario Stefanutti 1111 gold badge1 silver badge4 bronze badges 3
  • 3 You can't put php in post content. Look into creating a shortcode or custom template. – Milo Commented Aug 10, 2017 at 13:10
  • 1 Ok, I used the plugin it.wordpress.org/plugins/insert-php and not it seems to be worgling, BUT I was not able access the field from the form (names). "On their website I read: The code between the tags must be complete in and of itself. References to variables or code blocks outside the area between the tags will fail. See the “more information” URL for an explanation of this.". I cannot access the names field from the form. Any suggestion? – Mario Stefanutti Commented Aug 10, 2017 at 13:49
  • I Milo. It worked fine. I inserted an ADDITION to the original question. Thanks – Mario Stefanutti Commented Aug 10, 2017 at 14:06
Add a comment  | 

1 Answer 1

Reset to default 0

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

本文标签: phpPostformactionresults on the same page