2

At the moment, I have a form with an input (text) and a button to submit the form.

The form is posted to 'submit.php'. I would like the form posted to same page as the form.

How can I do this?

<form action="submit.php" method="POST"> <input type="text" class="emailField" placeholder="email address" name="email" /> <button type="submit"> <div class="submit" /> </button> </form> 

submit.php

<?php mysql_connect('localhost', '', '') or die('unable to connect'); mysql_select_db('') or die('unable to select db'); $query = mysql_query(""); if (mysql_num_rows($query) > 0) { exit("You have already subscribed!"); } else { mysql_query("") or die(mysql_error()); exit("You have successfully subscribed!"); } ?> 
2
  • What exit() method? Is your question missing some code? Commented Sep 11, 2011 at 23:11
  • change submit.php to the same page as the form, and check the $_POST to see if the form has been submitted. Commented Sep 11, 2011 at 23:14

1 Answer 1

6

I think you have a couple of options here.

Submit the form onto itself

<?php if('POST' == $_SERVER['REQUEST_METHOD']) { // process the form here } ?> <form action="" method="POST"> <input type="text" class="emailField" placeholder="email address" name="email" /> <button type="submit"> <div class="submit" /> </button> </form> 

Redirect back to the form

Add something like this in submit.php where you want to show the form.

<?php header('Location: http://example.org/form.php'); exit; ?> 
Sign up to request clarification or add additional context in comments.

2 Comments

You need to change the extension on that file from .html to .php so it gets executed as PHP. There are other ways around this but this is the simplest.
You don't even need to specify action="". Just skip that part all together, since default is itself.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.