dounanyin3179 2016-04-07 22:53
浏览 12
已采纳

使用PHP和JS更新表单值

I have a foreach statement that returns some text from the current DB and I'm attempting to populate a another field based on that that rows ID. I made a button with an onclick function but every time I click the echoed button it fills in the same ID every time. Any recommendations so that the echoed click returns the ID of that echoed row?

$a = 0;

                    foreach ($announcement as $row) { //Displays title, startDate, endDate from announcement table from database 

                    $x[$a] = $row["announcementID"];
                    ?>
                    <script type="text/javascript">
                        function changeText(value) {
                             document.getElementById('title').value = <?php echo(json_encode($row["announcementID"])); ?> ;
                        }
                    </script>
                    <?php
                    //echo "<h2 style=width:auto;padding:8px;margin-top:-30px;font-size:18px;><a style=text-decoration:none;color:#c4572f; >".$row["title"]."</a></h2><br>";
                    //echo "<p style=padding-top:10px;>".$row["content"]."</p><br>";
                    //echo "<p style=font-size:10px;>Posted: ".$row["startDate"]."</p><br>";
                    echo "<input type=button onclick=changeText".$x[$a]."() value=Edit>";
                    echo "<p style=font-size:10px;>Posted: ".$x[$a]."</p> <br />";
                    //echo "<h5 style=line-height:2px;margin-top:-15px;><p>_____________________________________</p></h5><br>";



}
  • 写回答

1条回答 默认 最新

  • download2565 2016-04-07 23:26
    关注

    you dont need to define a new function to deal with this each time, you can pass the element into the function in your onclick call and get the id.

    in your js script

     <script>
      function changeText(elem) {
          document.getElementById('title').value = elem.id;
      }
     </script>
    

    your php

    foreach ($announcement as $row) { //Displays title, startDate, endDate from announcement table from database 
    
        $x[$a] = $row["announcementID"];
        //echo "<h2 style=width:auto;padding:8px;margin-top:-30px;font-size:18px;><a style=text-decoration:none;color:#c4572f; >".$row["title"]."</a></h2><br>";
        //echo "<p style=padding-top:10px;>".$row["content"]."</p><br>";
        //echo "<p style=font-size:10px;>Posted: ".$row["startDate"]."</p><br>";
        echo '<input id="'.$x[$a].'" type=button onclick="changeText(this)" value="Edit">';
        echo "<p style=font-size:10px;>Posted: ".$x[$a]."</p> <br />";
        //echo "<h5 style=line-height:2px;margin-top:-15px;><p>_____________________________________</p></h5><br>";
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用