javascript - Refresh SPAN content automatically -


i'm stuck in problem here tried google answers , nothing did work out. specially because prefer pure javascript solution if possible... have menu, cart. menu shows like: cart (0) 0 number of items inside cart. items number query sql table 'cart', save users products added cart. don't lose items on cart when session closes. want update menu span everytime add inside cart, without need refresh page. items being added way:

in table products displayed have:

<td id="add'.htmlspecialchars($row['id'], ent_quotes, 'utf-8').'"> <label><a href="javascript:addtocart('.htmlspecialchars($row['id'], ent_quotes, 'utf-8').')"><b><img src="/img/cart.gif" /></b></a></label> </td> 

so call javascript:addtocart passing 'id' of product parameter. function does:

function addtocart(id) {     document.getelementbyid('add'+id).innerhtml='<b>on cart!</b>';     load('/index.php?addtocart&id='+id); } 

ok, function load does:

function load(url) { var xmlhttp;  if (window.xmlhttprequest) {     // code ie7+, firefox, chrome, opera, safari     xmlhttp = new xmlhttprequest(); } else {     // code ie6, ie5     xmlhttp = new activexobject('microsoft.xmlhttp'); }  xmlhttp.onreadystatechange = function() {     if (xmlhttp.readystate == 4 && xmlhttp.status == 200) {         eval( xmlhttp.responsetext );     } }  xmlhttp.open('get', url, true); xmlhttp.send(); } 

ok, have index.php receiving id of product add cart, , on index.php:

if (isset($_get['addtocart']) && isset($_get['id'])) {     $id = mysql_real_escape_string($_get['id']);     mysql_query("insert cart values('$id', '$userid')");            } 

this working well, , if refresh page, in menu "your cart (1)"... how refresh automatically when client clicks on table? possible pure javascript function?

//edit index.php content:

<?php     include("./includes/header.php");     include("./includes/db.php"); ?> <html> <head>     <link rel="stylesheet" href="./css/style.css">     <title>myshop</title>     <script type="text/javascript">      function load(url) {     var xmlhttp;      if (window.xmlhttprequest) {         // code ie7+, firefox, chrome, opera, safari         xmlhttp = new xmlhttprequest();     } else {         // code ie6, ie5         xmlhttp = new activexobject('microsoft.xmlhttp');     }      xmlhttp.onreadystatechange = function() {         if (xmlhttp.readystate == 4 && xmlhttp.status == 200) {            document.getelementbyid('cart').innerhtml = xmlhttp.responsetext;         }     }      xmlhttp.open('get', url, true);     xmlhttp.send();     }      function addtocart(id)     {         document.getelementbyid('add'+id).innerhtml='<b>on cart!</b>';         load('/index.php?addtocart&id='+id);     }     </script> </head> <body id="home">     <?php include 'menu.php'; ?>     <div id="container">         <?php              if (isset($_get['news']))             {                 include 'news.php';                      }             else if (isset($_get['orders']))             {                 include 'orders.php';                        }             else if (isset($_get['addtocart']) && isset($_get['id']))             {                 $id = mysql_real_escape_string($_get['id']);                 mysql_query("insert cart values('$id', '$userid')");                        }         ?>     </div> </body> </html> 

from index.php page, return number of products inserted current user , use success handler of ajax call update span.

xmlhttp.onreadystatechange = function() {     if (xmlhttp.readystate == 4 && xmlhttp.status == 200) {         // here number of products inserted , display in span         document.getelementbyid('span_id').innerhtml = xmlhttp.responsetext;     } } 

edit

do not make ajax call same page. have id sent other page, let's abc.php. keep php related code inside update_cart_ajax.php page.

so addtocart() becomes:

function addtocart(id) {     document.getelementbyid('add'+id).innerhtml='<b>on cart!</b>';     load('/update_cart_ajax.php?addtocart&id='+id); } 

update_cart_ajax.php

<?php           include("./includes/db.php"); if (isset($_get['addtocart']) && isset($_get['id'])) {    $id = mysql_real_escape_string($_get['id']);    mysql_query("insert cart values('$id', '$userid')");            } else {     echo 0;     exit; } // number of products added cart current user $total_products_in_cart = <value returned db>; echo $total_products_in_cart; exit; 

Comments

Popular posts from this blog

c++ - OpenCV Error: Assertion failed <scn == 3 ::scn == 4> in unknown function, -

php - render data via PDO::FETCH_FUNC vs loop -

The canvas has been tainted by cross-origin data in chrome only -