Arlind
Arlind

Reputation: 434

Multiple forms on 1 html

I am trying to do the following. Have 6 different forms on an html file which are all post forms. I want to have the buttons at the top of the page and each of the corrispond to a specific form.

What is the best way to achieve this?

I can't seem to get the post methods working in the destination files.

Upvotes: 1

Views: 2053

Answers (1)

zedfoxus
zedfoxus

Reputation: 37069

You can do something like this below. We create two buttons on top of the page. They both call submitform JavaScript function but with different parameters. The parameter is the name of the form. The function just takes the name of the form and submits it.

form.php
--------

<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    echo '<pre>';
    print_r($_POST);
    echo '</pre>';
}
?>

<button id="button1" name="b1" onclick="submitform('form1');">Submit Form1</button>
<button id="button2" name="b2" onclick="submitform('form2');">Submit Form2</button>

<form id="form1" action="form.php" method="POST">
<input type="text" id="i1" name="i1">
<input type="hidden" id="h1" name="i1" value="form1">
</form>

<form id="form2" action="form.php" method="POST">
<input type="text" id="i2" name="i2">
<input type="hidden" id="h2" name="i2" value="form2">
</form>

<script>
function submitform(formname) {
    document.forms[formname].submit();
}
</script>

Result:

When you press Submit Form1:
Array
(
    [i1] => form1
)

When you press Submit Form2:
Array
(
    [i1] => form2
)

Upvotes: 1

Related Questions