1

My problem is that I don't know how to make a function that has the same id as my $loop['id'] that I created with foreach loop.

foreach ($getongoing as $loop) {
 echo "<tr>";
 echo "<td>" . $loop['id'] . "</td>";
 echo "<td>" . $loop['player1'] . "</td>";
 echo "<td>" . "$" . $loop['bet1'] . "</td>";
 echo "<td>" . '<html><img src="images/'.$loop['site1'].'.png" width="25" height="25"/></html>' . "</td>";

Within my loops I create a button, which has the $loop['id']

echo '<td><button type="button" class="btn btn-link" onClick="'.$loop['id'].'()" id="test">Join game</button></td></tr>';

I would like to make a function/script which comes up with a altert box, asking if I really want to join $loop['id'] with the bet of $loop['bet1']

This is the script that I want to appear when clicking on the specfic join button

<script>
function myFunction() {
    var x;
    if (confirm("Press a button!") == true) {
        x = "You pressed OK!";
    } else {
        x = "You pressed Cancel!";
    }
    document.getElementById("demo").innerHTML = x;
}
</script>

1 Answer 1

2

Instead of creating and calling function using the value of $loop['id'], you can simply pass the value as parameter, see example below:

echo '<td><button type="button" class="btn btn-link" onClick="myFunction(' . $loop['id'] . ');" id="test">Join game</button></td></tr>';

<script>
function myFunction(id) {
    console.log(id);
    var x;
    if (confirm("Press a button!") == true) {
            x = "You pressed OK!";
    } else {
            x = "You pressed Cancel!";
    }
    document.getElementById("demo").innerHTML = x;
}
</script>
4
  • But this comes up being all other buttons linking to the first one? May 7, 2016 at 12:39
  • No, they have call the same function myfunction but you can identify with the help of parameter id .. check browser console
    – kamal pal
    May 7, 2016 at 12:41
  • Can we make it, so if i press ok, then it will go to joingame.php? May 7, 2016 at 16:07
  • if (confirm("Press a button!")) {window.location = 'joingame.php';}
    – kamal pal
    May 7, 2016 at 16:25

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.