简体   繁体   English

具有不同名称的多个提交按钮,一个操作php

[英]multiple submit buttons with different names one action php

he i'm trying to select all the record with a certain date out of my database when a submit is hit but the submits are created out of dates from the database. 当单击提交时,我试图从数据库中选择具有特定日期的所有记录,但提交是从数据库中过期的。 like 喜欢

<?php
        echo "<form method='post' action=''><table class='turflist'>
              <tr>
              <th>Turf list Date</th>
              </tr>";

        foreach($backup_results as $row){
            echo "<tr>";
            echo "<td><button class='turfButton' type='submit' name='". $row['date'] ."'>" . $row['date'] . "</button></td>";
            echo "</tr>";
        }
        echo "</table></form>";
    ?>

now i want to check if the submit of a date for example 2017-03-02 isset and then select all the record with the date 2017-03-02 out of the database. 现在我想检查是否设置了提交日期(例如2017-03-02),然后从数据库中选择日期为2017-03-02的所有记录。 But the same query should also work with the date 2016-20-03 for example. 但是,例如,同一查询也应与日期2016-20-03一起使用。 Can anybody help me with this. 谁能帮我这个忙。

You can on submit check for each individual name... so: 您可以在提交检查中为每个个人名称...这样:

if(isset($_POST['2017-03-02'])){

}

But a better way to do this would be dynamically build radio buttons instead with ONE name, dynamic values. 但是更好的方法是动态构建单选按钮,而不是使用一个名称,动态值。 THEN have ONE submit button. 然后有一个提交按钮。 Like so: 像这样:

?php
    echo "<form method='post' action=''><table class='turflist'>
          <tr>
          <th>Turf list Date</th>
          </tr>";

    foreach($backup_results as $row){
        echo "<tr>";
        echo "<td><input type='radio' name='dates' class='turfButton'  value='". $row['date'] ."'>" . $row['date'] . "/></td>";
        echo "</tr>";
    }
    echo "</table>
echo "<input type='submit' name='submit' value='submit' />";
</form>";

if(isset($_POST['submit'])){
  $date = $_POST['dates'];
  // RUN YOUR MYSQL QUERY HERE USING $date, which would be the selected radio button
}
?>

I'm not quite sure how you stored your date in the table. 我不太确定您如何将日期存储在表中。 but however the $row['date'] is dynamic therefore you can just assign $row['date']; 但是$row['date']是动态的,因此您可以分配$row['date']; to a new variable. 到一个新变量。 when the form button loads your the name because what ever that is in that variable, so you just need to check if that variable isset and fetch everything that equals that variable. 当表单按钮加载您的名称时(因为该变量中包含的内容),因此您只需要检查该变量是否已设置并获取与该变量相等的所有内容即可。

<?php
echo "<form method='post' action=''><table class='turflist'>
              <tr>
              <th>Turf list Date</th>
              </tr>";

foreach ($backup_results as $row) {
    echo "<tr>";

      $datebtn = strtotime($row['date']);

    echo "<td><button class='turfButton' type='submit' name='" .$datebtn . "'>" . $datebtn  . "</button></td>";
    echo "</tr>";

}
echo "</table></form>";


if (isset($_POST["$datebtn"])) {

    $sql = $conn->prepare("SELECT * FROM tbl  WHERE DATE(DateField) = ? ");
    $sql->bind_param("s",$datebtn);
    $sql->execute();

  //continue fetch ur results

}

?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM