Latest News:
v1.2.1 Release Date is Sunday August 31st
on August 27, 2008, 08:52:45 PM [View]
Welcome, Guest. Please login or register.
August 28, 2008, 08:56:08 AM
Show Unread Posts | View Replies to Your Posts


Login with username, password and session length
cpCommerce Support  |  Version 1.x.x  |  Bug Reports  |  Resolved  |  Topic: Javascript error on Sub-Products 0 Members and 1 Guest are viewing this topic.
Pages: [1] Send this topic Print
Author Topic: Javascript error on Sub-Products  (Read 2038 times)
tyutyuszomoru
Junior Member
*
Offline Offline

Posts: 1


View Profile
« on: May 24, 2007, 07:05:39 AM »

Hi!

In case someone else has this problem:

Fresh install v.1.1.1 Sub-Products page shows the following errors:
showing the dropdowns but both in-and out of stock is shown, prices won't appear when different sub products are chosen, etc.

Generally speaking subproducts.php doesn't return with a javascript, so no javascript functions are working.

SOLUTION:

The problem was, that languages/{yourlanguage}.php has returned "You are not allowed to view this script" as the CPCOMMERCE RUNNING wasn't defined in subproducts.php

Just define it anywhere before the require of {yourlanguage}.php...

Report to moderator   Logged
jayarmstrong1
Junior Member
*
Offline Offline

Posts: 11


View Profile
« Reply #1 on: July 25, 2007, 02:23:00 AM »

But how do we do that?
Report to moderator   Logged
Andreww1
Junior Member
*
Offline Offline

Posts: 7


View Profile
« Reply #2 on: September 18, 2007, 04:05:51 PM »

thanks but any information on how to define it?
« Last Edit: September 18, 2007, 04:08:07 PM by Andreww1 » Report to moderator   Logged
marwol
cpModerators
Involved Member
*****
Offline Offline

Posts: 157


View Profile
« Reply #3 on: September 18, 2007, 04:09:58 PM »

I will need a solution to.

/marwol
Report to moderator   Logged
Andreww1
Junior Member
*
Offline Offline

Posts: 7


View Profile
« Reply #4 on: September 18, 2007, 04:14:20 PM »

i think we need  to add:

<?php 
  if (!defined("CPCOMMERCE_LOADED"))
  {
    die("You are not allowed to access this script.");
  }
?>

testing it now
Report to moderator   Logged
Andreww1
Junior Member
*
Offline Offline

Posts: 7


View Profile
« Reply #5 on: September 18, 2007, 04:32:25 PM »

i added the above to cpcommerce/template/default/script/subproduct.php with no luck

still same problems
Report to moderator   Logged
marwol
cpModerators
Involved Member
*****
Offline Offline

Posts: 157


View Profile
« Reply #6 on: September 19, 2007, 03:57:28 AM »

Hi

I might have found a solution. Here is the new code to put in the subproducts.php file.

Ii have just changed some small things and added
Code:
Header("content-type: application/x-javascript");
at the top of the script.

Code:
<?
Header("content-type: application/x-javascript");
echo "var subproducts = new Array();\r\n";
echo "var selectboxes = new Array();\r\n";

  // Grab the Configuration Information
  require_once("../../../_config.php");
 
  // If the user did not update their _config.php file
  if (!isset($config['mysqli']))
  {
    $config['mysqli'] = 0;
  }
  ## Connect to the Database
  require_once("../../../_dbquery.php");
  $db_chooser = new db_chooser;
 
  // Get Configuration Data
  $config_query = $db_chooser->sql_query("select * from {$config['prefix']}ConfigSettings");
  if ($config_query)
    while ($data = $db_chooser->sql_fetch_assoc($config_query))
      $config[$data['name']] = $data['inputvalue'];
  // Include the Language Pack
  require_once("../../../{$config['up2date_language']}");
  if (isset($_SESSION['cpLanguage']))
    require_once("../../../{$_SESSION['cpLanguage']}");
  else
    require_once("../../../{$config['language']}");

// Include the Format Currency Function
  require_once("../../../{$config['functions']}format_currency.func.php");
 
  // Query the Database for the Drop Down Types
  $sql['dropdowntypes']   = "select `id_droptype` from `{$config['prefix']}DropDownTypes` " .
                            "where `id_product`={$_GET['id_product']}";
  $query['dropdowntypes'] = $db_chooser->sql_query($sql['dropdowntypes']);
 
  // Cycle through the Drop Down Types
  $count = 0;
  while ($info['dropdowntypes'] = $db_chooser->sql_fetch_assoc($query['dropdowntypes'])) {
    // Output the JavaScript Code
    echo "selectboxes[{$count}] = \"{$info['dropdowntypes']['id_droptype']}\";\r\n";
   
    $count++;
  }
// Query the Database for the SubProducts
  $sql['subproducts']   = "select `id_subproduct`, `name`, `price`, `stock` from " .
                          "`{$config['prefix']}SubProducts` where `id_product`={$_GET['id_product']}";
  $query['subproducts'] = $db_chooser->sql_query($sql['subproducts']);
 
  // Cycle through the SubProducts
  $count = 0;
while ($info['subproducts'] = $db_chooser->sql_fetch_assoc($query['subproducts'])) {
    // Output the JavaScript Information
    echo "subproducts[{$count}] = new Array();\r\n";
    echo "subproducts[{$count}][0] = \"" . addslashes($info['subproducts']['name']) . "\";\r\n";
    echo "subproducts[{$count}][1] = \"" . format_currency("{$info['subproducts']['price']}") . "\";\r\n";
    echo "subproducts[{$count}][2] = \"{$info['subproducts']['stock']}\";\r\n";
   
    // Check to see if the Item is On Sale
    $sql['sale']   = "select `price`, `limit` from `{$config['prefix']}Sales` where `id_product`=" .
                     "{$_GET['id_product']} and `id_subproduct`={$info['subproducts']['id_subproduct']} " .
                     "and `start`<=NOW() and `last`>=NOW()";
    $query['sale'] = $db_chooser->sql_query($sql['sale']);
   
    // Output the JavaScript Sale Information
    if ($query['sale'] && $db_chooser->sql_num_rows($query['sale']) > 0) {
      // Grab the Results
      $info['sale'] = $db_chooser->sql_fetch_assoc($query['sale']);
     
      // Check if the Sale Limit is Unlimited
      if ($info['sale']['limit'] == -1)
        $info['sale']['limit'] = "{$output['0038']}";
     
      echo "subproducts[{$count}][3] = \"1\";\r\n";
      echo "subproducts[{$count}][4] = \"" . format_currency("{$info['sale']['price']}") . "\";\r\n";
      echo "subproducts[{$count}][5] = \"{$info['sale']['limit']}\";\r\n";
    } else {
      echo "subproducts[{$count}][3] = \"0\";\r\n";
      echo "subproducts[{$count}][4] = \"" . format_currency("0.00") . "\";\r\n";
      echo "subproducts[{$count}][5] = \"0\";\r\n";
    }
   
    $count++;
  }

?>

function getObjectSubProducts(id) {
  var ref;
  var NS4DOM = document.layers ? true:false;
  var IEDOM = document.all ? true:false;
  var W3CDOM = document.getElementById ? true: false;
  if (NS4DOM)
    ref = "document." + id;
  if (IEDOM)
    ref = id;
  if (W3CDOM)
    ref = "document.getElementById('" + id + "')";
 
  var object = eval(ref);
  return object;
}

