Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9116759
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T04:45:03+00:00 2026-06-17T04:45:03+00:00

I have a form with multiple items and an id attributed to each of

  • 0

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:

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

I want to be able to grab this value: $item_id = $_POST["item_id"]; on add_item_cart.php, but this doesn’t seem to be working.

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?

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

Here is my actual form (non simplified example)

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

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T04:45:04+00:00Added an answer on June 17, 2026 at 4:45 am

    When your form is posted and you want to collect the item_id value on add_item_cart.php first you need to actually assign a value to id as in (Assuming item_id is a php variable). The id is just used for setting the css editing not a value…

    <input type="hidden" id="item_id" value="<?php echo $item_id; ?>" name="item_id">
    

    you cannot have id=” for the value because ‘value’ is value.

    Then you can get that value on your other page with:

    <?php
    if(isset($_POST['item_id'])){
    $item_id = $_POST['item_id'];
    }
    ?>
    

    If you want to edit the variable after post depending on which button you hit you can try.

    <input name="submit_item1" id="btn_sub" name="button1" type="submit" value="Add">
    <input name="submit_item2" id="btn_sub" name="button1" type="submit" value="Add">
    <input name="submit_item3" id="btn_sub" name="button1" type="submit" value="Add">
    

    Then on the top of your page you can do.

    <?php
    if(isset($_POST['submit_item1'])){
    $item_id = 1;
    }
    if(isset($_POST['submit_item2'])){
    $item_id = 2;
    }
    if(isset($_POST['submit_item3'])){
    $item_id = 3;
    }
    ?>
    

    If you are creating forms from an array of items you could do something like, (assuming you somehow have an id associated with those items; I would need to know more information about how you are making your item list. But it would generally do like this.

    <?php
    foreach($item_array as $item){
    ?>
    <form method="post" action="add_item_cart.php">
        <input type="hidden" id="item_id" name="item_id" value="<?php echo $item['id']; ?>">
        <input name="submit_item" id="btn_sub" type="submit" value="Add">
    </form>
    
    <?php
    }
    ?>
    

    Then on the top of your page you can just get $_POST[‘item_id’] and since that value is dynamically set you do not need any conditionals you can just get that value and run any query.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an HTML form that needs multiple submit buttons, like this: <input type=submit
I have inserted this form with multiple checkbox items which collects some values, What
I have form with multiple input type=radio with text next to them. I want
I have a form with multiple textboxes which are created dynamically, now all these
i have a form that has a multiple select drop down. a user can
I have a form that enables a user to update multiple alert rules records
I have a report that is basically a form with multiple empty fields and
Let me try and explain the scenario: I have a form with multiple items
I have a form (new poll form) in which I can add multiple items
I have a multiple items form which does not mention the ID of the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.