How get (Submit Button' name) in next page in

2019-08-14 20:03发布

I hope my question is clear and has an answer. This my Submit Button (not real code):

while... input type=submit name=$row['ID']

when I click the submit button I need to get its name in the next page. How?

I have different submit buttons and I don't know if the user chose the first, second, third, or x button.

5条回答
该账号已被封号
2楼-- · 2019-08-14 20:17

If the submit button is part of a form with a value, you can access it in the $_POST or $_GET data array on the following page. If your script doesn't know the name of the submit button, loop through the postdata to find it.

查看更多
虎瘦雄心在
3楼-- · 2019-08-14 20:20

If you dont want to change your application design, you can do simply

  foreach ($yourids_used_in_prev_page as $id)
  {
    if (isset($_POST[$id])) // or $_GET if your form method="get"
    {
      // and you are get it!
    }
  }

It is not a beautiful solution, but may be appropriate for you

查看更多
Viruses.
4楼-- · 2019-08-14 20:30

EDIT: matino's answer is more appropriate if it fits your use-case.

If I understand you correctly, you want to get/check the presence of "the_name" after the submission of the following form:

<form method="post">
  <input name="the_name" type="submit">
</form>

Now, all POST'ed data is available in PHP's $_POST superglobal, however PHP has no way of knowing which part of the data is the submit button.

There are two possible ways around this:

  • If you already know the name, you can check its existence with isset($_POST['the_name']) - this will tell you if the user submitted the form by clicking the the_name submit button (however, if the hit enter, the variable would not be present).
  • If you can change the HTML, you could use subscripting by changing all submit element names to, e.g., submit[the_name] - this way you could find the name of the pressed submit button by looking at array_keys($_POST['submit'])[0].
查看更多
走好不送
5楼-- · 2019-08-14 20:32

Instead of assigning an id to the button name, use query string and submit to your_script.php?id=$row['ID']. Then on the next page use $_GET['id'] to get the button name.

查看更多
做自己的国王
6楼-- · 2019-08-14 20:43

Based on your comment you actually need to find out if a button was pressed. Check the example below.

Here is a simple form with a username input and three buttons to perform a variety of actions on that username:

<form action="next_page.php" method="post">
  <input type="text" name="myname" />
  <input type="submit" name="get_myname" value="Give my name back" />
  <input type="submit" name="rand_name" value="I will give you some name" />
  <input type="submit" name="color_name" value="Your name as a rainbow" />
</form>

Once one of the submit buttons is pressed, we go to next_page.php and check to see which one it was and perform an action based on that.

<?php
  //this is next_page.php
  if (isset($_POST['get_myname']))   {
    //echo user's name to screen

  } elseif ( isset($_POST['rand_name']) )   {
    //echo a random name

  } elseif ( isset($_POST['color_name']) )   {
    //change every letter to a different colour

  } else   {
    //nothing to do here, just nice to have a comment letting you know
  }
?>
查看更多
登录 后发表回答