function ShowProductStatus() {

  var selected = "";
  var option1, option2;
  var formItem, object;



  // Read the Select Boxes

  //for (option1 in selectboxes) {
//    formItem = getObjectSubProducts("dropdownitem" + selectboxes[0]);
//    selected += formItem.options[formItem.selectedIndex].text + ", ";
//  }


for (var option1 = 0; option1 <= (selectboxes.length -1); option1++)
   {
    formItem = getObjectSubProducts("dropdownitem" + selectboxes[option1]);
    selected += formItem.options[formItem.selectedIndex].text + ", "; 
   }

  // Fix Safari Handle of Drop Down Options
  if (selected.substring(0, 1) == " ")
    selected = selected.substring(1, selected.length);
 
  // Cut off the last ', '
  selected = selected.substring(0, selected.length - 2);

  // Cycle through the subproducts and see if there is a match
  //for (option2 in subproducts)
for (var option2 = 0; option2 <= (subproducts.length -1); option2++)

    // The Item was found
    if (subproducts[option2][0] == selected) {
      // Check the Stock for this Item
      if (subproducts[option2][2] != "0") {
        object = getObjectSubProducts("divSubProductInStock");
        object.style.display="block";
        object = getObjectSubProducts("divSubProductOutOfStock");
        object.style.display="none";
        object = getObjectSubProducts("inpAddToCart");
        object.style.visibility="visible";
        object = getObjectSubProducts("selQuantity");
        object.style.visibility="visible";
        var listObj = object;

        // Remove the Current Options
        while (listObj.options.length)
          // Make sure FireFox Operates Properly
          if (document.all)
            listObj.options.remove(0);
          else
            listObj.remove(0);
       
        // Create the Stock Choices
        var newObj;
        for (var i = 1; i <= 10 && (i <= parseInt(subproducts[option2][2]) ||
             subproducts[option2][2] == "-1"); i++) {
          newObj = document.createElement("OPTION");
          newObj.text  = i;
          newObj.value = i;
         
          // Make sure FireFox Operates Properly
          if (document.all)
            listObj.add(newObj);
          else
            listObj.add(newObj, null);
        }
       
        // Select the first option
        listObj.options[0].selected = true;
      }

else {
        object = getObjectSubProducts("divSubProductInStock");
        object.style.display="none";
        object = getObjectSubProducts("divSubProductOutOfStock");
        object.style.display="block";
        object = getObjectSubProducts("inpAddToCart");
        object.style.visibility="hidden";
        object = getObjectSubProducts("selQuantity");
        object.style.visibility="hidden";

      }

      // Check the Price for this Item
      object = getObjectSubProducts("divSubProductPrice");
      if (subproducts[option2][3] == "1")
        // Item is on Sale
        object.innerHTML = "<span class=\"spnProductPrice\"><strike>" + subproducts[option2][1] +
                           "</strike></span> " + "<span class=\"spnProductSale\">" +
                           subproducts[option2][4] + "</span><br />" + "<span " +
                           "class=\"spnProductSaleLimit\">" + "<?php echo $output['0131']; ?> " +
                           subproducts[option2][5] + "</span>";
      else
        // Item is not on Sale
        object.innerHTML="<span class=\"spnProductPrice\">" + subproducts[option2][1] + "</span>";

      }
    }

I works for me. Please test it and post your comment here.

If other can confirm it works, I will move this thread to "solved.


/marwol
« Last Edit: September 19, 2007, 04:00:07 AM by marwol » Report to moderator   Logged
Andreww1
Junior Member
*
Offline Offline

Posts: 7


View Profile
« Reply #7 on: September 19, 2007, 05:34:08 PM »

i copied your code as it is and that did nothing for me. i still have the same error.

see: http://labconsumables.com.au/cpcommerce/product.php?id_product=7
« Last Edit: September 19, 2007, 07:09:42 PM by Andreww1 » Report to moderator   Logged
marwol
cpModerators
Involved Member
*****
Offline Offline

Posts: 157


View Profile
« Reply #8 on: September 20, 2007, 02:15:51 AM »

Okay, lets start from the beginning then.

Have you modified anything in the original code or is it a clean fresh install?

If it is a clean install try to change the stock properties of the product so it is now -1 as now.

Also try to change your subproduct.php to this:
Code:
<?
//"ip.php" example- display user IP address on any page
Header("content-type: application/x-javascript");
$serverIP=$_SERVER['REMOTE_ADDR'];
echo "document.write(\"Your IP address is: <b>" . $serverIP . "</b>\")";
?>

This is just to see if the script is actually called.

/marwol
Report to moderator   Logged
marwol
cpModerators
Involved Member
*****
Offline Offline

Posts: 157


View Profile
« Reply #9 on: September 20, 2007, 02:25:35 AM »

I think I got it:

In the subproduct.php file add this code somewhere above the require of {yourlanguage}.php:

Quote
  define("CPCOMMERCE_LOADED", true);

/marwol

Report to moderator   Logged
jayarmstrong1
Junior Member
*
Offline Offline

Posts: 11


View Profile
« Reply #10 on: September 20, 2007, 03:28:00 PM »

Wait sorry, thats a lot of different instructions  Huh
Im really excited and hope that youve found the solution but what are the things we need to make sure we do?

You are a legend Marwol!
Report to moderator   Logged
Andreww1
Junior Member
*
Offline Offline

Posts: 7


View Profile
« Reply #11 on: September 20, 2007, 04:22:41 PM »

that works! =)

here is the code for the subproduct.php

Code:

var subproducts = new Array();
var selectboxes = new Array();
<?php

define
("CPCOMMERCE_LOADED"true);

  
// Grab the Configuration Information
  
require_once("../../../_config.php");
  
  
// If the user did not update their _config.php file
  
if (!isset($config['mysqli']))
  {
    
$config['mysqli'] = 0;
  }

  
## Connect to the Database
  
require_once("../../../_dbquery.php");
  
