Stack Exchange
Identifying which dynamically generated button was clicked
I am dynamically creating 10 rows in a table with a button at the end of each row. These rows are all contained in a form. When I press a button $_POST returns array(0) { } and the contents gathered from php://input are 
string(239) "code=J7410&amount=0&code=abc123&amount=0&code=42139&amount=0&code=BBTCG&amount=0&code=13064&amount=0&code=D40717&amount=0&code=D11255&amount=0&code=D40617&amount=0&code=D68691&amount=0&code=11-600.50.04&amount=0&code=9789170378041&amount=0"
This is all the relevant code:
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">

<table>
    <tr>
        <td>ID</td>
        <td>Name</td>
        <td>VAT(%)</td>
        <td>Price exl VAT</td>
        <td>Amount</td>
    </tr>
<?php
foreach ($json as $row)
{
$code = $row['code'];
echo "<tr>";
?>
<td><input type="text" name="code" value="<?php echo $code?>" readonly title=""></td>
    <?php
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['vat'] . "</td>";
echo "<td>" . $row['price'] . "</td>";
echo "<td>" . "<input type='number' name='amount' value='0'>"  . "</td>";
     ?>
    <td><input type="submit"  name="<?php $row['code'] ?>"  value="Add To Cart"></td>
    <?php
echo "</tr>";
}
echo "</table>";
echo "</form>";

if ($_SERVER['REQUEST_METHOD'] === 'POST') {

var_dump($_POST);

$rest_json = file_get_contents("php://input");
var_dump($rest_json);
}

What I need here is some way to identify which button was clicked. Either through the ID or if there is a way to dynamically give the buttons unique identifiers that solve this issue.
The variable $json is read from a file similarly to $rest_json and works completely fine.
I started with PHP yesterday so any suggestions or opinions that can be of help are very welcome.

So what's stopping you from giving each button a unique id? Just increment an integer variable on each loop and add id="<? echo $number; ?>" or something like that?

I've tried that but it didn't resolve my issue. I assumed the problem lied in the code not knowing which button was being clicked since php://input recovers a string containing all data each button would send combined.

One way is to create an array of the buttons and use the code or something else unique as the index:
<input type="submit" name="submit[<?php $row['code'] ?>]" value="Add To Cart">

Now the input should look something like:
<input type="submit" name="submit[J7410]" value="Add To Cart">

Now there will be a $_POST['submit']['J7410'] if that button is clicked, so to get the code use:
$code = key($_POST['submit']);


I'll accept this because it answers my question. The problems persists though that var_dump($_POST); returns array(0) { }.

That is strange.  Does var_dump(filter_input_array(INPUT_POST)); show anything?

var_dump(filter_input_array(INPUT_POST)); returns null.

Has to be a server issue or http://stackoverflow.com/questions/15092581/get-post-and-request-not-being-populated

I think you are on point. I tried with a very small and simple test and still the array is 0. I'm going to test a different server software; I don't seem to have the same issue that link is having. Thanks for the help!
