douxianwu2221 2016-11-10 06:28
浏览 40
已采纳

解析变量从php到javascript的问题

Scenario: For tracking events(Add to cart, Search terms, Product views, etc) from a test e-commerce website(Opencart), JavaScript function is written in the footer page and these functions are called at button triggers or page loads.

Problem: While tracking product view event following code is added to Product page, function captureProductView() is called from footer, and parameters clientid, fname, lname are retrieved from getClientDetails();

<script>
window.onload=function(){
     getClientDetails();
     captureProductView('<?php echo $product['product_id']; ?>',
            '<?php echo $product['name']; ?>', '<?php echo $product['price']; ?>',
            clientid, fname, lname);
};
</script>

But the values from php variables $product['product_id'], $product['name'], $product['price'] cannot be parsed to the script tag, leaving Notice:

Notice : Undefined variable: product in /home/path.../product.tpl on line 29"

for all those three variables

I tried assigning these values to javaScript variables, which also did not work.

var p_id = "<?php echo $product['product_id']; ?>";
var p_name = "<?php echo $product['name']; ?>";

Is there a way where I can parse the php variables to the script tag, so that they can be passed as parameters to the function?

  • 写回答

1条回答 默认 最新

  • dongzhila3786 2016-11-10 09:39
    关注

    You get an error because the associative array $product is not defined in the PHP script that renders the page.

    Your first option is to edit the script so that $product['product_id'], $product['name'], $product['price'] are defined and assigned with the proper values.

    Note that your first snippet of code have a syntax error as you're mis-using the single quote. You have to use both single and double quotes:

     captureProductView('<?php echo $product["product_id"]; ?>',
                        '<?php echo $product["name"]; ?>', 
                        '<?php echo $product["price"]; ?>',
                        clientid, fname, lname);
    

    You have a second option that is parsing with JavaScript the values from the rendered page.

     captureProductView( $('input[name=product_id]').val(),
                         $('h1').text().trim(),
                         $('.price').text().trim(),
                        clientid, fname, lname);
    

    This worked on a layout I tried but may need minor changes on a different layout to target the appropriate page elements to fetch the values from.

    Note I used jQuery to retrieve the value from the page elements.

    The value for price is a string like "Price: $24" and depending on your needs may require further basic string manipulation to extract only the price value (24 in the example).

    For reference I tried the above on this page:

    http://themes.webiz.mu/opencart/apparel/index.php?route=product/product&product_id=69

    However I strongly discourage this approach because as the layout changes the function may break.


    All the above two options assume that captureProductView is properly sending the data to the server with an AJAX request and clientid, fname, lname are defined in the JavaScript code.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。