根据不同的按钮提交动态获取数据

时间:2019-04-01 12:32:36

标签: php html mysql

我有3个按钮可以提交。假设按钮A,B,C。 每次按下按钮都会基于按钮从DB中获取数据。但是数据模板是相同的。

我的问题是可以动态更改查询。尤其是查询中的WHERE字段。

$sql = "SELECT Name FROM DevicesList WHERE Device='A' ";

我想要的是哪里零件应该根据按钮的按下而改变。

//my forms are as follows
    <div class="button_style1">
      <form action="displayData.php" method="get" target="_blank">
        <input type="submit" id ="mybutton1" value="A" />
      </form>
    </div>

    <div class="button_style2">
      <form action="displayData.php" method="get" target="_blank">
        <input type="submit" id ="mybutton2" value="B" />
      </form>
    </div>


    <div class="button_style3">
      <form action="displayData.php" method="get" target="_blank">
        <input type="submit" id ="mybutton3" value="C" />
      </form>
    </div>

//I am trying to avoid creating different pages for each button press. Just one page (displayData.php) but with different data based on button press.

3 个答案:

答案 0 :(得分:1)

是的,绝对可以。

为每个元素命名相同:

services.AddHttpClient<ApiClient>(c => { ... });

在发布的表单中,获取提交的按钮:

<input type="submit" name="submit" id ="mybutton1" value="A" />

一次只能提交一个提交按钮。

因此,您每次都会获得Submit按钮的名称,这就是您在SQL中进行比较的地方。

答案 1 :(得分:0)

您可以使用隐藏字段来传递一些数据。

例如:

<div class="button_style1">
  <form action="displayData.php" method="get" target="_blank">
    <input type="hidden" name="MyHiddenData" value="A">
    <input type="submit" id ="mybutton1" value="A" />
  </form>
</div>

<div class="button_style2">
  <form action="displayData.php" method="get" target="_blank">
    <input type="hidden" name="MyHiddenData" value="B">
    <input type="submit" id ="mybutton2" value="B" />
  </form>
</div>


<div class="button_style3">
  <form action="displayData.php" method="get" target="_blank">
    <input type="hidden" name="MyHiddenData" value="C">
    <input type="submit" id ="mybutton3" value="C" />
  </form>
</div>

然后,在 displayData.php

$MyDevice = $_GET['MyHiddenData'];

答案 2 :(得分:0)

您可以按照以下步骤进行操作。

<div class="button_style1">
    <form action="displayData.php" method="get"  target="_blank">
        <input type="submit" name="submit" id ="mybutton1" value="A" />
    </form>
</div>

<div class="button_style2">
    <form action="displayData.php" method="get" target="_blank">
        <input type="submit" name="submit" id ="mybutton2" value="B" />
    </form>
</div>


<div class="button_style3">
    <form action="displayData.php" method="get" target="_blank">
        <input type="submit" name="submit" id ="mybutton3" value="C" />
    </form>
</div>

在displayData.php

if (isset($_GET['submit']) && in_array($_GET['submit'], array('A', 'B', 'C'))) {
    $variable = mysqli_real_escape_string($con, $_GET['submit']); //$con - database connection object
    $sql = "SELECT Name FROM DevicesList WHERE Device='" + $variable + "' ";
}