0

When I select products, Quantity is being passed but not getting shown in NUMBERS (1,2 or 3) on the VIEW CART page.

Confusing me why is it happening? Any help would be much appreciated.

I tried to pass quantity value using ajax in two files view cart and add_Ajax but it doesn't display my selected quantity.

<form method="GET" action="add_Ajax.php">
                        <input type="hidden" value="<?php echo $row[0];?>" name="id"> 
                    <div class="col-md-6">
                        <div class="product-body">
                            <div class="product-label">



        <div class="product-btns">
        <div class="qty-input">
                <span class="text-uppercase">QTY: </span>   <select name="q" id="q" style="position:absolute; margin-left:10px; margin-top:-5px;">


                                <option value="1">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>



                                </select>
                                </div>


                                <script>
function additem(id,quantity)
{
    q_index  = document.getElementById("q").selectedIndex;
    q = document.getElementById("q").options[q_index].value;

    var xhttp = new XMLHttpRequest();
     xhttp.open("GET", "add_ajax.php?id="+id+"&quantity="+q, true);
  xhttp.send();
  xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
     // alert('item added');
    }
  };

}
</script>
                                <h3 style="margin-top:20px;">Tale :-</h3>
                                <p style="font-size:16px;"><?php echo $row[8];?></p>
              <button type="button" onClick="additem(<?php echo $row['id'];?>)" class="btn btn-info" data-toggle="modal" data-target="#myModal<?php echo $row['id'];?>">Add to cart</button>

  <div id="myModal<?php echo $row[0];?>" class="modal fade" role="dialog">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                         <button type="button" class="close" data-dismiss="modal">&times;</button>
                            <h4 class="modal-title">Details</h4>
                    </div>
                    <div class="modal-body">
            <p>Item Added to cart
                <?php echo $row[1];?>
                <?php echo $row[2];?>
                <?php echo $row[3];?>



              <a href="cart.php"> <button type="button" class="btn btn-info" >View Cart</button></a>
                <button type="button" class="btn btn-info" data-dismiss="modal">Continue Shopping</button>

                    </div>
                </div>
            </div>
        </div>
                            </form>

<---add_Ajax.php---->
session_start();
$id = $_GET['id'];
$quantity = $_GET['q'];
echo "<script>alert('".$quantity."');</script>";

    $con  = mysqli_connect("localhost","root","") or die(mysqli_error($con));
    mysqli_select_db($con,"booksjerry")  or die(mysqli_error($con));
    require_once("product.php");
    $quantity = isset($_GET['q']) ? $_GET['q'] : 1;
    $result = mysqli_query($con,"select * from products where id='".$id."'");
    $row = mysqli_fetch_array($result);

    $p = new product();
    $p->img = $row['image'];
    $p->id = $row['id'];
    $p->name = $row['name'];
    $p->price = $row['actualprice'];
    $p->quantity = $quantity;



    @$cart = unserialize(serialize($_SESSION['cart']));
print_r(@$cart);
    $index=-1;
    if($cart)
    {
        foreach(@$cart as $k=>$v)
        {
        if($v->id==$id)
        {
        $index = $k;
        echo $index;
        break;      
        }

        }
    }

    echo 'index: '.$index;
    if($index>=0)
    {
    $cart[$index]->quantity++;
    $_SESSION['cart']= $cart;

    @$c = (int) $_SESSION['count'];
    $c = $c+1;
    $_SESSION['count'] = $c;
    //echo "found".$index;
    }
        else
    {
    echo 'not found';
    $_SESSION['cart'][]= $p;
    $_SESSION['count'] = 1;
    }


    //$_SESSION['cart'][]= $p;
    //print_r($_SESSION['cart']);









if(isset($_GET['rem_id']))
{
    require_once("product.php");
    $id = $_GET['rem_id'];
    @$cart = unserialize(serialize($_SESSION['cart']));
    $index=  -1;
    foreach($cart as $k=>$v)
    {
        if($v->id==$id)
        {
        $index = $k;
        echo $index;
        break;      
        }

    }
    if($index>=0)
    {   
        unset($cart[$index]);
        $_SESSION['cart']= $cart;
    }
}
?>

I want to pass my selected quantity from product page to view cart and add_Ajax but it doesn't display my selected quantity it shows 1.

Vikrant
  • 4,920
  • 17
  • 48
  • 72

1 Answers1

0

If I correct, then you are sending in the request the quantity param. So you can reference to it with $_GET['quantity'] in add_Ajax.php, not $_GET['q'].

But if you change

xhttp.open("GET", "add_ajax.php?id="+id+"&quantity="+q, true);

to

xhttp.open("GET", "add_ajax.php?id="+id+"&q="+q, true);

then it works better. :)

hNczy
  • 305
  • 1
  • 8