ben a ben a - 2 months ago 13
HTML Question

Submitting form when there are 2 forms

How do I send the contents of the form with out sending the contents of my other form on the same page? For example

<form class="form" method="get" action="page.php">
<input type="text" value="hi" name="forminput1">
<input type="submit" value="send">
</form>
<form class="form" method="get" action="page.php">
<input type="text" value="byebye" name="forminput2">
<input type="submit" value="send">
</form>


page.php:

if (isset($_GET['forminput1'])) {
//some code
}
if (isset($_GET['forminput2'])) {
//some code
}


Whenever I submit form #2, I end up submitting form #1.

Answer

You have not given any of your inputs a name attribute. Without a name="somename" attribute the browser will not pass anything back on the GET or POST.

If you add a name attribute like this

<form class="form" action="page.php">
    <input type="text" value="hi" name="data">
    <input type="submit" value="send" name="send">
</form>
<form class="form" action="page.php">
    <input type="text" value="byebye" name="data">
    <input type="submit" value="send" name="send">
</form>

It will suddenly start to work as you expect.

If you want to make both forms unique you can add a different name to the submit buttons.

<form class="form" action="page.php">
    <input type="text" value="hi" name="data">
    <input type="submit" value="send" name="send_form1">
</form>
<form class="form" action="page.php">
    <input type="text" value="byebye" name="data">
    <input type="submit" value="send" name="send_form2">
</form>

and then in your PHP, you will be able to differentiate between which form (button) is being submitted like this

<?php
if ( $_SERVER['REQUEST_METHOD'] == 'GET' && isset($_GET['send_form1'])) {
    // User sent form1
}

if ( $_SERVER['REQUEST_METHOD'] == 'GET' && isset($_GET['send_form2'])) {
    // User sent form2
}