php after form in wordpress

101 views Asked by At

what I am trying to do is to create a from, and execute a php script after the submit has been pressed. the problem seems to be that the page of wprdpress with the form and the code gets executed all at once.

If I put the code below into a regular test.php file on my server, it does what it is supposed to do (echo "Form Submitted!") after I click submit. However if I put the same code in a page template or a wp page it spits it out all at once (the form, and the "form submitted).

<html>
  <head>
    <title>Notify on Submit</title>
  </head>
  <body>
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
      <label>Name: <input type="text" name="name" /></label>
      <input type="submit" value="Submit" />
    </form>
    <?php if (count($_POST)>0) echo "Form Submitted!"; ?>
  </body>
</html>

I have no idea why this is like that, and would really need some help on this.

What I have also done is to create two different wp pages(one goes to the other). It works, but will create a bit of a mess. I would like to do this in one page.

page 1

<form action="page2" method="POST">
Your form input.
<input type="submit" name="submit" />
</form>

Page 2

<?php 
if (count($_POST)>0) 
{
 echo "Form Submitted!";
 unset($_POST);
 $_POST = array();
}
else echo "Form has been reset!";
?>
3

There are 3 answers

0
Jim Maguire On

Remember that WP uses actions and hooks, and so if you put code "in a WP page" or "in a template" it may fire at various times. You might get an echo statement that fires something to the screen before the content comes out. Consider putting all your output within filters, actions and hooks.

Your logic seems to depend on the post count. Consider using a unique name, i.e. the name of the submit button on your form. Check for if(isset($_POST['my-unique-submit-button'])). Is anything else in WP submitting via post? You might not know!

0
Jef On

Probably there is another form on the Wordpress page using the POST method. Instead of checking for $_POST > 0 (which will only tell you that something was posted) add some identifier to your form, and check for that so you can tell if your form was posted.

A simple way to do this is with a hidden input:

<html>
  <head>
    <title>Notify on Submit</title>
  </head>
  <body>
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
      <label>Name: <input type="text" name="name" /></label>
      <input type="hidden" name="whatform" value="myform" />
      <input type="submit" value="Submit" />
    </form>
    <?php if (isset($_POST['whatform']) && $_POST['whatform'] == 'myform') echo "My Form Submitted!"; ?>
  </body>
</html>
0
eddiemoya On

I think you just need to remove the php from your action. The action defaults to the current url on any form.

PHP_SELF refers to the file path - thats why it works when you do it on just a solo file. As part of the wordpress application, you can't run files directly - you have to reach that url.