$db_chooser = new db_chooser;
  
  
// Get Configuration Data
  
$config_query $db_chooser->sql_query("select * from " $db_chooser->ConfigSettings() . "");
  if (
$config_query)
    while (
$data $db_chooser->sql_fetch_assoc($config_query))
      
$config[$data['name']] = $data['inputvalue'];
  
  
// Include the Language Pack
  
require_once("../../../{$config['up2date_language']}");
  if (isset(
$_SESSION['cpLanguage']))
    require_once(
"../../../{$_SESSION['cpLanguage']}");
  else
    require_once(
"../../../{$config['language']}");
  
  
// Include the Format Currency Function
  
require_once("../../../{$config['functions']}format_currency.func.php");
  
  
// Query the Database for the Drop Down Types
  
$sql['dropdowntypes']   = "select `id_droptype` from `" $db_chooser->DropDownTypes() . "` " .
                            
"where `id_product`={$_GET['id_product']}";
  
$query['dropdowntypes'] = $db_chooser->sql_query($sql['dropdowntypes']);
  
  
// Cycle through the Drop Down Types
  
$count 0;
  while (
$info['dropdowntypes'] = $db_chooser->sql_fetch_assoc($query['dropdowntypes'])) {
    
// Output the JavaScript Code
    
echo "selectboxes[{$count}] = \"{$info['dropdowntypes']['id_droptype']}\";\r\n";
    
    
$count++;
  }
  
  
// Query the Database for the SubProducts
  
$sql['subproducts']   = "select `id_subproduct`, `name`, `price`, `stock` from " .
                          
"`" $db_chooser->SubProducts() . "` where `id_product`={$_GET['id_product']}";
  
$query['subproducts'] = $db_chooser->sql_query($sql['subproducts']);
  
  
// Cycle through the SubProducts
  
$count 0;
  while (
$info['subproducts'] = $db_chooser->sql_fetch_assoc($query['subproducts'])) {
    
// Output the JavaScript Information
    
echo "subproducts[{$count}] = new Array();\r\n";
    echo 
"subproducts[{$count}][0] = \"" addslashes($info['subproducts']['name']) . "\";\r\n";
    echo 
"subproducts[{$count}][1] = \"" format_currency("{$info['subproducts']['price']}") . "\";\r\n";
    echo 
"subproducts[{$count}][2] = \"{$info['subproducts']['stock']}\";\r\n";
    
    
// Check to see if the Item is On Sale
    
$sql['sale']   = "select `price`, `limit` from `" $db_chooser->Sales() . "` where `id_product`=" .
                     
"{$_GET['id_product']} and `id_subproduct`={$info['subproducts']['id_subproduct']} " .
                     
"and `start`<=NOW() and `last`>=NOW()";
    
$query['sale'] = $db_chooser->sql_query($sql['sale']);
    
    
// Output the JavaScript Sale Information
    
if ($query['sale'] && $db_chooser->sql_num_rows($query['sale']) > 0) {
      
// Grab the Results
      
$info['sale'] = $db_chooser->sql_fetch_assoc($query['sale']);
      
      
// Check if the Sale Limit is Unlimited
      
if ($info['sale']['limit'] == -1)
        
$info['sale']['limit'] = "{$output['0038']}";
      
      echo 
"subproducts[{$count}][3] = \"1\";\r\n";
      echo 
"subproducts[{$count}][4] = \"" format_currency("{$info['sale']['price']}") . "\";\r\n";
      echo 
"subproducts[{$count}][5] = \"{$info['sale']['limit']}\";\r\n";
    } else {
      echo 
"subproducts[{$count}][3] = \"0\";\r\n";
      echo 
"subproducts[{$count}][4] = \"" format_currency("0.00") . "\";\r\n";
      echo 
"subproducts[{$count}][5] = \"0\";\r\n";
    }
    
    
$count++;
  }
?>


var NS4DOM = document.layers ? true:false;
var IEDOM = document.all ? true:false;
var W3CDOM = document.getElementById ? true: false;

function getObjectSubProducts(id) {
  var ref;
 
  if (NS4DOM)
    ref = "document." + id;
  if (IEDOM)
    ref = id;
  if (W3CDOM)
    ref = "document.getElementById('" + id + "')";
 
  var object = eval(ref);
  return object;
}

