-1

i am working with multiple form in one page. Basically i have a form in which there are some element and when a user select one of the category from drop-down list in the form, it adds addition elements on the form depending on the category chosen. my problem occurs when i try to get the value from user input and save it into database. After fiddling with the code i realised that the code is unable to catch the value of the form after one div tag has occurred. let me explain through code.

<form>
<input type="text" name="" >
<select name="category" id="category" onchange="show()">
<option name="Phone" value="Phone" >Smartphone</option>
<option name="Laptop" value="Laptop" >laptop </option>
</select>
 //(show() function will make one of the div tag visible(depending on category selected) and so it will display on screen.)

<div id="phone" style="visibility:hidden">
<form>
<some input elements....>
</form>
</div>

 <div id="laptops" style="visibility:hidden">
 <form>
 <some input elements....>
 </form>
 </div>

 </form>

now in php when i try to get the input value using $_POST i can only access the value of first div tag which is with id="phone" but when i chose category laptop, even though it display on screen correctly i am not getting the value of user input. i have checked the forms and there is no error since when i swap the forms around, it have same problem.

kiran patel
  • 135
  • 1
  • 3
  • 10

1 Answers1

2

You have forms contained by other forms. This is not allowed in HTML and usually produces inconsistent and undesired results.

You also have at least one other error (which you repeat). Test your HTML with a validator.

i can only access the value of first div tag which is with id="phone"

Where you have vaguely said <some input elements....>, you might have inputs which share a name. Invisible elements still exist, so the elements in the first (illegal) nested child form will always be submitted.

When PHP populates $_POST and friends, only the first set of data associated with a given name will be stored, the rest will be discarded. (Unless you name the inputs with [] characters).

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • Thank you, i have figured out the solution. Removing forms tags under each divs solves my problem so thank you very much.:) – kiran patel Apr 15 '13 at 22:16