Welcome Guest, Not a member yet? Register   Sign In
Getting Errors with JavaScript from Controller
#1

[eluser]RMinor[/eluser]
I have a question regarding some JavaScript that I am echoing from my controller. I am retrieving products from my controller and echoing them out as an array because the Ajax loading of new products was taking too long before. Now, what has happened is when my client enters a description for a product that has commas or line breaks in it, the entire JavaScript output is wrong and none of the JavaScript on that page works. I was wondering if there is a better way to echo this JavaScript out so that I would be able to escape the output of commas or line breaks which I believe is the cause of the JavaScript not working.

Controller:
Code:
public function index()
{
    $data['page'] = $this->Global_model->pageInformation($this->_page);
    $data['initial'] = $this->Weekly_model->getInitial();
    // Begin pagination code
    $this->load->library('pagination');
    $config['base_url'] = base_url() . 'nail_of_the_week/index/';        
    $config['total_rows'] = $this->Weekly_model->getTotalRows();
    $config['per_page'] = 12;
    $config['uri_segment'] = 3;
    $this->pagination->initialize($config);
    $data['pagination'] = $this->pagination->create_links();
    $data['products'] = $this->Weekly_model->getProducts($this->pagination->per_page, (int)$this->uri->segment(3));
    // Begin looping through all products and putting them into an array to quicken up the product change rate
    $data['products_js'] = "var products = Array();\n";
    foreach ($data['products'] as $product) {
        $data['products_js'] .= "products[" .
        $product['product_id'] . "] = {id: '" .
        $product['product_id'] . "', photo: '" .
        $product['product_photo'] . "', name: '" .
        $product['product_name'] . "', description: '" .
        $product['product_description'] . "', sku: '"  .
        $product['product_sku'] . "', price: '" .
        $product['product_price'] . "', notw_date: '"  .
        $product['product_date'] . "', notw_description: '" .
        $product['product_notw_description'] . "', notw_headline: '" .
        $product['product_notw_headline'] ."'};\n";
    }
    // Load the category view.
    $this->load->view('weekly-nail_view', $data);
}

View:
Code:
<!DOCTYPE html>
&lt;html lang="en"&gt;
&lt;head&gt;
    &lt;title&gt;&lt;?php echo $page['page_title']; ?&gt;&lt;/title&gt;
    &lt;meta charset="utf-8"&gt;
    &lt;meta name="keywords" content="&lt;?php echo $page['page_meta_keywords']; ?&gt;"/&gt;
    &lt;meta name="description" content="&lt;?php echo $page['page_meta_description']; ?&gt;"/&gt;
    &lt;link rel="stylesheet" type="text/css" href="&lt;?php echo base_url(); ?&gt;css/style.css" /&gt;
    &lt;link rel="stylesheet" type="text/css" href="http://fonts.googleapis.com/css?family=Questrial|Anton" /&gt;
    [removed][removed]
    [removed][removed]
    [removed]
    $(document).ready(function(){
        $('#merchandiseSlider img').hide();
        function merchandiseSlider() {
            $("#merchandiseSlider img").first().appendTo('#merchandiseSlider').hide();
            $("#merchandiseSlider img").first().show();
            setTimeout(merchandiseSlider, 10000);
        }

        merchandiseSlider();

        &lt;?php echo $products_js; ?&gt;

        $("#addToCart input[name='add']").live('click', function(event) {
            event.preventDefault();
            var form = $('#addToCart');
            $.ajax({
                type: 'POST',
                url: form.attr('action'),
                dataType: 'json',
                data: form.serialize(),
                success: function(html) {
                    if (html.message == 'TRUE') {                
                        alert('Item added to shopping cart.');
                    } else {
                        alert('Error, item not added.');
                    }
                }
            });
        });

        function get_record_id(record_id) {
            $(".product-photo").attr('src', '&lt;?php echo base_url();?&gt;/products/' + products[record_id].photo);
            $(".product-name strong").text(products[record_id].name);
            $(".product-description").text(products[record_id].description);
            $(".product-sku").text(products[record_id].sku);
            $(".product-price").text('$' + products[record_id].price);
            $(".product-notw-description").text(products[record_id].notw_description);
            $(".product-notw-date").text(products[record_id].notw_date);
            $(".product-notw-headline").text(products[record_id].notw_headline);

            $("#id").val(record_id);
            $("#name").val(products[record_id].name);
            $("#price").val(products[record_id].price);
        }

        $("a.get_record").live('click', function(event){
            event.preventDefault();
            $("#addToCart :submit").attr('disabled', 'disabled');
            get_record_id($(this).attr('rel'));
            $("#addToCart :submit").removeAttr('disabled');
        });
    });
    [removed]
&lt;/head&gt;




Theme © iAndrew 2016 - Forum software by © MyBB