douwu0882 2015-04-26 08:29
浏览 61
已采纳

使用php创建并获取动态变量

i know this question probably been asked and answered, and i also not sure what proper question i should ask, so pardon me :D

what im trying to do is:

  • i got a table, i get the value from mysql and paste it into the table through a LOOP
  • for each row i give it a button (edit button)
  • i give each button the id of value through a variable $id (eg. 1 2 3 4 )

my question is how can i get the specify button that i press by $_POST? i tried every way i could think of but it not work ( in my code below i warp the id with another variable=asdf so that it static but i can only get the last value of the id)

$viewvalue = mysqli_fetch_all ($result, MYSQLI_ASSOC);
<?php if (count($viewvalue) > 0): ?>
            <table id="t01">
              <thead>
                <tr>
                  <th><?php echo implode('</th><th>', array_keys(current($viewvalue))); ?></th>
                  <th>Edit</th>
                  <th>Delete</th>
                </tr>
              </thead>
              <tbody>
            <?php foreach ($viewvalue as $row): array_map('htmlentities', $row); ?>
                <tr>
                  <td><?php echo implode('</td><td>', $row);?></td>
                  <td><input type="submit" class="edit" name="<?php $a=$row['id']; $$a='asdf'; echo $$a; ?>" value="Edit" /></td>
                </tr>
            <?php endforeach; ?>
              </tbody>
            </table>
            <?php endif; ?>

            <?php 
                if(isset($_POST['asdf'])){
                    echo $a;
                }
            ?>

i know theres a way to use jquery and ajax but sadly i cant get it to work, is it a way to do it with php alone?

  • 写回答

1条回答 默认 最新

  • doudou0111 2015-04-26 08:36
    关注

    I'll offer to replace this row:

    <td><input type="submit" class="edit" name="<?php $a=$row['id']; $$a='asdf'; echo $$a; ?>" value="Edit" /></td>
    

    by:

    <td><button class="edit" name="act" value="<?php echo  $row['id'];?>">Edit</button></td>
    

    after that you'll simply check the value of the $_POST['act']

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 学不会递归,理解不了汉诺塔参数变化
  • ¥30 软件自定义无线电该怎样使用
  • ¥15 R语言mediation包做中介分析,直接效应和间接效应都很小,为什么?
  • ¥15 Jenkins+k8s部署slave节点offline
  • ¥15 如何实现从tello无人机上获取实时传输的视频流,然后将获取的视频通过yolov5进行检测
  • ¥15 WPF使用Canvas绘制矢量图问题
  • ¥15 用三极管设计一个单管共射放大电路
  • ¥15 孟德尔随机化r语言运行问题
  • ¥15 pyinstaller编译的时候出现No module named 'imp'
  • ¥15 nirs_kit中打码怎么看(打码文件是csv格式)