从Bootstrap Modal运行查询

时间:2015-11-05 10:20:54

标签: php mysql twitter-bootstrap bootstrap-modal

在我的项目中,我在表中有一个贪睡按钮,表格正在填充数据库中的值。我给了一个href按钮,它将打开一个bootstrap模态对话框。

在模态中,我调用了特定id的数量。

模态代码如下:

<?php
    require_once '../invoice/config.php';
    $reserver = $_GET["id"]; 
    echo $reserver;
    $result = mysqli_query($con,"SELECT * FROM table_name where id = '$reserver'");
    $row = mysqli_fetch_array($result);
?>
<form id="modal-form" data-remote="true" method="post" action="saveSnoozeReminder.php?id='<?php echo $reserver ?>'" >
    <div class="modal-body">
        <p>SELECT DATE FOR REMINDER </p>
        <label>date</label>
        <input type="date" name="snoozeReminderDate" />
        <label>amount</label>
        <input type="text" name="amt"  value="<?php echo $row['amount']; ?> " disabled >
    </div>
    <div class="modal-footer">
        <button type="submit" class="btn btn-default" data-dismiss="modal" value="">Snooze</button>
        <button type="button" class="btn btn-default" data-dismiss="modal" onClick="window.location.reload()">Close</button>
    </div>
</form>

现在用户将选择日期,然后按下再响按钮,我已经编写了另一个PHP文件,将在表单提交时调用,并更新表格中该特定ID的提醒。

另一个文件名为saveSnoozeReminder.php,代码为:

<?php
    require_once '../invoice/config.php';
    $recordid = $_GET["id"];
    echo $recordid;
    $snoozedate = $_POST['snoozeReminderDate'];
    echo $snoozedate;
    $snoozeQuery = "UPDATE paymentremainder SET reminderdate='$snoozedate' WHERE id = $recordid";           
    if (mysqli_query($con, $snoozeQuery)) {
        $message = 'Reminder Snoozed Successfully';
        echo "<SCRIPT type='text/javascript'> 
        alert('$message');
        window.location.replace('index.php');
        </SCRIPT>";
    } else {
        return "";
    }
?>

同样运行此代码后,更新查询也不会被触发。有什么方法可以使用Ajax或其他东西。有人可以帮忙吗。

1 个答案:

答案 0 :(得分:1)

使用您当前的方法,<form>

中几乎没有错误
  1. 不要发布id以及表单操作网址action="saveSnoozeReminder.php?id='<?php echo $reserver ?>'",创建hidden输入并为其添加id值,并将其与表单一起发布
  2. 请勿disable输入使用readonly
  3. 表单HTML

    <form id="modal-form" data-remote="true" method="post" action="saveSnoozeReminder.php" >
    <input type="hidden" name="id" value="<?php echo $reserver ?>">
        <div class="modal-body">
            <p>SELECT DATE FOR REMINDER </p>
            <label>date</label>
            <input type="date" name="snoozeReminderDate" />
            <label>amount</label>
            <input type="text" name="amt" value="<?php echo $row['amount']; ?> " readonly >
        </div>
        //You don't need to close the modal `data-dismiss="modal"` when submitting the form, it will be auto closed
        <div class="modal-footer">
            <button type="submit" class="btn btn-default" value="submit">Snooze</button>
            <button type="button" class="btn btn-default" data-dismiss="modal" onClick="window.location.reload()">Close</button>
        </div>
    </form>
    

    在PHP中,将$_GET替换为$_POST并使用isset函数

    <?php
        require_once '../invoice/config.php';
        if(isset($_POST['id'])) { //isset function
            $recordid = $_POST["id"]; //replace $_GET with $POST
            echo $recordid;
            $snoozedate = $_POST['snoozeReminderDate'];
            echo $snoozedate;
            $snoozeQuery = "UPDATE paymentremainder SET reminderdate='$snoozedate' WHERE id = '$recordid'";
            if (mysqli_query($con, $snoozeQuery)) {
                $message = 'Reminder Snoozed Successfully';
                    echo "<SCRIPT type='text/javascript'> 
                    alert('$message');
                    window.location.replace('index.php');
                    </SCRIPT>";
            } else {
                return "";
            }
        }
    ?>
    

    这将解决未触发更新查询的问题。

    使用Ajax解决方案

    您要求使用备用Ajax解决方案。

    1. Snooze按钮的类型从submit更改为button,因此不会在默认情况下提交表单,也无需在Ajax方法中使用e.preventdefaultreturn false < / LI>
    2. action="saveSnoozeReminder.php"
    3. 中移除<form>
    4. ids输入您希望通过Ajax和Snooze按钮传递值以将其与jQuery点击功能绑定
    5. 表单HTML

      <form id="modal-form" data-remote="true" method="post">
      <input type="hidden" name="id" id="reserverid" value="<?php echo $reserver ?>">
          <div class="modal-body">
              <p>SELECT DATE FOR REMINDER </p>
              <label>date</label>
              <input type="date" id="snoozeReminderDate" name="snoozeReminderDate" />
              <label>amount</label>
              <input type="text" name="amt" id="amt" value="<?php echo $row['amount']; ?> " readonly >
          </div>
          //You don't need to close the modal `data-dismiss="modal"` Snooze button, otherwise it will close the modal
          <div class="modal-footer">
              <button type="button" class="btn btn-default" value="submit" id="Snooze">Snooze</button>
              <button type="button" class="btn btn-default" data-dismiss="modal" onClick="window.location.reload()">Close</button>
          </div>
      //Ajax message here
      <div id="message"></div>
      </form>
      

      现在进行Ajax方法调用

      $(document).ready(function () {
          $("#Snooze").click(function () {
              var snoozeid = $('#reserverid').val(); //id of row
              var snoozeReminder = $('#snoozeReminderDate').val(); //date value
              var dataString = 'snoozeid=' + snoozeid + '&snoozeReminder=' + snoozeReminder;
              alert(dataString);
              $.ajax({
                  type: "POST",
                  url: "saveSnoozeReminder.php",
                  data: dataString,
                  cache: false,
                  success: function (data) {
                      $("#message").html(data);
                  }
              });
          });
      });
      
      表单HTML中的

      $("#message").show(data);将显示来自PHP saveSnoozeReminder.php

      的Ajax响应

      并且PHP saveSnoozeReminder.php将是

      <?php
          require_once '../invoice/config.php';
          if(isset($_POST['snoozeid'])) {
              $recordid = $_POST["snoozeid"];
              $snoozedate = $_POST['snoozeReminder'];
              $snoozeQuery = "UPDATE paymentremainder SET reminderdate='$snoozedate' WHERE id = '$recordid'";
              if (mysqli_query($con, $snoozeQuery)) {
                     echo "<strong>Success!</strong> Everything Is Good.";
              } else {
                     echo "<strong>Error!</strong> Something Went Wrong.";
              }
          }
      ?>