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 7634579
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T07:12:44+00:00 2026-05-31T07:12:44+00:00

i have a form on a page with a hidden input with the name

  • 0

i have a form on a page with a hidden input with the name of a book and a submit button i use to add the book name to an an arry contanined in the $_SESSION array. each time the user clicks on the button the name of the book is added to the array contained in the session.this works fine. the main problem is when i refresh the page it the name of the book to the array again without me pressing the “add to cart button” i can see why this is happening as when i send the value to the array in an if statement i make sure and unset() the submit button. here is the code for the php part.

 <?php
   if(isset($_SESSION['username']))
   {
      if(!isset($_SESSION['itemnames']))
      {
        $itemnames = array();
        $_SESSION['itemnames'] = $itemnames;
    echo "<p>second if</p>"; 
      }
      if (isset($_GET['submit1']))
      {
       $name = $_GET['book1'];
       $_SESSION['itemnames'][]= $name ;
       unset($_GET['submit1']);
       echo "<p>" . var_dump($_SESSION['itemnames']) . "</p>";
      }
   } 
?>

and here is my form i also have another form on the page and im wondering if this is causing the problem i dont think so at the moment.

<form method="get" action="" name="myform">
    <span style="color:#000">
        <label for="quantity">Quantity:</label>
    </span>
    <select name="quantity" id="quantity">
        <option value="1" selected>1</option>
        <option value="2">2</option>
        <option value="3">3</option>
        <option value="4">4</option>
        <option value="5">5</option>
        <option value="6">6</option>
        <option value="7">7</option>
        <option value="8">8</option>
        <option value="9">9</option>
        <option value="10">10</option>
        <option value="11">11</option>
        <option value="12">12</option>
        <option value="13">13</option>
        <option value="14">14</option>
        <option value="15">15</option>
        <option value="16">16</option>
        <option value="17">17</option>
        <option value="18">18</option>
        <option value="19">19</option>
        <option value="20">20</option>
        <option value="21">21</option>
        <option value="22">22</option>
        <option value="23">23</option>
        <option value="24">24</option>
        <option value="25">25</option>
        <option value="26">26</option>
        <option value="27">27</option>
        <option value="28">28</option>
        <option value="29">29</option>
        <option value="30">30</option>
    </select>
    <input type="hidden" name="book1" value="cape clear island: its peopleand landscape"
    />
    <input type="submit" value="Add To Cart" name="submit1">
    </button>
</form>

i cant think why refreshing the page will add one to my array! im adding the name numerous time just to test will the multiple clicks work. thanks for your help in advance! obviously ill be doing other stuff with the dropdown list when i get this part working.
what i basically have is a submit button on a page that i should be able to click multiple times to add a book to a session array depending on how many times i want to add the book to the cart this will increment a counter at the top of the page depending on how many times the item is clicked. i just dont want the book to increment in the session array when i refresh. so the add item to cart button does not redirect the user to the cart im going to have a seperate button to “view cart” if you get what im trying to do here.

  • 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-05-31T07:12:46+00:00Added an answer on May 31, 2026 at 7:12 am

    You should use the POST method for a form of this nature to help avoid problems like this.

    Either way (GET or POST) you should issue a 303 See Other HTTP status code after you have handled the form submission, and redirect the user to the original page.

    For example:

    if (isset($_GET['submit1'])) {
      // Add the item to the array
      $_SESSION['itemnames'][] = $_GET['book1'];
      // Redirect and exit
      header('HTTP/1.1 303 See Other');
      header('Location: '.$_SERVER['HTTP_REFERER']);
      exit;
    }
    

    In the above example I have used the Referer: header to determine where to send the user. This is not necessarily the best approach, as the PHP manual states:

    Not all user agents will set this, and some provide the ability to modify HTTP_REFERER as a feature. In short, it cannot really be trusted.

    However, since I know nothing of the way you site is structured I have used this, since it will work correctly 99% of the time.

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

Sidebar

Related Questions

I have a form in an HTM page that, after pressing the submit button,
I have been using hidden values for forms. Example: <form method=post action=page.php> <input type=text
Simple question. I have the following hidden input field on my ASP.NET page: <form
I have a form containing 2 input fields, one being hidden and a button.
I have a form on an HTML page with multiple submit buttons that perform
I have a form loading inside my page and there is a button out
I have a simple form on a page and I want to auto submit
I have a form on a non-SSL page that I want to submit as
On a page that I'm designing I have a form with one input of
I have multiple forms like so: <form method=post action=change.php id=form1> <input name=product_id type=hidden value=abc

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.