传递数据时隐藏的表单字段值不正确

时间:2015-07-03 16:19:23

标签: php mysql forms mysqli

我有一个使用MySQL的PHP​​表单并将数据传递到另一个页面,在第一页中,一个mysql查询选择一些记录然后将其打印在页面上,其中一个记录显然是ID,也是打印的在隐藏字段的值中,以便在下一页中使用它

<input type="hidden" name="cat_id" value="<?php echo $row['id']; ?>" />

并且结果是正确的,我在检查页面输出代码时有10个不同的ID

<input type="hidden" name="cat_id" value="1" />
<input type="hidden" name="cat_id" value="2" />
<input type="hidden" name="cat_id" value="3" />
...

当我提交表单时出现问题(每个数据库结果都有1个表单,所以如果我有10个条目,我还有10个隐藏字段,其中包含10个不同的值),即使我提交第二个第10 形式,在下一页中,$ _POST ['cat_id']的值始终为1,根据$ _POST的一般打印

Array
(

[cat_id] => 1

)

有人明白为什么吗?

编辑:添加完整表单输出脚本以进行说明

 <div class="container">
  <div class="row">

  <?php
    if($c == 0){
  ?>
      <h3>Il catalogo è momentaneamente vuoto.</h3>
  <?php
    } else {

      while($row = $result->fetch_assoc()) {
        ?>

  <div class="col-md-3 col-sm-4 col-xs-12">
      <div class="thumbnail">
          <div class="caption-img" style="background: url('imgs/thumb.jpg');"></div>
          <a href="#" data-toggle="modal" data-target="#myModal">
          <div class="caption-link">
            <h3><?php echo $row['titolo']; ?></h3>
            <span class="glyphicon glyphicon-book"></span>
          </div>
          </a>
      </div>
    </div>

    <!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title" id="myModalLabel">Password</h4>
      </div>
      <div class="modal-body">
        <p>Perfavore inserisci la password necessaria per visualizzare questo elemento</p>
        <form action="show.php" method="post">
          <input type="password" class="form-control" name="psw" />
          <input type="hidden" name="cat_id" value="<?php echo $row['id']; ?>" />
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Chiudi</button>
        <button type="submit" class="btn btn-primary" name="view">Prosegui</button>
        </form>
      </div>
    </div>
  </div>
</div>    

        <?php
      }

    }
  ?>

  </div>
</div>

1 个答案:

答案 0 :(得分:0)

您的表单已损坏,在表单关闭之前不能关闭其父元素:

<div class="modal-body">
        <p>Perfavore inserisci la password necessaria per visualizzare questo elemento</p>
        <form action="show.php" method="post">
          <input type="password" class="form-control" name="psw" />
          <input type="hidden" name="cat_id" value="<?php echo $row['id']; ?>" />
      </div> <!-- HERE IS YOUR ISSUE -->
      <div class="modal-footer"> <!-- HERE IS YOUR ISSUE -->
        <button type="button" class="btn btn-default" data-dismiss="modal">Chiudi</button>
        <button type="submit" class="btn btn-primary" name="view">Prosegui</button>
        </form>
      </div>

看起来应该更像这样:

<form action="show.php" method="post">
    <div class="modal-body">
        <p>Perfavore inserisci la password necessaria per visualizzare questo elemento</p>
        <input type="password" class="form-control" name="psw" />
        <input type="hidden" name="cat_id" value="<?php echo $row['id']; ?>" />
    </div>
    <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Chiudi</button>
        <button type="submit" class="btn btn-primary" name="view">Prosegui</button>
    </div>
</form>