1

I have a 9x9 field. When I press any button, I want it to display the coordinates, but for some reason it doesn't.

Does someone know where it goes wrong? Am I doing something which is impossible?

Current code:

<?php
echo "<iframe name='frame'></iframe>";
$aww_ar = array();
// SELECT PLAYING FIELD ValLUES
$th = 10;
$tw = 10;
// Amount of numbers
$amount = 5;
// Create 2d array
for ($y = 0;$y < $th;$y++) {
    for ($x = 0;$x < $tw;$x++) {
        $randomise = rand(1, $amount);
        $aww_ar[$x][$y] = $randomise;
    }
}

// create 2d table array
?>        
    <form method="post" target="frame" action="">
        <table>
        <?php
foreach ($aww_ar as $x => & $value) {
    echo "<tr>";
    foreach ($aww_ar[0] as $y => $v) {
        echo "<td><input type='submit' name='" . $x . ',' . $y . "' value='" . $aww_ar[$x][$y] . "'></td>";
    }
    echo "</tr>";
}
?>
        </table>
    </form>
    <?php
if (isset($_POST[$x . ',' . $y])) {
    echo $x . ',' . $y;
}
Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Kaede
  • 198
  • 1
  • 11
  • Names don't usually have commas in them. Try an underscore instead. – aynber Dec 12 '19 at 14:06
  • 1
    `if(isset($_POST[$x.','.$y])){` - `$x` and `$y` both have the value `9` at this point in your script, so this part would only work if you clicked the very last button you created, because only that one has the name `9,9`. – 04FS Dec 12 '19 at 14:07
  • @aynber i've just tried but doesn't seem to work. – Kaede Dec 12 '19 at 14:09
  • @04FS valid point, sadly even when pressed the 9,9 button it still doesn't work. Also when pressed the button, it updates the values (atleast that is what i think it does) – Kaede Dec 12 '19 at 14:12
  • 1
    I would use the same name on all buttons, and put all the info you need to submit into the value - for example by sticking x, y and the random value into an array, and then encoding that as JSON. And the receiving end, you decode it again, and you have access to your three separate values again. – 04FS Dec 12 '19 at 14:16
  • 1
    Maybe it's unrelated, but I'd remove the `action=""` altogether. (see https://stackoverflow.com/questions/1131781/is-it-a-good-practice-to-use-an-empty-url-for-a-html-forms-action-attribute-a) – devb Dec 12 '19 at 14:22
  • @04FS Im not really familiar with JSON but ill give it a shot thanks for the help! – Kaede Dec 12 '19 at 14:25
  • @devb every bit helps, thanks :) – Kaede Dec 12 '19 at 14:25
  • 2
    As @04FS already commented, you're using the $x and $y values which are set after finishing the loop, so you're only checking 1 button, the last one. Loop over the possible values for $x and $y to find the value that was clicked. – devb Dec 12 '19 at 14:26
  • 1
    you should simplify the `name=` attribute to: `name='$x,$y'`. Now when you click the very last button you'll get the 9,9 echoed. – jibsteroos Dec 12 '19 at 14:29
  • @jibsteroos I changed the name to name='$x,$y' , But i still cant get it echoed? – Kaede Dec 12 '19 at 14:40

1 Answers1

1

Im not sure what the purpose of the iframe is. If you rename your fields to use underscore instead of comma, then the following code should print the value of the clicked button.

<?php

if($_SERVER['REQUEST_METHOD'] == 'POST'){
    $value = $_POST['field'];
    echo $value;
}
    $aww_ar = array();

    // SELECT PLAYING FIELD ValLUES
    $th = 10;
    $tw = 10;

    // Amount of numbers
    $amount = 5;

    // Create 2d array
    for($y = 0; $y < $th; $y++){
        for($x = 0; $x < $tw; $x++){
            $randomise = rand(1,$amount);
            $aww_ar[$x][$y] = $randomise ;
        }
    }

    // create 2d table array
?>        
    <form method="post">
        <table>
        <?php
        foreach ($aww_ar as $x=>&$value) {
            echo"<tr>";
            foreach ($aww_ar[0] as $y=>$v) {
                echo "<td><input type='submit' name='field' value='".$aww_ar[$x][$y]."'></td>";
            }
            echo"</tr>";
        }
        ?>
        </table>
    </form>

Note, the code assumes you will only have one variable in your post array.

Syed Hussim
  • 720
  • 1
  • 4
  • 16
  • The reason why im using an iframe is because i dont want all the numbers that have been automatically generated to reload when pressed 1 button. – Kaede Dec 12 '19 at 14:42
  • Are you saying that on post action, you don't want to show the grid but just the value selected? – Syed Hussim Dec 12 '19 at 14:44
  • When you press on any button in the grid, i want the echo to be echoed out BUT without iframe the field would reload. – Kaede Dec 12 '19 at 14:46
  • 1
    I've refactored your code such that on submit it does not show the grid but the value selected. – Syed Hussim Dec 12 '19 at 14:53
  • Okay, that seems to be working but ive only got 1 problem now, The value showing is good except, the grid goes away. I wanted the grid to stay. – Kaede Dec 12 '19 at 15:00
  • 1
    Edited the code, the grid should stay now and show the selected value. – Syed Hussim Dec 12 '19 at 15:16
  • Almost perfect, the only but surely last problem is that when you press a button all the buttons are getting new values, the iframe i added prevented that from happening. – Kaede Dec 12 '19 at 15:20