Latest News: (loading..)
cinolas

Adding a small javascript form calculator to product_info.php

7 posts in this topic

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!

 

Share this post


Link to post
Share on other sites

@cinolas

Try moving the semicolon to the end of the echo statement, like this:

var sizebias = <?php echo $size_bias; ?>

 

Edited by frankl

Share this post


Link to post
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

Share this post


Link to post
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

Share this post


Link to post
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 

Share this post


Link to post
Share on other sites

@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!

Share this post


Link to post
Share on other sites

:thumbsup:

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now