使用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>";



}

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>";

}