wright67uk Posted February 7, 2012 Share Posted February 7, 2012 I have the below javascript file which I am using to calculate a shopping cart. Info is sent via a php file. I have recently modified the php file to include a json array, and now my calculator isnt working properly. How can I modify the javascript file so that it would work as it should? For referance I will include the old and new php file codes, (below the javascript code) My site is currently at www.1pw.co.uk/demo2.php - (calculator bottom right) Ive been trying to get this to work for days on end now and I have now started pulling my hair out! Please anyone? var totalrounded = 0 totalrounded = (Math.round(totalprice*100)/100).toFixed(2); var purchased=new Array(); var totalprice=0; $(document).ready(function(){ $('.product').simpletip({ offset:[40,0], content:'<img src="img/ajax_load.gif" alt="loading" style="margin:10px;" />', onShow: function(){ var param = this.getParent().find('img').attr('src'); if($.browser.msie && $.browser.version=='6.0') { param = this.getParent().find('img').attr('style').match(/src=\"([^\"]+)\"/); param = param[1]; } this.load('ajax/tips.php',{img:param}); } }); $(".product img").draggable({ containment: 'document', opacity: 0.6, revert: 'invalid', helper: 'clone', zIndex: 100 }); $("div.content.drop-here").droppable({ drop: function(e, ui) { var param = $(ui.draggable).attr('src'); if($.browser.msie && $.browser.version=='6.0') { param = $(ui.draggable).attr('style').match(/src=\"([^\"]+)\"/); param = param[1]; } addlist(param); } }); }); function addlist(param) { $.ajax({ type: "POST", url: "ajax/addtocart2.php", data: 'img='+encodeURIComponent(param), dataType: 'json', beforeSend: function(x){$('#ajax-loader').css('visibility','visible');}, success: function(msg){ $('#ajax-loader').css('visibility','hidden'); if(parseInt(msg.status)!=1) { return false; } else { var check=false; var cnt = false; for(var i=0; i<purchased.length;i++) { if(purchased[i].id==msg.id) { check=true; cnt=purchased[i].cnt; break; } } if(!cnt) $('#item-list').append(msg.txt); if(!check) { purchased.push({id:msg.id,cnt:1,price:msg.price}); } else { if(cnt>=3) return false; purchased[i].cnt++; $('#'+msg.id+'_cnt').val(purchased[i].cnt); } totalprice+=msg.price; update_total(); } $('.tooltip').hide(); } }); } function findpos(id) { for(var i=0; i<purchased.length;i++) { if(purchased[i].id==id) return i; } return false; } function remove(id) { var i=findpos(id); totalprice-=purchased[i].price*purchased[i].cnt; purchased[i].cnt = 0; $('#table_'+id).remove(); update_total(); } function change(id) { var i=findpos(id); totalprice+=(parseInt($('#'+id+'_cnt').val())-purchased[i].cnt)*purchased[i].price; purchased[i].cnt=parseInt($('#'+id+'_cnt').val()); update_total(); } function update_total() { if(totalprice) { $('#total').html('total: $'+totalprice); $('a.button').css('display','block'); $('#total').formatCurrency(); } else { $('#total').html(''); $('a.button').hide(); } } CURRENT PHP FILE --- <?php define('INCLUDE_CHECK',1); require "../connect2.php"; if(!$_POST['img']) die("There is no such product!"); $img=mysql_real_escape_string(end(explode('/',$_POST['img']))); $row=mysql_fetch_assoc(mysql_query("SELECT * FROM internet_shop WHERE img='".$img."'")); $jsonArr = array('status' => 1, 'id' => $row['id'], 'thumb' => $row['thumb'],'price' => $row['price'], 'txt' => ' <table width="100%" id="table_'.$row['id'].'"> <tr> <td width="40%">'.$row['thumb'].'</td> <td width="20%">'.$row['name'].'</td> <td width="10%">$'.$row['price'].'</td> <td width="15%"> <select name="'.$row['id'].'_cnt" id="'.$row['id'].'_cnt" onchange="change('.$row['id'].');"> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option></slect> </td> <td width="15%"><a href="#" onclick="remove('.$row['id'].');return false;" class="remove">remove</a></td> </tr> </table>'); echo json_encode($jsonArr); ?> PREVIOUS PHP FILE - (calculator worked fine using this script) <?php define('INCLUDE_CHECK',1); require "../connect2.php"; if(!$_POST['img']) die("There is no such product!"); $img=mysql_real_escape_string(end(explode('/',$_POST['img']))); $row=mysql_fetch_assoc(mysql_query("SELECT * FROM internet_shop WHERE img='".$img."'")); echo '{status:1,id:'.$row['id'].',price:'.$row['price'].$row['thumb'].',txt:\'\ \ <table width="100%" id="table_'.$row['id'].'">\ <tr>\ <td width="60%">'.$row['name'].'</td>\ <td width="10%">$'.$row['price'].'</td>\ <td width="15%"> <select name="'.$row['id'].'_cnt" id="'.$row['id'].'_cnt" onchange="change('.$row['id'].');">\ <option value="1">1</option>\ <option value="2">2</option>\ <option value="3">3</option></slect>\ \ </td>\ <td width="15%"><a href="#" onclick="remove('.$row['id'].');return false;" class="remove">remove</a></td>\ </tr>\ </table>\'}'; ?> Quote Link to comment Share on other sites More sharing options...
Adam Posted February 8, 2012 Share Posted February 8, 2012 What data type is the price column? I'm guessing it's not a numeric type, and so json_encode() is (rightly) treating it as a string. Quote Link to comment Share on other sites More sharing options...
wright67uk Posted February 8, 2012 Author Share Posted February 8, 2012 Hi thanks for the reply, my price column was setup as follwed; `price` double NOT NULL default '0', however the fault turned out to be with my javascript line 104 ish I changed; totalprice+=msg.price; to totalprice+=new Number(msg.price); and now it works fine. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.