Welcome Guest, Not a member yet? Register   Sign In
Javascript validation doesn't work
#1

[eluser]akho[/eluser]
Hi, I'm new with Codeigniter. I'm try to make a shopping cart but when i'm update shopping cart it doesn't work. someone please help me

here is my model
Code:
<?php
class MOrders extends CI_Model{

    function __construct()
    {
    // Call the Model constructor
       parent::__construct();
    }
    
    function updateCart($productid,$fullproduct){
        //pull in existing cart first!
        $cart = $_SESSION['cart'];
        $totalprice = 0;
        if (count($fullproduct)){
            if (isset($cart[$productid])){
                $prevct = $cart[$productid]['count'];
                $prevname = $cart[$productid]['name'];
                $prevprice = $cart[$productid]['price'];
                $cart[$productid] = array(
                    'name' => $prevname,
                    'price' => $prevprice,
                    'count' => $prevct + 1
                );
            }else{
                $cart[$productid] = array(
                'name' => $fullproduct['name'],
                'price' => $fullproduct['price'],
                'count' => 1
                );
            }
            foreach ($cart as $id => $product){
                $totalprice += $product['price'] * $product['count'];
            }
            $_SESSION['totalprice'] = $totalprice;
            $_SESSION['cart'] = $cart;
            $this-> session-> set_flashdata('conf_msg', "We've added this product to your cart.");
        }
    }
    
    function updateCartAjax($idlist){
        $cart = $_SESSION['cart'];
        //split idlist on comma first
        $records = explode(',',$idlist);
        $updated = 0;
        $totalprice = $_SESSION['totalprice'];
        if (count($records)){
            foreach ($records as $record){
                if (strlen($record)){
                    //split each record on colon
                    $fields = explode(":",$record);
                    $id = $fields[0];
                    $ct = $fields[1];
                        if ($ct > 0 && $ct != $cart[$id]['count']){
                            $cart[$id]['count'] = $ct;
                            $updated++;
                        }elseif ($ct == 0){
                            unset($cart[$id]);
                            $updated++;
                        }
                }
            }
            if ($updated){
                $totalprice =0; //with changes, must reset this value!
                foreach ($cart as $id => $product){
                    $totalprice += $product['price'] * $product['count'];
                }
                $_SESSION['totalprice'] = $totalprice;
                $_SESSION['cart'] =$cart;
                echo $updated . "records updated!";
            }else{
            echo "No changes detected!";
            }
        }else{
            echo "No records to update!";
        }
    }
    function removeLineItem($id){
        $totalprice = 0;
        $cart = $_SESSION['cart'];
        if (isset($cart[$id])){
            unset($cart[$id]);
            foreach ($cart as $id => $product){
                $totalprice += $product['price'] * $product['count'];
            }
            $_SESSION['totalprice'] = $totalprice;
            $_SESSION['cart'] = $cart;
            echo "Product removed.";
        }else{
            echo "Product not in cart!";
        }
    }
}        
?>
#2

[eluser]akho[/eluser]
here is my shop

Code:
<?php

class Shop extends CI_Controller {

    function __construct()
    {
        parent::__construct();
        session_start();
    }

    function index(){
    //use this for your home page
        $data['title'] = "Welcome to Claudia's Kids";
        $data['navlist'] = $this->MCats->getCategoriesNav();
        $data['mainf'] = $this-> MProducts-> getMainFeature();
        $skip = $data['mainf']['id'];
        $data['sidef'] = $this-> MProducts->getRandomProducts(3,$skip);
        $data['main'] = 'home';
        $this-> load-> vars($data);
        $this-> load-> view('template');
    }
    
    function cat($id){
    //use this for the category page
        //$cat = $this-> MCats-> getCategory($this-> uri-> segment(3));
        $cat = $this-> MCats-> getCategory($id);
        if (!count($cat)){
            redirect('shop/index','refresh');
        }
        $data['title'] = "Claudia's Kids | ". $cat['name'];
        if ($cat['parentid'] < 1){
            //show other categories
            $data['listing'] = $this-> MCats-> getSubCategories($id);
            $data['level'] = 1;
        }else{
            //show products
            $data['listing'] = $this-> MProducts->getProductsByCategory($id);
            $data['level'] = 2;
        }
        $data['category'] = $cat;
        $data['main'] = 'category';
        $data['navlist'] = $this-> MCats-> getCategoriesNav();
        $this-> load-> vars($data);
        $this-> load-> view('template');
    }
    
    function subcat(){
    //use this for the subcategory view
    }
    
    function product($id){
    //use this for the product view
        $product = $this-> MProducts-> getProduct($id);
        if (!count($product)){
            redirect('shop/index','refresh');
        }
        $data['grouplist'] = $this-> MProducts-> getProductsByGroup(3,$product['grouping'],$id);
        $data['product'] = $product;
        $data['title'] = "Claudia's Kids | ". $product['name'];
        $data['main'] = 'product';
        $data['navlist'] = $this-> MCats-> getCategoriesNav();
        $this-> load-> vars($data);
        $this-> load-> view('template');
    }
    
