tags:

views:

94

answers:

3

I have two forms in a page. Have decided to update my post with more realistic code from the page itself...

 <form action="test.php" method="POST">
 <strong>Details of work carried out</strong>
 <textarea name="detailsOfWorkCarriedOut"></textarea>
 <strong>Materials used</strong>
 <textarea name="materialsUsed"></textarea>
 <input type="hidden" name="submitted" value="true">
 <input type="submit" value="Save">
 <form/>
 <br />
 <form action="test.php" method="POST">
 <strong>Details of work not carried out</strong>
 <textarea name="detailsOfWorkNotCarriedOut"></textarea>
 <input type="hidden" name="submitted" value="true">
 <input type="submit" value="Save">
 </form>

"test.php" simply contains:

 <?php
print_r($_POST)
?>

No matter which form I post, I always get the same array returned:

Array ( [detailsOfWorkCarriedOut] => [materialsUsed] => [submitted] => true [detailsOfWorkNotCarriedOut] => )

Why is this?

+1  A: 

Yes, browsers submit only the fields in the form in which the submit button is nested. You could use JavaScript to monitor form submissions and include values from the other form in the submission, but you're seeing the expected behavior right now.

Rafe
A: 

Your submit button is inside one form, so, it only submits that one form.

So, no, $_POST should not contain data that comes from both forms : it should only contain data coming from the submitted one.

(but, why don't you try it ? )

Something ugly like this should do the trick, if you're only willing to test :

<form method="get">
    <input name="a" type="text">
</form>

<form method="get">
    <input name="b" type="text">
</form>

<?php

var_dump($_GET);

(Yeah, it's ugly, I said ^^ )

If you type something into the first field and submit by pressing ENTER, you'll get :

array
  'a' => string 'aa' (length=2)

And, with the second field :

array
  'b' => string 'second field' (length=12)

So, only one form each time :-)

Pascal MARTIN
+1  A: 

Your problem is the

<form/>

at line 8: replace it with

</form>

:)

inakiabt
Why the negative?
inakiabt
If you do what I'm saying, you'll get what you expect
inakiabt
This is exactly the problem, not sure why it got down voted.
chyne
Thanks! The down vote wasn't from me by the way!
chriswattsuk
Sorry! :) I don't know why I thought you did the negative vote :)
inakiabt