如何使用 PHP 处理同一页面和同一表单上的不同提交按钮?

问题描述 投票:0回答:0

使用不同的按钮发送数据

我正在尝试使用 2 个不同的提交按钮从同一页面上的表单提交数据,如您所见,这些是按钮:

  1. 保存按钮:

    type="submit" name="button" value="guardar" id="guardarcalificaciones"

  2. 更新按钮

    type="submit" name="button" value="actualizar" id="actualizarCalificaciones"

注意:我正在使用 PDO

我想要做的是每个按钮都有不同的行为并将数据从表单发送到同一页面,目前我有这段代码可以发送数据并成功保存!但我希望这仅适用于保存按钮

name="guardar" value="guardar" id="guardarcalificaciones"
,以及更新按钮
name="actualizar" value="actualizar" id="actualizarCalificaciones"
我想修改$query以便为其分配此查询:

$query = "UPDATE dgirew.dbo.actas_w
        SET calif = :calif,
            exp_prof_calif = :exp_prof_calif,
            fecha_modicalif = :fecha_modicalif,
            estatus = '3'
        WHERE exp_alum = :exp_alum
        AND asig = :asig
        AND gpo = :gpo
        AND folio = :folio
        ";

表格

这是它在同一个名为 indexGrupo.php 的文件中的形式

<form method="POST" action="indexGrupo.php" id="formularioActas">
  <div class="table-responsive">
    <table id="ActasGrupo" class="boleta">
      <tbody>
        <?php
        include_once './model/loginModel.php';
        $model = new loginModel();
        foreach ($resultadoModalVerMasLista as $row) {
        ?>
          <input type="hidden" name="asig" value="<?php echo $_GET['asig']; ?>">
          <input type="hidden" name="gpo" value="<?php echo $_GET['grupo']; ?>">
          <input type="hidden" name="folio" value="<?php echo $_GET['folio']; ?>">
          <input type="hidden" name="exp_alum_<?php echo $i; ?>" value="<?php echo ($row['exp_alum']); ?>">

          <tr>
            <th scope="row" colspan="1"><?php echo $i; ?></th>
            <td colspan="1"><?php echo ($row['exp_alum']); ?></td>
            <td colspan="6"><?php echo mb_convert_encoding($row['a_nom'] . ' ' . $row['a_pat'] . ' ' . $row['a_mat'], "UTF-8", "ISO-8859-1"); ?></td>
            <td colspan="3">
              <!-- Get the ratings and display them in an input -->
              <?php if (is_null($row['calif'])) { ?>
                <input class="grade-input" id="asignacalif" style="text-align: center;" type="text" name="calif_<?php echo $i; ?>" value="" class="form-control">

              <?php } else { ?>
                <input class="grade-input" id="asignacalif" style="text-align: center;" type="text" name="calif_<?php echo $i; ?>" value="<?php echo $row['calif']; ?>" class="form-control">
                <?php
                #display thison print page
                echo '<p style="display:none;" id="califimpresa" class="text-center">' . $row['calif'] . '</p>';
                ?>
              <?php } ?>
            </td>
          </tr>
        <?php
          $i++;
        }
        ?>
      </tbody>

      <tfoot>
        <!-- button to return -->
        <tr class="btns-actas">
          <td style="text-align: left" colspan="12">
            <a href="indexActas.php" class="btn btn-primary" style="color: #fff"><i class="fa-solid fa-arrow-left"></i> Regresar</a>
            <!-- Show save button, enabled or disabled as appropriate -->
            <?php
            if ($promedio > 0) {
              echo '<button type="submit" id="guardarcalificaciones"  name="guardar" value="guardar" class="btn btn-success" disabled><i class="fa-solid fa-check"></i> Guardar</button>';
            } else {
              echo '<button type="submit" name="guardar" value="guardar" id="guardarcalificaciones" class="btn btn-success" style="color: #fff" data-folio="' . $_GET['folio'] . '" data-pa_nombre="' . $_GET['pa_nombre'] . '" data-asig="' . $_GET['asig'] . '" data-gpo="' . $_GET['grupo'] . '"><i class="fa-solid fa-check"></i> Guardar</button>
                                                                        ';
            }
            ?>
            <?php
            #if the average is >0 add a button to update grades
            if ($promedio > 0) {
              echo '<button type="submit" name="actualizar" value="actualizar" id="actualizarCalificaciones" class="btn btn-warning" style="color: #fff" data-folio="' . $_GET['folio'] . '" data-pa_nombre="' . $_GET['pa_nombre'] . '" data-asig="' . $_GET['asig'] . '" data-gpo="' . $_GET['grupo'] . '"><i class="fa-solid fa-pen"></i> Actualizar</button>';
            }
            ?>
            <!-- button to print -->

            <button onclick="window.print()" id="imprimir" class="btn btn-danger" style="color: #fff" type="button"><i class="fa-solid fa-file-pdf"></i>Imprimir</button>

            <?php
            #if rating is = null add button to update ratings
            if (isset($calif)) {
              if (in_array(null, $calif)) {
                #y ocultar boton de imprimir
                echo '<script>document.getElementById("imprimir").style.display = "none";</script>';
                #ocultar boton de actualizar
                echo '<script>document.getElementById("actualizarCalificaciones").style.display = "none";</script>';
              }
            }
            ?>
          </td>
        </tr>
      </tfoot>
    </table>
  </div>
