0

I am very new to PHP and don't know how to match the name of the submit button to the argument inside the $_POST. $email is a string variable. I echo it to make it be the name of the input button and I also set it as the argument of $_POST. But this does not work. Can anybody tell me why?

<tr>
  <form method="post" action="">
    <input type="submit" name="<?php echo $email?>" value="Approve" class="btn btn-primary">
  </form>
</tr>

<?php 
  if(isset($_POST[$email])) {
    echo "z"; 
  }
?>
Adam Lau
  • 153
  • 2
  • 10

1 Answers1

1

You need <input type="hidden"> with name="email" and value="PHP variable value" with submit button name="action" with value="Approve"

<form method="post" action="">
        <input type="submit" name="action" value="Approve" class="btn btn-primary">
        <input type="hidden" name="email" value="<?php echo $email; ?>"/>
     </form>

Then after posting it you can just check for the action and email

 <?php
 
 if (@$_POST['action'] && $_POST['email']) {
  if ($_POST['action'] == 'Approve') {
          echo $_POST['email'];
  }
}
?>
KUMAR
  • 1,993
  • 2
  • 9
  • 26
  • To check if a post param exists, use `isset()` or `empty()` instead. `@` is used to suppress error messages and is a bad practice to use (since it can hide important errors while debugging). You should also check for both properties. Just because one is set doesn't mean that the other is. Example: `if (isset($_POST['action'], $_POST['email'])) { ... }` – M. Eriksson Jul 01 '20 at 12:34
  • @Magnus Eriksson sir check for both properties we have to use `,` or `&&` in between them. – KUMAR Jul 01 '20 at 12:54
  • The function `isset()` can take one or more variables. If you pass in multiple: `isset($foo, $bar)`, then the function will return `true` only if both variables are set and not null. It's the same thing as doing this: `isset($foo) && isset($bar)` – M. Eriksson Jul 01 '20 at 12:59