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>";
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度