tags:

views:

814

answers:

4

The php page is called page.php; this pages has 2 submit forms on it: form1 and form2. When one of the form's submit button is pressed what in the HTML header with identify which form was submitted?

+9  A: 

I don't believe that it does post any identification. The easiest way to have your code know which form posted is to put a hidden field in each form identifying the form like this:

<form id="form1">
  <input type="hidden" name="formName" value="form1"/>
  <input type="submit" value="submit" />
</form>
<form id="form2">
  <input type="hidden" name="formName" value="form2"/>
  <input type="submit" value="submit" />
</form>
Jacob
You can also give name to submit buttons and use that for identifying the forms.
che
+1  A: 

What about the action attribute of the form tag?

I would have guesssed that you could specify a different action attribute (each with a different URI value) in the different form instances.

Also, +1 to adding a name attribute to the submit buttons: if you do then the name of the "successful" (i.e. clicked) submit button will be added to the string of names-plus-values which the form returns to the server.

ChrisW
+1  A: 

As mentioned in che's comment on Jacob's answer:

<form id="form1">
  <input type="submit" value="submit" name="form1" />
</form>
<form id="form2">
  <input type="submit" value="submit" name="form2" />
</form>

And then in your form handling script:

if(isset($_POST['form1']){
    // do stuff
}

This is what I use when not submitting forms via ajax.

rpflo
A: 

the way "rpflo" uses does not identify forms. the $_POST['form1'] here corresponds to the input with name="form1", not to the form with id="form1".

there are IMHO two reasonable ways to identify two forms on one page. first is via 'action' attribute by adding a GET variable in to it, like action="mypage.php?form_id=1". and second way, which is imho more practical, is to name all inputs like an array. for example:

<form>
  <input name="form1[first_name]" />
  <input name="form1[last_name]" />
</form>
<form>
  <input name="form2[first_name]" />
  <input name="form2[last_name]" />
</form>

then you have $_POST['form1']['first_name'] and so on..