Hello Guys,

I coded the following and worked 100% perfectly but I want to do it ajaxly !
how to make all of this sending by ajax with animation(fadeIn,fadeOut)

this my code:
product.php
url: product.php?id=4

<div id="product_page_desc">
        <?php foreach ($products as $info) { ?>
            <div
                style="background:#fff;box-shadow: 0 0 1px #cd9db1;float: left;padding-bottom: 30px;width: 100%;position:relative;">
                <p class='productCategory'>
                    <?php
                    if ($info->type == 'mobile') {
                        echo '<i class="fa fa-mobile"></i>';
                    } else if ($info->type == 'tablet') {
                        echo '<i class="fa fa-tablet"></i>';
                    } else {
                        echo '<i class="fa fa-cogs"></i>';
                    }
                    ?>
                </p> <!-- PRODUCT CATEGORY -->
                <h1
                    class='productFullName'><?= $info->manufacture . ' ' . $info->series . ' ' . $info->model . ' ' . strtoupper($info->colorName) ?></h1>
                <header class="floatLeft clearLeft">
                    <img src='img/<?= $info->image ?>'/>
                </header>
                <!-- FLOAT LEFT HEADER -->
                <div style="padding:1%;width:58%;" class="floatRight clearRight">
                    <div class='details'>
                        <div class='productDetails'>

                            <p class="productAvailability floatLeft"><?= $info->availabilty ?></p>

                            <p class='productAgeDesc'>
                                <?php if ($info->age == 1)
                                    echo 'New';
                                else
                                    echo "";
                                ?>
                            </p> <!-- PRODUCT-AGE-DESC  -->

                            <p class='productYear'><?= $info->year ?></p>

                            <h2 class='briefIntro'><?= $info->description ?></h2>
                        </div>
                    </div>
                    <!-- DETAILS -->

                    <form method="post" action="addToCart.php">
                        <span class="productPrice floatLeft clearLeft">Price: $<?= $info->price; ?></span>

                        <div class="qtyAmount">
                            <span>QTY: </span>
                            <input type="button" value="-" id="minus" class="minus">
                            <input type="text" name="qty" value="1" id="count" class="count" size="10">
                            <input type="button" value="+" id="plus" class="plus">
                        </div>
                        <input type="submit" name="addToCart" class="add-to-cart-productPage floatLeft clearLeft" value="ADD TO BAG">
                    </form>
                </div>
                <!-- FLOAT RIGHT -->
            </div>

        <?php } ?>

        <section class="spec">
            <div class="tabs">
                <ul class="tab-links">
                    <li class="active"><a href="#tab1">Tab #1</a></li>
                    <li><a href="#tab2">Tab #2</a></li>
                </ul>

                <div class="tab-content">
                    <div id="tab1" class="tab active">
                        <p>Tab #1 content goes here!</p>

                        <p>Donec pulvinar neque sed semper lacinia. Curabitur lacinia ullamcorper nibh; quis imperdiet
                            velit eleifend ac. Donec blandit mauris eget aliquet lacinia! Donec pulvinar massa interdum
                            risus ornare mollis.</p>
                    </div>

                    <div id="tab2" class="tab">
                        <p>Tab #2 content goes here!</p>

                        <p>Donec pulvinar neque sed semper lacinia. Curabitur lacinia ullamcorper nibh; quis imperdiet
                            velit eleifend ac. Donec blandit mauris eget aliquet lacinia! Donec pulvinar massa interdum
                            risus ornare mollis. In hac habitasse platea dictumst. Ut euismod tempus hendrerit. Morbi ut
                            adipiscing nisi. Etiam rutrum sodales gravida! Aliquam tellus orci, iaculis vel.</p>
                    </div>

                </div>
            </div>
        </section>
        <article class="featureArea">

        </article>
    </div> <!-- product_page_desc -->

addToCart.php :

<?php require_once "includes/db.php"; ?>
<?php require_once "models/shoppingCart.class.php" ?>
<?php
if (isset($_POST['addToCart'])) {
    $productId = (int)($_GET['id']);
    $cartId = (int)($_COOKIE['trace']);
    $quantity = (int)($_POST['qty']);
    $cart = new ShoppingCart();
    $cart->addToCart($productId, $cartId, $quantity);
}
?>

ShpppingCart.class.php:

class ShoppingCart
{

    public
        $Id,
        $CustomerId,
        $CartDate,
        $IsComplete,
        $IsShipped;

public static function addToCart($productId, $cartId, $quantity)
    {
        $dbh = mySqlDatabase::getConnectionObject();
        // if there's a product in cart where its rows are the same as inserted rows go and update the item with new quantity
        if (ShoppingCartItem::getItemFromCartIfExist($productId, $cartId) == 1) {
            $sql = "UPDATE cartitems
                    SET quantity = quantity + :quantity
                    WHERE productId = :productId";
            $stmt = $dbh->prepare($sql);
            $data = array(
                ':quantity' => $quantity,
                ':productId' => $productId
            );
            $stmt->execute($data);
            return $stmt->rowCount();
        } else {
            $sql = "INSERT INTO cartitems (productId,cartId,singlePrice,quantity)
                VALUES (:productId,
                        :cartId,
                        (SELECT price FROM products WHERE id = :productId),
                        :quantity)";
            $stmt = $dbh->prepare($sql);
            $data = array(
                ':productId' => $productId,
                ':cartId' => $cartId,
                ':quantity' => $quantity
            );
            $stmt->execute($data);
            return $dbh->lastInsertId();
        }
    }
}

Make a file called validate.php — make an ajax request to that, set the cookie, done.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.