0

Here is my index.php

<!doctype html>
<html>
<head>
    <title>welcome to vikash general shop</title>
    <link rel="stylesheet" type="text/css" href="css/table_styling.css">
</head>
    <body>
        <table>
            <tr>
                <td>Item</td>
                <td>Amount(kg)</td>
                <td>Amount(gm)</td>
            </tr>
            <tr>
                <td>Sugar</td>
                <td><form method="POST" action="process.php"><input type="text" name="sugar_amount_kg"/></form></td>
                <td><form method="POST" action="process.php"><input type="text" name="sugar_amount_gm"/></form></td>
            </tr>
            <tr>
                <td>Rice</td>
                <td><form method="POST" action="process.php"><input type="text" name="rice_amount_kg"/></form></td>
                <td><form method="POST" action="process.php"><input type="text" name="rice_amount_gm"/></form></td>
            </tr>
        </table>
        <form method="POST" action="process.php">
            <input type="submit" name="submit" value="submit" />
        </form>
    </body>
</html>

actually i want to send data of all the forms in page using one submit button it is not working. It's obvious because submit button is only sending it's own form tag(quite selfish :P). So i want to know how to send data of all the forms using one submit button...
or if you have any other solution for my code then please tell me...

chandola
  • 126
  • 1
  • 10

3 Answers3

4

Just make one form. Wrap your table with form tags, because it's all being processed by process.php anyway.

    <form method="POST" action="process.php">
    <table>
        <tr>
            <td>Item</td>
            <td>Amount(kg)</td>
            <td>Amount(gm)</td>
        </tr>
        <tr>
            <td>Sugar</td>
            <td><input type="text" name="sugar_amount_kg"/></td>
            <td><input type="text" name="sugar_amount_gm"/></td>
        </tr>
        <tr>
            <td>Rice</td>
            <td><input type="text" name="rice_amount_kg"/></td>
            <td><input type="text" name="rice_amount_gm"/></td>
        </tr>
    </table>
    <input type="submit" name="submit" value="submit" />
    </form>
Expedito
  • 7,771
  • 5
  • 30
  • 43
  • thanks i solved this problem.. just after asking here i go to facebook page see their log in page. they do it in same way as you have said.. – chandola Jul 16 '13 at 16:05
  • @VVV The OP ask for a way to submit multiple forms with one button. While it might be better to do it in one form, this doesn't really answer his question. – msnider Jul 16 '13 at 16:12
  • @msnider - Feel freee to answer that question if you like. Sometimes it's better to solve problems than try to answer questions in their most literal sense. – Expedito Jul 16 '13 at 16:15
  • @VVV - If you liked the answer, please consider clicking on the check mark to the left to accept it. Thanks! – Expedito Jul 16 '13 at 17:11
2

You do not need to add the form tags multiple times. Just wrap it around the input fields and add the action attribute in it like so:

<table>
    <form action="process.php" method="post">
        <tr>
            <td>Item</td>
            <td>Amount(kg)</td>
            <td>Amount(gm)</td>
        </tr>
        <tr>
            <td>Sugar</td>
            <td><input type="text" name="sugar_amount_kg"/></td>
            <td><input type="text" name="sugar_amount_gm"/></td>
        </tr>
        <tr>
            <td>Rice</td>
            <td><input type="text" name="rice_amount_kg"/></td>
            <td><input type="text" name="rice_amount_gm"/></td>
        </tr>
        <input type="submit" name="submit" value="submit" />
    </form>
</table>

And then you can get the inputs in process.php as follows:

if(isset($_POST['submit'])){ //checking if form was submitted
    $sugar_amount_kg = $_POST['sugar_amount_kg'];
    ...
}

Hope this helps!

Amal Murali
  • 75,622
  • 18
  • 128
  • 150
0

This question has been asked a few times. Do a quick search and you will see ways of doing this with JS/jQuery. Examples here and here.

Doing it in one form really might make more sense though for your specific use.

Community
  • 1
  • 1
msnider
  • 444
  • 1
  • 4
  • 9