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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里