Tom
Tom

Reputation: 425

php two self submitting forms on one page

I have a webpage form that submits to itself to carry out php action. I want to add a second form to this same webpage that is capable of self submit as well but I am not having any luck finding a working solution for my setup. Here is what my webpage looks like.

First, it checks to see if the page has already been submitted, and if it has, it redirects elsewhere.

if($_SERVER['REQUEST_METHOD'] == "POST") {
header("Location: viewcustomers.php");
}

Next, the form itself.

<form id="addCustomer" method="POST" action=""> ..stuff.. </form>

Then, finally my form action.

if('POST' == $_SERVER['REQUEST_METHOD']) {
..phpstuff..
}

How could I adjust this form action (or add another) to differentiate between two different forms?

Thanks.

Upvotes: 1

Views: 1179

Answers (1)

Raphael Caixeta
Raphael Caixeta

Reputation: 7846

Easy!

<?php
    if(isset($_POST['action']) && $_POST['action'] == 'form1') {
        // Form 1
    } else if(isset($_POST['action']) && $_POST['action'] == 'form2') {
        // Form 2
    }
?>

<form action="#" method="post">

    <input type="hidden" name="action" value="form1" />
</form>

<form action="#" method="post">

    <input type="hidden" name="action" value="form2" />
</form>

Upvotes: 3

Related Questions