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

4

1 回答 1

0

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

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

  • 摆脱隐藏的输入
  • 将值存储在按钮上

Only the clicked submit button will be successful and submit its data.

<td>
  <button 
    class="btn btn-primary"
    name="giden"
    value="<?= htmlspecialchars($person->id) ?>"/>
  >
    Güncelle
  </button>
</td>

You should also use a validator on your generated HTML as you have a number of errors.

A tbody element can't go inside a tr element (and it wouldn't make sense anyway, the point of tbody is to group a set of tr elements!).

input elements can't be children of tr or tbody elements. If they appear inside a table, then must be inside a cell of that table. It doesn't matter if they are hidden or not.

于 2020-08-12T19:02:09.753 回答