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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T06:20:38+00:00 2026-05-14T06:20:38+00:00

I am trying to build a shopping cart site. When a user click add

  • 0

I am trying to build a shopping cart site. When a user click add to cart image on the product page, The product title will show a “The product is in your cart” text without reloading the page. I am using session and ajax but no luck so far. I appreciate any helps.

My html cold

 <table id="<?php echo $productId; ?>" width="594" border="0" cellpadding="5" cellspacing="0">
    <tr>
                <td><img src="<?php echo "$brandImage"; ?></td>
    <td <?php echo $productName; ?>  //The "The product is in your cart" will be showed here</td>
        </tr> 
<tr>
<td><a class="addToCart" href="javascript:void(0);" onclick="addToCart(<?php echo $productId?>)"> 
</td>

My Javascript file (addToCart.js)

   function addToCart(productId){

var url="addToCart.php";
url=url+"?productId="+productId;
url=url+"&sid="+Math.random();


$.post(
 url,
 function(responseText){
  alert(responseText); //I wish I can get productData value from addToCart.php
 },
 "html"
)

My php file (addToCart.php)

   <?php SESSION_START();

$productId=$_GET['productId'];
$cart=$_SESSION['cart'];

if(isset($cart)){
 $cart.=",".$productId;
 $product=explode(',',$cart);
 $totalItem=count($product);

}else{
 $cart=$productId;
 $totalItem=1;

};
$productData=array();
foreach($product as $id){
$productData[$id]=(isset($productData[$id])) ? $productData[$id]+1 :1;
};
$_SESSION['cart']=$cart;
//print_r($productData);
echo $productData; //Not sure what to do to send $productData back to my addToCart.js variable




?>

I tried to make the code look simple. Any suggestion will be a great help. Thanks

  • 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-14T06:20:38+00:00Added an answer on May 14, 2026 at 6:20 am

    The most simple way is to use json_encode() on the variable.
    so…

    header('Content-type: text/javascript; charset=UTF-8'); // or what ever charset you're using
    echo json_encode($productData);
    

    then you can just read the values in responseText as:

    alert(responseText.whatever);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to build a shopping website with ajax. When a user clicks
I am trying to build a simple shopping cart, i read about controlling them
I'm trying to build a shopping cart. There's very simple class which creates a
I am trying to build up a small model of a shopping cart. It
Trying to build a shopping cart based on the Agile Web Development 4th edition.
I am trying to build a custom library of a Shopping Cart. I had
I'm trying to display a user's shopping cart with the items listed. What I
I'm learning Python, Django, Javascript and jQuery by trying to build a simple shopping
Trying to build a website where the background of the site is a video
Hi i was recently looking over a shopping cart paginator class, trying to understand

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.