danplaton4
danplaton4

Reputation: 136

How to check if form was submitted

My question is how to check which form was submitted if I have 2 different forms with the same name, and I can't change the name.

For example I have 2 forms and 2 php scripts that evaluate them:

<form action=mypage.php method=post>
  <input type=text name=name>
  <input type=submit name=ok value=ok>
</form>

<?php
  if(isset($_POST['ok'])) {
    $name=$_POST['name'];
  }
?>

<form action=mypage.php method=post>
  <input type=text name=pass>
  <input type=submit name=ok value=ok>
</form>

<?php
  if(isset($_POST['ok'])) { // <- here is wrong
    $pass=$_POST['pass]']; // this code is not executing
  }
?>

How I can differentiate these two submits without changing their names?

P.S I can't bring the (Forms) together.

Upvotes: 1

Views: 4006

Answers (1)

hans-k&#246;nig
hans-k&#246;nig

Reputation: 553

One solution is to add a hidden input on both forms:

<input type="hidden" name="form1" value="name" />

and:

<input type="hidden" name="form2" value="pass" />

Then to check which one has been submitted:

if(isset($_POST['ok']) && isset($_POST['form1'])){

// For the first form

}

And:

if(isset($_POST['ok']) && isset($_POST['form2'])){

// For the second form

}

Upvotes: 4

Related Questions