cinolas Posted October 18, 2017 Share Posted October 18, 2017 Hi, Thanks for reading :) I'm trying to add a small JS based calculator to the product_info page so that users can calculate their shoe size from a few input fields directly on the page. I'm not exactly fluent in php, or JS, but I'm usually successful with copy-pasting bits of code and replicating syntax. I duplicated the also_purchased_products.php as a starting point and replaced the code with my JS and form divs. The form grabs a few pieces of data from the user, mixes it with data from the product and sets a result input field without refreshing the page: document.getElementById('result').value = "Error: size mismatch, please contact customer service"; In order to pass some product data to the JS I use this: <?php $size_bias = tep_db_query("select products_size_bias from " . TABLE_PRODUCTS . " where products_id = '" . (int)$HTTP_GET_VARS['products_id'] . "'"); ?> and this inside the JS: var sizebias = <?php echo $size_bias ?>; The resulting product_info page seems to be cut short around these php echo at the beginning of the JS and the rest doesn't even make it on the resulting page. Any idea why? Is this not a good way to pass variables? Anything else I'm doing wrong? Thanks! Quote Link to comment Share on other sites More sharing options...
frankl Posted October 19, 2017 Share Posted October 19, 2017 (edited) @cinolas Try moving the semicolon to the end of the echo statement, like this: var sizebias = <?php echo $size_bias; ?> Edited October 19, 2017 by frankl Quote osCommerce user since 2003! Link to comment Share on other sites More sharing options...
♥bruyndoncx Posted October 19, 2017 Share Posted October 19, 2017 (edited) You are missing code to fetch the result array Googling tep_db_query has this nice tutorial on mysql, and the oscommerce equivalent code required http://www.oscommerce-template-easy.com/basic-php-mysql-tutorial-for-oscommerce/mysql-database-query.html HTH Edited October 19, 2017 by bruyndoncx Quote KEEP CALM AND CARRY ON I do not use the responsive bootstrap version since i coded my responsive version earlier, but i have bought every 28d of code package to support burts effort and keep this forum alive (albeit more like on life support). So if you are still here ? What are you waiting for ?! Find the most frequent unique errors to fix: grep "PHP" php_error_log.txt | sed "s/^.* PHP/PHP/g" |grep "line" |sort | uniq -c | sort -r > counterrors.txt Link to comment Share on other sites More sharing options...
cinolas Posted October 19, 2017 Author Share Posted October 19, 2017 (edited) Thanks for the help! @frankl So I need a semicolon both to close the php line AND one to close the JS line, like so, correct? var sizebias = <?php echo $size_bias; ?>; I found the syntax I used here: Passing PHP Variables to JavaScript and it didn't mention a semicolon inside the php. @bruyndoncx I believe I have the necessary code to fetch the data I need. My includes file looks like this: <?php if (isset($HTTP_GET_VARS['products_id'])) { $is_skates = tep_db_query("select products_is_skates from " . TABLE_PRODUCTS . " where products_id = '" . (int)$HTTP_GET_VARS['products_id'] . "'"); $available_in_half_sizes = tep_db_query("select products_available_in_half_sizes from " . TABLE_PRODUCTS . " where products_id = '" . (int)$HTTP_GET_VARS['products_id'] . "'"); $size_bias = tep_db_query("select products_size_bias from " . TABLE_PRODUCTS . " where products_id = '" . (int)$HTTP_GET_VARS['products_id'] . "'"); $narrow_appropriate = tep_db_query("select products_narrow_feet_appropriate from " . TABLE_PRODUCTS . " where products_id = '" . (int)$HTTP_GET_VARS['products_id'] . "'"); $slightly_narrow_appropriate = tep_db_query("select products_medium_narrow_feet_appropriate from " . TABLE_PRODUCTS . " where products_id = '" . (int)$HTTP_GET_VARS['products_id'] . "'"); $regular_appropriate = tep_db_query("select products_regular_feet_appropriate from " . TABLE_PRODUCTS . " where products_id = '" . (int)$HTTP_GET_VARS['products_id'] . "'"); $slightly_wide_appropriate = tep_db_query("select products_medium_wide_feet_appropriate from " . TABLE_PRODUCTS . " where products_id = '" . (int)$HTTP_GET_VARS['products_id'] . "'"); $wide_appropriate = tep_db_query("select products_wide_feet_appropriate from " . TABLE_PRODUCTS . " where products_id = '" . (int)$HTTP_GET_VARS['products_id'] . "'"); if ($is_skates = 1) { ?> <script type="text/javascript"> function sizewizard(){ var availablehalfsizes = "<?php echo $available_in_half_size; ?>"; var sizebias = "<?php echo $size_bias; ?>"; var sizebias = Number(sizebias); var narrowappropriate = "<?php echo $narrow_appropriate; ?>"; var slightlynarrowappropriate = "<?php echo $slightly_narrow_appropriate; ?>"; var regularappropriate = "<?php echo $regular_appropriate; ?>"; var slightlywideappropriate = "<?php echo $slightly_wide_appropriate; ?>"; var wideappropriate = "<?php echo $wide_appropriate; ?>"; var ssize = document.getElementById('ssize').value; ... but when I call the include on my product_info page (right before the product_available) it ends up cutting in the middle of the JS like this: <script type="text/javascript"> function sizewizard(){ var availablehalfsizes = ""; var sizebias = "</script> </div? Weird how the first variable I try to pass fails as blank, then the second one just causes the script to be truncated right there... Edited October 19, 2017 by cinolas Quote Link to comment Share on other sites More sharing options...
♥bruyndoncx Posted October 19, 2017 Share Posted October 19, 2017 noop you dont tep_db_fetch_array is needed even if you just return one field in one row, it is still an array needing parsing Quote KEEP CALM AND CARRY ON I do not use the responsive bootstrap version since i coded my responsive version earlier, but i have bought every 28d of code package to support burts effort and keep this forum alive (albeit more like on life support). So if you are still here ? What are you waiting for ?! Find the most frequent unique errors to fix: grep "PHP" php_error_log.txt | sed "s/^.* PHP/PHP/g" |grep "line" |sort | uniq -c | sort -r > counterrors.txt Link to comment Share on other sites More sharing options...
cinolas Posted October 20, 2017 Author Share Posted October 20, 2017 @bruyndoncx right! That was it, along with a few hundred JS syntax errors (I never do JS). It's almost working now, I just need to debug the JS some more. THANK YOU SIR! Quote Link to comment Share on other sites More sharing options...
♥bruyndoncx Posted October 20, 2017 Share Posted October 20, 2017 Quote KEEP CALM AND CARRY ON I do not use the responsive bootstrap version since i coded my responsive version earlier, but i have bought every 28d of code package to support burts effort and keep this forum alive (albeit more like on life support). So if you are still here ? What are you waiting for ?! Find the most frequent unique errors to fix: grep "PHP" php_error_log.txt | sed "s/^.* PHP/PHP/g" |grep "line" |sort | uniq -c | sort -r > counterrors.txt Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.