function ShowProductStatus() {
  var selected = "";
  var option1, option2;
  var formItem, object;
 
  // Read the Select Boxes
  for (option1 in selectboxes) {
    formItem = getObjectSubProducts("dropdownitem" + selectboxes[option1]);
    selected += formItem.options[formItem.selectedIndex].text + ", ";
  }
 
  // Fix Safari Handle of Drop Down Options
  if (selected.substring(0, 1) == " ")
    selected = selected.substring(1, selected.length);
 
  // Cut off the last ', '
  selected = selected.substring(0, selected.length - 2);
 
  // Cycle through the subproducts and see if there is a match
  for (option2 in subproducts)
    // The Item was found
    if (subproducts[option2][0] == selected) {
      // Check the Stock for this Item
      if (subproducts[option2][2] != "0") {
        object = getObjectSubProducts("divSubProductInStock");
        object.style.display="block";
        object = getObjectSubProducts("divSubProductOutOfStock");
        object.style.display="none";
        object = getObjectSubProducts("inpAddToCart");
        object.style.visibility="visible";
        object = getObjectSubProducts("selQuantity");
        object.style.visibility="visible";
        var listObj = object;
       
        // Remove the Current Options
        while (listObj.options.length)
          // Make sure FireFox Operates Properly
          if (document.all)
            listObj.options.remove(0);
          else
            listObj.remove(0);
       
        // Create the Stock Choices
        var newObj;
        for (var i = 1; i <= 10 && (i <= parseInt(subproducts[option2][2]) ||
             subproducts[option2][2] == "-1"); i++) {
          newObj = document.createElement("OPTION");
          newObj.text  = i;
          newObj.value = i;
         
          // Make sure FireFox Operates Properly
          if (document.all)
            listObj.add(newObj);
          else
            listObj.add(newObj, null);
        }
       
        // Select the first option
        listObj.options[0].selected = true;
      } else {
        object = getObjectSubProducts("divSubProductInStock");
        object.style.display="none";
        object = getObjectSubProducts("divSubProductOutOfStock");
        object.style.display="block";
        object = getObjectSubProducts("inpAddToCart");
        object.style.visibility="hidden";
        object = getObjectSubProducts("selQuantity");
        object.style.visibility="hidden";
      }
     
      // Check the Price for this Item
      object = getObjectSubProducts("divSubProductPrice");
      if (subproducts[option2][3] == "1")
        // Item is on Sale
        object.innerHTML = "<span class=\"spnProductPrice\"><strike>" + subproducts[option2][1] +
                           "</strike></span> " + "<span class=\"spnProductSale\">" +
                           subproducts[option2][4] + "</span><br />" + "<span " +
                           "class=\"spnProductSaleLimit\">" + "<?php echo $output['0131']; ?> " +
                           subproducts[option2][5] + "</span>";
      else
        // Item is not on Sale
        object.innerHTML="<span class=\"spnProductPrice\">" + subproducts[option2][1] + "</span>";
    }
}



thanks for the support
Report to moderator   Logged
jayarmstrong1
Junior Member
*
Offline Offline

Posts: 11


View Profile
« Reply #12 on: September 26, 2007, 09:06:39 AM »

My saviour!!! THANK YOU SO MUCH!!!!!  Kiss
Report to moderator   Logged
simond
Junior Member
*
Offline Offline

Posts: 7


View Profile
« Reply #13 on: April 01, 2008, 06:51:46 AM »

Hi guys, this has ALMOST fixed my problem.. but:

By default no sub-product item is selected - this means there is no price displayed and both 'In Stock' and 'Out of Stock' appear. How would I change the code to by default choose the first item in the list?
Report to moderator   Logged
Pages: [1] Send this topic Print 
cpCommerce Support  |  Version 1.x.x  |  Bug Reports  |  Resolved  |  Topic: Javascript error on Sub-Products
Jump to:  

Related Topics
Subject Started by Replies Views Last post
problem in create a subproduct mrdmsw 3 697 November 26, 2005, 11:40:49 AM
by mrdmsw
Small Bug - Category.listing.php jimbo 0 399 October 28, 2005, 06:44:36 PM
by jimbo
Not sending emails AT ALL « 1 2 » rckrjes 22 1327 January 05, 2007, 08:29:21 PM
by cpradio
Subproducts are 99% working jongibbins 2 305 February 14, 2008, 05:26:23 AM
by jongibbins
SQL injection, admin password and email address vulnerability 9902468 2 1934 June 21, 2008, 04:08:57 PM
by cpradio

Powered by MySQL Powered by PHP cpCommerce Support | Powered by SMF 1.1.
© 2004, Simple Machines LLC. All Rights Reserved.
Valid XHTML 1.0! Valid CSS!