为了账号安全,请及时绑定邮箱和手机立即绑定

如何从 php 中的每个按钮获取值?

如何从 php 中的每个按钮获取值?

PHP
繁花不似锦 2023-10-01 16:02:11
     <form action="posts-ayarlar.php" method="POST" id="demo-form2" data-parsley-validate class="form-horizontal form-label-left">                    <table class="table table-striped table-bordered" >                <tr>                <thead>                    <th scope="col">ID</th>                    <th scope="col">BLOG TITLE</th>                                                        <th scope="col">EDİT</th>                </thead>                                </tr>         <?php                foreach($personellist as $person){                      $xasr=$person->id;             ?>                       <tr>                  <tbody>                     <td><?= $person->id ?> </td>                     <td><?= $person->title ?></td>                     <td><button  type="submit" name="post_ayar_guncelle_<?php echo $xasr ?>" class="btn btn-primary">Güncelle</button> </td>                     <input type="hidden" name="giden" value="<?= $person->id ?>"/>                  </tbody>                        </tr>                         <?php } ?>      </table>   </form>在流程页面,您可以在左上角看到8,它是来自主编辑表页面的值。<?php include 'connectionconfig.php';  $gelen=$_POST["giden"]; $sorgu=$db->prepare('SELECT * FROM posts where id:id'); $sorgu->execute(array('id'=>$gelen)); $personellist=$sorgu-> fetchAll(PDO::FETCH_OBJ);?>          //example edit section                 <div class="form-group">                    <label class="control-label col-md-3 col-sm-3 col-xs-12" for="first-name">Blog Title <span class="required">*</span>                    </label>                    <div class="col-md-6 col-sm-6 col-xs-12">                      <input type="text" name="title" id="first-name" required="required" class="form-control col-md-7 col-xs-12" value="<?php echo $personellist['title']; ?>">                    </div>                  </div>这个想法是, 当我单击这些按钮之一时,我想使用该按钮 id 转到 process.php,然后我可以回调 id 所在的数据,因此网页将显示具有按钮 id 的编辑博客页面。但是当我单击其中的任何按钮时,由于 foreach 循环,值返回 8。顺便一提; 我已经使用过ajax,但不知道为什么它不起作用。
查看完整描述

1 回答

?
FFIVE

TA贡献1797条经验 获得超6个赞

您的按钮没有值,但隐藏的输入有。

所有隐藏输入的值都将被提交,但由于名称不只以[]一个结尾,因此 PHP 不会丢弃该值。

  • 摆脱隐藏的输入

  • 将值存储在按钮上

只有点击提交按钮才会成功并提交其数据。

<td>

  <button 

    class="btn btn-primary"

    name="giden"

    value="<?= htmlspecialchars($person->id) ?>"/>

  >

    Güncelle

  </button>

</td>

您还应该对生成的 HTML使用验证器,因为存在许多错误。

元素tbody不能进入tr元素内部(无论如何它都没有意义,重点tbody是将一组元素分组tr!)。

input元素不能是trtbody元素的子元素。如果它们出现在表格内,则必须位于该表格的单元格内。它们是否隐藏并不重要。


查看完整回答
反对 回复 2023-10-01
  • 1 回答
  • 0 关注
  • 38 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信