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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T17:03:24+00:00 2026-05-22T17:03:24+00:00

I have this code to prosses the ADD TO CART button: if (isset($_POST[‘pid’])) {

  • 0

I have this code to prosses the ADD TO CART button:

if (isset($_POST['pid'])) {
$pid = $_POST['pid'];
$wasFound = false;
$i = 0;
// If the cart session variable is not set or cart array is empty
if (!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1) {
    // RUN IF THE CART IS EMPTY OR NOT SET
    $_SESSION["cart_array"] = array(0 => array("item_id" => $pid, "quantity" => 1));
} else {
    // RUN IF THE CART HAS AT LEAST ONE ITEM IN IT
    foreach ($_SESSION["cart_array"] as $each_item) {
          $i++;
          while (list($key, $value) = each($each_item)) {
              if ($key == "item_id" && $value == $pid) {
                  // That item is in cart already so let's adjust its quantity using array_splice()
                  array_splice($_SESSION["cart_array"], $i-1, 1, array(array("item_id" => $pid, "quantity" => $each_item['quantity'] + 1)));
                  $wasFound = true;
              } // close if condition
          } // close while loop
       } // close foreach loop
       if ($wasFound == false) {
           array_push($_SESSION["cart_array"], array("item_id" => $pid, "quantity" => 1));
       }
}
exit();
}

And my add button:

<form id="bd_itm1" name="bd_itm1" method="post" action="help_scripts/cart_functions.php">
      <input type="hidden" name="pid" id="pid" value="'. $id . '" />
      <input type="submit" name="button" id="button" value="Add to Cart" />
</form>

When I click it it takes me to the cart.php as I tell it to do with my header(location).
But I do not want it to. I want to stay at the same page as my ad button.
Now, if I remove my header(location) it takes me to a blank page. (but still it adds the item to cart)
No errors in my logs. FOR SURE. 🙂

Many are saying things about ajax and jquery… but I am not that familiar with these!

Thank you

My dynamicList code: (I don’t know what’s happening but just now it showed me your question)

$sql = mysql_query("SELECT * FROM products WHERE category='body' ORDER BY id ASC");
$productCount = mysql_num_rows($sql);
// count the output amount
if ($productCount > 0) {
        $i=0;
        $dynamicListBody = '<table width: 90%; margin-right: auto; margin-left: auto; color: #00E6AA;>';
        while($row = mysql_fetch_array($sql)){
            $id = $row["id"];
            $product_name = $row["product_name"];
            $details = $row["details"];
            $price = $row["price"];
            $date_added = strftime("%b %d, %Y", strtotime($row["date_added"]));
        $dynamicListBody .= ($i==0) ? '<tr>':'';
        $dynamicListBody .= '<td width="10%">
                    <img style="border:#666 1px solid;" src="../stock_photos/' . $id . '.png" height="80px" width="40px" alt="' . $product_name . '" />
                     </td>
                     <td width="35%">
                    <span class=itmttl>' . $product_name . '</span>
                    <br />
                    <span class=text>' . $details . '
                    <br />
                    €' . $price . '</span>
                    <br />
                      <form name="bd_itm" id="bd_itm" method="post" action="help_scripts/cart_functions.php">
                    <input type="hidden" name="pid" id="pid" value="' . $id . '" />
                    <input type="submit" name="button' . $id . '" id="button' . $id . '" value="Add to Cart" />
                    <br />
                    <br />
                      </form>
                     </td>';
        $dynamicListBody .= ($i==1) ? '</tr>':'';
        $i++;
        ($i==2) ? $i=0:'';
        }
        $dynamicListBody.='</table>';
        } else {
            $dynamicListBody = "We have no products listed in our store yet";
            }
        mysql_close();
?>
  • 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-22T17:03:25+00:00Added an answer on May 22, 2026 at 5:03 pm

    If you don’t mind the page refreshing after the POST from your button you could add a redirect in your php code to bring you back to the page the button is on.

    header('Location: http://domain.com/button/page.php');
    exit();
    

    You’ll have to use AJAX if you want the page not to refresh.

    with jquery:

    $(':submit').click(function() {
        var keyValues = {
            pid : $(this).parent().find('input[name="pid"]').val()
        };
        $.post('help_scripts/cart_functions.php', keyValues, function(rsp) {
            // make your php script return some xml or json that gives the result
            // rsp will be the response
        });
        return false; // so the page doesn't POST
    });
    

    put the above javascript in a .js file that is loaded with your button page.

    You may want to make this change in your php that runs when the button is pressed:

    $_SESSION["cart_array"] = array(0 => array("item_id" => $pid, "quantity" => 1));
    

    Change the above to

    $_SESSION["cart_array"] = array("item_id" => $pid, "quantity" => 1);
    

    this is the code that runs if the cart array is not set or is empty

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

Sidebar

Related Questions

I have this code <?php session_start(); if (isset($_GET[cmd])) $cmd = $_GET[cmd]; else die(You should
I have this code for when the user presses a button: SettingsViewController *settingsViewController =
I have this code in jQuery, that I want to reimplement with the prototype
I have this code: chars = #some list try: indx = chars.index(chars) except ValueError:
I have this code that performs an ajax call and loads the results into
I have this code #include <iostream> using namespace std; int main(int argc,char **argv) {
I have this code: CCalcArchive::CCalcArchive() : m_calcMap() { } m_calcMap is defined as this:
I have this code: myVariable which I want to change into trace(myVariable: + myVariable);
I have this code while($row = mysql_fetch_row($result)) { echo '<tr>'; $pk = $row[0]['ARTICLE_NO']; foreach($row
I have this code, which works fine, but I would like to be able

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.