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

     <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,但不知道为什么它不起作用。


繁花不似锦
浏览 76回答 1
1回答

FFIVE

您的按钮没有值,但隐藏的输入有。所有隐藏输入的值都将被提交,但由于名称不只以[]一个结尾,因此 PHP 不会丢弃该值。摆脱隐藏的输入将值存储在按钮上只有点击提交按钮才会成功并提交其数据。<td>  <button     class="btn btn-primary"    name="giden"    value="<?= htmlspecialchars($person->id) ?>"/>  >    Güncelle  </button></td>您还应该对生成的 HTML使用验证器,因为存在许多错误。元素tbody不能进入tr元素内部(无论如何它都没有意义,重点tbody是将一组元素分组tr!)。input元素不能是tr或tbody元素的子元素。如果它们出现在表格内,则必须位于该表格的单元格内。它们是否隐藏并不重要。
打开App,查看更多内容
随时随地看视频慕课网APP