1

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:

enter image description here

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

enter image description here

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]
3
  • 3
    You can't put php in post content. Look into creating a shortcode or custom template.
    – Milo
    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? Aug 10, 2017 at 13:49
  • I Milo. It worked fine. I inserted an ADDITION to the original question. Thanks Aug 10, 2017 at 14:06

1 Answer 1

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

5
  • Still I have problem to have the post working. See additional notes. Aug 10, 2017 at 12:02
  • Are you suggesting inserting $_POST variables directly into a SQL query without sanitizing? Bad idea Aug 10, 2017 at 14:55
  • 1
    Read the question properly @RobertDundon.
    – user106916
    Aug 11, 2017 at 5:10
  • what problem you are facing @MarioStefanutti
    – user106916
    Aug 11, 2017 at 5:12
  • 1
    Already solved: The problem I had was to prepare a WP post with a form, then use PHP to call an external JSON service and after that present the data on the same page. I didn't need a DB. I used the "insert-php" plug-in to embed php into the page. About forms they can be done using directly the HTML or Ninja Forms or other. Aug 11, 2017 at 13:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.