Jump to content
  • Checkout
  • Login
  • Get in touch

osCommerce

The e-commerce.

Adding a small javascript form calculator to product_info.php


cinolas

Recommended Posts

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!

 

Link to comment
Share on other sites

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 by bruyndoncx

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

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 by cinolas
Link to comment
Share on other sites

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 

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

:thumbsup:

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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...