</form>

indexActas.php

发送数据并成功保存!,但它是通过两个按钮发送的,我只希望使用

save button
执行此操作,对于
update button
修改 $query,如开头所述

include_once './model/loginModel.php';
$model = new loginModel();
$conn = $model->connect();
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
  var_dump($_POST);
  foreach ($_POST as $key => $value) {
    if (strpos($key, 'calif_') === 0) {
      $num_registro = substr($key, strlen('calif_'));
      // update the rating in the database
      $query = "UPDATE dgirew.dbo.actas_w
                SET calif = :calif,
                    exp_prof_calif = :exp_prof_calif,
                    fecha_altacalif = :fecha_altacalif,
                    estatus = '1'
                WHERE exp_alum = :exp_alum
                AND asig = :asig
                AND gpo = :gpo
                AND folio = :folio";
      $stmt = $conn->prepare($query);
      $stmt->bindParam(':calif', $value);
      $stmt->bindParam(':exp_alum', $_POST['exp_alum_' . $num_registro]);
      $stmt->bindParam(':asig', $_POST['asig']);
      $stmt->bindParam(':gpo', $_POST['gpo']);
      $stmt->bindParam(':folio', $_POST['folio']);
      $stmt->bindParam(':exp_prof_calif', $_SESSION['exp_prof_titular']);
      $fecha = date('Y-m-d H:i:s') . '.000';
      $stmt->bindParam(':fecha_altacalif', $fecha);

      if (!$stmt->execute()) {
        //show error if the data could not be inserted correctly
        var_dump($stmt->errorInfo());
      }
    }
  }

  // Gets the number of records affected by the query
  $rowCount = $stmt->rowCount();
  if ($rowCount > 0) {
    echo '<div class="alert alert-success" role="alert">';
    echo '<button type="button" class="close" data-dismiss="alert" aria-label="Close">';
    echo '<span aria-hidden="true">&times;</span></button>';
    echo '<h4>¡Éxito!</h4>';
    echo '<p>Group<strong>' . $_POST['gpo'] . '</strong> ratings have been saved.</p>';
    echo '<hr>';
    echo '<p class="mb-0"><strong>NOTA:</strong> You only have<strong> 1 </strong> chance to correct the grades for this group</p>';
    echo '</div>';
  }
}

我试过的

如果使用 PHP 的 isset() 函数单击“保存”或“更新”按钮,我认为它可以工作。如果按下“保存”按钮,将执行保存数据的代码,而如果按下“刷新”按钮,将执行更新数据的代码。但我不知道如何实现它

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
  if(isset($_POST['guardar'])){
    // Code to save the data
  } else if(isset($_POST['actualizar'])){
    // Code to update the data
  }
}

javascript php forms post pdo
© www.soinside.com 2019 - 2024. All rights reserved.