    function cart($productid=0){
    //use this for the shopping cart view
        if ($productid > 0){
            $fullproduct = $this-> MProducts-> getProduct($productid);
            $this-> MOrders-> updateCart($productid,$fullproduct);
            redirect('shop/product/'.$productid, 'refresh');
        }else{
            $data['title'] = "Claudia's Kids | Shopping Cart";
            if (count($_SESSION['cart']) == true){
                $data['main'] = 'shoppingcart';
                $data['navlist'] = $this-> MCats-> getCategoriesNav();
                $this-> load-> vars($data);
                $this-> load-> view('template');
            }else{
                redirect('shop/index','refresh');
            }
        }
    }
    
    function search(){
    //use this for the search results
        if ($this-> input-> post('term')){
            $data['results'] = $this-> MProducts-> search($this-> input-> post('term'));
        }else{
            redirect('shop/index','refresh');
        }
        $data['main'] = 'search';
        $data['title'] = "Claudia's Kids | Search Results";
        $data['navlist'] = $this-> MCats-> getCategoriesNav();
        $this-> load-> vars($data);
        $this-> load-> view('template',$data);
    }
    
    function ajax_cart(){
        //$this-> load-> model('MOrders','',TRUE);
        return $this-> MOrders-> updateCartAjax($this-> input-> post('ids'));
    }
    function ajax_cart_remove(){
        return $this-> MOrders-> removeLineItem($this-> input-> post('id'));
    }
    function about_us(){
    //use this for the about_us page
    }
    
    function contact (){
    //use this for the contact page
    }
    
    function privacy(){
    //use this for the privacy page
    }
}
#3

[eluser]akho[/eluser]
here is my shoppingcart

Code:
<h1>Shopping Cart</h1>
<div id='pleft'>
&lt;?php echo form_open('shop/checkout'); ?&gt;
<table border='1' cellspacing='0' cellpadding='5'>
&lt;?php
        $TOTALPRICE = $_SESSION['totalprice'];
        if (count($_SESSION['cart'])){
        foreach ($_SESSION['cart'] as $PID => $row){
            $data = array(
                'name' => "li_id[$PID]",
                'value'=> $row['count'],
                'id' => "li_id_$PID",
                'size' => 5,
                'class' => 'process'
            );
            echo " <tr valign='top'> \n";
            echo " <td> ". form_input($data)." </td> \n";
            echo " <td id='li_name_".$PID."'> ". $row['name']." </td> \n";
            echo " <td id='li_price_".$PID."'> ". $row['price']." </td> \n";
            echo " <td id='li_total_".$PID."'> ".$row['price'] * $row['count']." </td> \n";
            echo "<td> <a href='#'>delete</a></td>\n";
            echo " </tr> \n";
        }
        $total_data = array('name' => 'total', 'id'=> 'total', 'value' => $TOTALPRICE);
        echo " <tr valign='top'> \n";
        echo " <td colspan='2'> &nbsp; </td> \n";
        echo " <td> $TOTALPRICE ".form_hidden($total_data)." </td> \n";
        echo " </tr> \n";
        echo "<tr valign='top'> \n";
        echo "<td colspan='2'> &nbsp; </td> \n";
        echo "<td> &lt;input type='button' name='update' value='update'&gt; </td> \n";
        echo "</tr> \n";
        echo " <tr valign='top'> \n";
        echo " <td colspan='2'> &nbsp; </td> \n";
        echo " <td> ".form_submit('submit', 'checkout')." </td> \n";
        echo " </tr> \n";
        }else{
            //just in case!
            echo " <tr> <td> No items to show here! </td> </tr> \n";
        }//end outer if count
?&gt;
</table>
&lt;/form&gt;
</div>
<div id='ajax_msg'> </div>
#4

[eluser]akho[/eluser]
and here is my javascript

Code:
function jsUpdateCart(){
  var parameter_string = '';
  allNodes = document.getElementsByClassName("process");
  for(i = 0; i < allNodes.length; i++) {
      var tempid = allNodes[i].id;
    var temp = new Array;
    temp = tempid.split("_");
     var real_id = temp[2];
     var real_value = allNodes[i].value;
    parameter_string += real_id +':'+real_value+',';
  }

  var params = 'ids='+parameter_string;
  var ajax = new Ajax.Updater(
    'ajax_msg','/shop/ajax_cart', {method:'post',parameters:params,onComplete:showMessage}
    );

}

function showMessage(req){
  $('ajax_msg')[removed] = req.responseText;
  location.reload(true);
}

function jsRemoveProduct(id){
    var params = 'id='+id;
    var ajax = new Ajax.Updater(
        'ajax_msg','/welcome/ajax_cart_remove', {method:'post',parameters:params,onComplete:showMessage}
    );
}

When i press update or delete it doesn’t work. Could someone please tell me what’s wrong with my code? I'm already call this javascript in my template with this code
Code:
&lt; script type="text/javascript" src="&lt;?php echo base_url();?&gt;js/customtools.js"&gt; < /script >




Theme © iAndrew 2016 - Forum software by © MyBB