-1

Here is my js fiddle and the text i been trying to escape so far the php code i tried to escape these characters

 $datas[] = ['attributes' => [$colorId => ['id' => $child->getColor(),
                                    'name' => $product->getName(),
                                    'productId' => $product->getId(),
                                    'price' => number_format($product->getSpecialPrice(),2),
                                    'rrp' => number_format($product->getMsrp(), 2),
                                    'was' => number_format($product->getPrice(), 2),
                                    'image' =>  Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA) . 'catalog/product' . $child->getImage(),
                                    'size' => $child->getSize(),
                                    'description' =>Mage::helper('core')->quoteEscape(htmlspecialchars(json_encode($product->getDescription()), ENT_COMPAT, 'UTF-8', false)),

                                    // Mage::helper('core')->quoteEscape($product->getDescription()),
                                    'promo' => Mage::helper('core')->quoteEscape($product->getProductpromobox()),
                                    'sku' => $product->getSku(),
                                    'netsuite' => $product->getInternalId()
                                    ],

                                    $sizeId => ['id' => $child->getSize(),
                                    'name' => $product->getName(),
                                    'productId' => $product->getId(),
                                    'price' => number_format($product->getSpecialPrice(),2),
                                    'rrp' => number_format($product->getMsrp(), 2),
                                    'was' => number_format($product->getPrice(), 2),
                                    'image' =>  Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA) . 'catalog/product' . $child->getImage(),
                                    'color' => $child->getColor(),
                                    'description' => Mage::helper('core')->quoteEscape(json_encode(utf8_encode($product->getDescription()), ENT_COMPAT, 'UTF-8', false)),

                                    // Mage::helper('core')->quoteEscape($product->getDescription()),
                                    'promo' => Mage::helper('core')->quoteEscape($product->getProductpromobox()),
                                    'sku' => $product->getSku(),
                                     'netsuite' => $product->getInternalId()]

                                    ]];

In my javascript file i did was

var productConfig = '<?php echo json_encode($datas);?>';

so this is the php code so error is only at description key while parsing https://jsfiddle.net/tsjfkwto/

ujwal dhakal
  • 2,289
  • 2
  • 30
  • 50

1 Answers1

1

Don't include the 's:

var productConfig = <?php echo json_encode($datas);?>;
// No ' here -------^--------------------------------^

json_encode outputs a valid JavaScript value for the right-hand side of an assignment (because JSON is a subset of JavaScript literal syntax). If you're encoding a simple string, it will have appropriate quotes; if you're encoding an object structure, it will have the correct object structure (which shouldn't be in quotes).

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875