1

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



}
3
  • Don't you need a while loop and put foreach between it? Commented Apr 7, 2016 at 22:59
  • 3
    If your foreach loop is creating multiple JS functions which are all named changeText(), then you are redefining the same function over and over. You can only define the function once. Commented Apr 7, 2016 at 23:13
  • Ahh I see, Ill work on it and post an update when i get it. Commented Apr 7, 2016 at 23:23

1 Answer 1

1

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

}
Sign up to request clarification or add additional context in comments.

5 Comments

I edited my post down to the bare bones. I'm confused as to how I'd implement your solution with my code.
what is in this echo(json_encode($row["announcementID"]))?
Thats supposed to echo the id stored in the DB of the row the button is attached to.
ok then as above, just to be clear, the code in the script tag gets added once....it will handle the onclick event on the elements. i'm going to tidy a small bit as well
Perfect! Worked great, just what I was looking for.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.