【问题标题】:Showing hidden table after clicking submit button点击提交按钮后显示隐藏表格
【发布时间】:2017-05-23 09:23:18
【问题描述】:

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

<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 
} 
?>

【问题讨论】:

  • 更改按钮类型="button" 以避免重新加载页面
  • 但在提交时将数据提交到数据库我不能这样做
  • 利用ajax提交表单,提交成功后显示表格
  • 2 个选项。通过 ajax 提交(并停止默认浏览器发布),或通过 php 隐藏/显示表格
  • 您的问题有些不清楚。你的意思是重新加载后表格应该保持可见?如果是,是否还有其他 JS 函数将其隐藏起来?

标签: javascript php jquery html mysql


【解决方案1】:

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

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

如果您想在提交时做某事,那么最好在 click 函数中使用 ajax 调用并完成它。

将你的 php 文件放入一个新文件中

update.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)) {
?>

从内部点击函数对 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>

【讨论】:

  • 由于问题中没有 ajax 的提示,这一切可能实现的就是停止表单做任何事情
  • 我的表在使用 e.preventDefault(); 后被隐藏了
  • 确保你有参数e来点击函数function(e)
  • 我有 2js + 1ajax 函数第一个用于依赖选择框。第二个显示隐藏的表格,第三个编辑我上面给出的表格。
  • @Nadir Laskar 是 onclick 事件吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-06-14
  • 1970-01-01
  • 2017-12-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多