0

I am writing a small script to run on a home server which will handle form data via post, but should submit to a page with get values. I.e. the index.php file receives a $_GET which sets which page is loaded.

If the page index.php?page=form is loaded then a form should appear, this works fine, but the form handling is on the same page.

Having a form like so:

<form action="index.php?page=form" method="post">
    <input type="text" name="email">
    Other form fields here
</form>

will post the data to index.php? instead of index.php?page=form.

Can this be done?

1 Answer 1

1

The code above is working for me. If I press enter, I get to the site index.php?page=form. Maybe your problem is anywhere else. Here are two options, maybe one of them are useful for you...

Is this possible for you?

If no $_POST is set, the form will displayed, if you submit the form, you can process the datas. So you dont need the get variable anymore.

<?php if (isset($_POST['email'])): ?>
  <!-- process the inputs -->
<?php else: ?>
<form action="index.php" method="post">
  <input type="text" name="email">
  <!-- Other form fields here -->
  <button type="submit">Submit</button>
</form>
<?php endif; ?>

If you need the $_GET variable, try

<?php 
if ($_GET['page'] == 'form'):
  //process the inputs
else: ?>
<form action="index.php" method="GET">
  <input type="hidden" name="page" value="form">
  <input type="text" name="email">
  <!-- Other form fields here -->
  <button type="submit">Submit</button>
</form>
<?php endif; ?>
1
  • Having given it a day it has started working. the only thing I can conclude is that it was a caching issue with my browser. I'll accept this answer as it does show the correct method of doing this. Jun 18, 2018 at 8:37

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.