Change hidden field value on Submit click
<p>I have a form with multiple items and an id attributed to each of these items, on Submit I want to be able to grab the id of the item that was clicked - I have tried using js, something like:</p>

<pre><code>&lt;form method="post" action="add_item_cart.php"&gt;
    &lt;input type="hidden" id="item_id" name="item_id"&gt;
    &lt;input name="submit_item" id="btn_sub" onclick="document.getElementById('item_id').value = &lt;?php echo '3'; ?&gt;" type="submit" value="Add"&gt;
&lt;/form&gt;
</code></pre>

<p>I want to be able to grab this value: <code>$item_id = $_POST["item_id"];</code> on <strong>add_item_cart.php</strong>, but this doesn't seem to be working. </p>

<p>Is this a problem with my js syntax or is my logic not plausible to solve this problem? Is it submitting before changing the value?</p>

<p>EDIT:
Let's see if I can explain myself better, I want to assign that hidden value dynamically, imagine that my form has 3 submit buttons (one for each item displayed). Depending on the one that is clicked, I want to pass the item's id to my hidden field, so if I click button1 - $_POST["item_id"]=1, button2 - $_POST["item_id"]=2... etc</p>

<p>Here is my actual form (non simplified example)</p>

<pre><code>&lt;form method="post" action="add_item_cart.php"&gt;
                &lt;table style="width:600px"&gt;
                    &lt;tr&gt;
                        &lt;?php foreach ($items as $item): ?&gt;
                            &lt;td&gt;
                        &lt;center&gt;
                            &lt;span style="font-size:20px"&gt;&lt;?php echo $item["item_name"] ?&gt;&lt;/span&gt;&lt;br/&gt;
                            €&lt;?php echo $item["price"] ?&gt;&lt;br/&gt;
                            Quantidade: &lt;input type="text" value="1" style="width:30px"&gt;&lt;br/&gt;
                            &lt;input type="hidden" id="item_id" name="item_id"&gt;
                            &lt;input name="submit_item" id="btn_sub" onclick="document.getElementById('item_id').value = &lt;?php echo $item["id"]; ?&gt;" type="submit" value="Adicionar"&gt;
                        &lt;/center&gt;
                        &lt;/td&gt;
                    &lt;?php endforeach; ?&gt;
                    &lt;/tr&gt;
                &lt;/table&gt; 
            &lt;/form&gt;
</code></pre>
