javascript - 单击提交按钮后显示隐藏的表格

标签 javascript php jquery html mysql

我有一个表单,我可以在其中选择项目名称并输入工作表名称。 单击提交按钮后,它会以表格格式显示最后插入的记录,但单击提交后,它会显示表格,但一旦重新加载页面,它就会隐藏表格。然后单击提交按钮,它会显示表格。我该如何解决这个问题?

<button type="submit" name="submit" id="button" class="btn-success btn">Submit</button>

<script>
  $(document).ready(function() {
    $("#button").click(function() {
      $("#table").show();
    });
  });
</script>

<?php 
  include('controller.php');
  $s4 = "SELECT * FROM col_heading ORDER BY coln_id DESC LIMIT 1";
  $res = mysqli_query($bd, $s4);
  while ($row = mysqli_fetch_array($res)) {
?>  

<table class="table table-bordered" id="table" style="width:50%;">
  <thead>
    <tr>
      <th>Sr.no</th>
      <th>Column Name</th>
    </tr>
  </thead>
  <tbody>

    <tr class="odd">
      <td>1</td>
      <td id="h1:<?php echo $row['coln_id']; ?>" contenteditable="true">
        <?php  echo $row['h1']; ?>
      </td>       
    </tr>
    <tr>
      <td>2</td>
      <td id="h2:<?php echo $row['coln_id']; ?>" contenteditable="true">
        <?php echo $row['h2']; ?>
      </td>
      </tr>
    <tr class="odd">
      <td>3</td>
      <td id="h3:<?php echo $row['coln_id']; ?>" contenteditable="true">
        <?php echo $row['h3']; ?>
      </td>
    </tr>
  </tbody>
</table>
<?php 
} 
?>

最佳答案

尝试在提交中使用e.preventDefault();

$(document).ready(function() {
  $("#button").click(function(e) {
    e.preventDefault();
    $("#table").show();
  });
});

如果您想在提交时执行某些操作,那么最好在 click 函数中使用 ajax 调用并完成它。

将您的 php 放入新文件中

更新.php

<?php 
  include('controller.php');
  $s4 = "SELECT * FROM col_heading ORDER BY coln_id DESC LIMIT 1";
  $res = mysqli_query($bd, $s4);
  while ($row = mysqli_fetch_array($res)) {
?>

从 click 函数内部对 update.php 进行 ajax 调用

 $(document).ready(function() {
      $("#button").click(function(e) {
        e.preventDefault();
        $("#table").show();
        $.ajax({
           url: 'update.php',
           data: '<pass any data here>',
           success: function(response){
             // do something with the resposne
           }
        });
      });
    });

完整代码

<button type="submit" name="submit" id="button" class="btn-success btn">Submit</button>

<script>
  $(document).ready(function() {
      $("#button").click(function(e) {
        e.preventDefault();
        $("#table").show();
        $.ajax({
           url: 'update.php',
           data: '<pass any data here>',
           success: function(response){
             // do something with the resposne
           }
        });
      });
    });
</script>


<table class="table table-bordered" id="table" style="width:50%;">
  <thead>
    <tr>
      <th>Sr.no</th>
      <th>Column Name</th>
    </tr>
  </thead>
  <tbody>

    <tr class="odd">
      <td>1</td>
      <td id="h1:<?php echo $row['coln_id']; ?>" contenteditable="true">
        <?php  echo $row['h1']; ?>
      </td>       
    </tr>
    <tr>
      <td>2</td>
      <td id="h2:<?php echo $row['coln_id']; ?>" contenteditable="true">
        <?php echo $row['h2']; ?>
      </td>
      </tr>
    <tr class="odd">
      <td>3</td>
      <td id="h3:<?php echo $row['coln_id']; ?>" contenteditable="true">
        <?php echo $row['h3']; ?>
      </td>
    </tr>
  </tbody>
</table>

关于javascript - 单击提交按钮后显示隐藏的表格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44130783/

相关文章:

php - 使用 'mail' 或 'sendmail' 通过 CodeIgniter 发送电子邮件

javascript - 输入 key 表单提交因 HTML5 日期输入而失败

javascript - 需要在 RTL 模式下对 IE6 和 IE7 中无效的 getBoundingClientRect() 进行解决

javascript - 在 CSS 中设置选取框

javascript - 使用 grunt-file-append 将文本附加到多个文件

php - 如何使用 GROUP BY 和 ORDER BY 关键字从表中获取数据?

php - 无法使用 SwiftMailer 发送电子邮件

javascript - 对象 find() 不能用于数组

javascript - 重新加载到页面顶部

javascript - 在原件之后插入克隆件