Jump to content
  • Checkout
  • Login
  • Get in touch

osCommerce

The e-commerce.

Why won't this javascript work?


Aragon127

Recommended Posts

I thought this was simple enough that even I couldn't screw it up, but I guess i'm wrong. What I'm trying to do here is update a price(I don't wanna do refreshes, so i'm using a seperate javascript function). I'm taking it one step at a time, and right now am just trying to change the text form with "onchange" on the select, but I get an error: window.document.formprice.currentprice is null or not an object. Can anyone tell me where I screwed it up? I'm sure it's something simple, but I just don't see it. Code's below. TIA

 

In <head>

<script language="JavaScript">

function updateprice()

{

window.document.formprice.currentprice.value = "value has changed"

}

</script>

//no longer in </head>

.

.

.

.

later...

.

.

<form name="formprice">

<input type="text" name="currentprice"

value="Current Price">

</form>

.

.

.

little bit later....

.

.

.

echo '<tr><td class="main">' . $products_options_name_values['products_options_name'] . ': </td><td>' . "n" . '<select name ="id[' . $products_options_name_values['products_options_id'] . ']"onchange="updateprice()">' . "n";

Link to comment
Share on other sites

I'm no Javascript expert but I think the window. is the problem, lose it so you just have

 

document.formprice.currentprice.value

Trust me, I'm an Accountant.

Link to comment
Share on other sites

Try moving the Javascript to the bottom of the page. At the time it's parsing the js, it hasn't yet seen the form...

 

Just a thought.

Link to comment
Share on other sites

Nice try, but still doesn't work. I think it's just a case of the PHP and Javascript conflicting. It's probably an issue of the variables no longer being in force. I think i'm gonna try something a little different and use pure PHP with a refresh on the OCHANGE command. Now, I just have to figure out how to make the server keep the option selections.....Thanks for the try btw.

Link to comment
Share on other sites

:idea: Well, it must be a stupid comment but ... try removing the space from the value="Current Price" bit. I have found that value statements cannot have free text in some situations.

Ian-san

Flawlessnet

Link to comment
Share on other sites

I doubt it's a conflict between PHP and JS. Also, the value should be able to hold any text...

 

Could you create a dumbed down version of the problem and post it so we can run it independently?

 

Thx,

Link to comment
Share on other sites

What I put was as "dumbed down" as I can get it. It's just a simple modification to the product_info.php page. Here's my entire php page, thanks for the help btw:

 

<?php

/*

$Id: product_info.php,v 1.71 2001/11/22 21:48:42 dgw_ Exp $

 

The Exchange Project - Community Made Shopping!

http://www.theexchangeproject.org

 

Copyright © 2000,2001 The Exchange Project

 

Released under the GNU General Public License

*/

 

require('includes/application_top.php');

 

require(DIR_WS_LANGUAGES . $language . '/' . FILENAME_PRODUCT_INFO);

 

$location = '';

?>

<html>

<head>

<title><?php echo TITLE; ?></title>

<base href="<?php echo (getenv('HTTPS') == 'on' ? HTTPS_SERVER : HTTP_SERVER) . DIR_WS_CATALOG; ?>">

<?

$stylesheet_sql = tep_db_query("select stylesheet from text_conf where id='1'");

$stylesheet_sql = tep_db_fetch_array($stylesheet_sql);

?>

<link rel="stylesheet" type="text/css" href="stylesheets/<? echo $stylesheet_sql['stylesheet']; ?>">

<script language="javascript"><!--

function popupImageWindow(url) {

window.open(url,'popupImageWindow','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizab

le=yes,copyhistory=no,width=100,height=100,screenX=150,screenY=150,top=150,left=1

50')

}

//--></script>

</head>

<body marginwidth="0" marginheight="0" topmargin="0" bottommargin="0" leftmargin="0" rightmargin="0">

<!-- header //-->

<?php require(DIR_WS_INCLUDES . 'header.php'); ?>

<!-- header_eof //-->

 

<!-- body //-->

<table border="0" width="100%" cellspacing="5" cellpadding="5">

<tr>

<td width="<?php echo BOX_WIDTH; ?>" valign="top"><table border="0" width="<?php echo BOX_WIDTH; ?>" cellspacing="0" cellpadding="0">

<tr>

<td><table border="0" width="100%" cellspacing="0" cellpadding="2">

<!-- left_navigation //-->

<?php require(DIR_WS_INCLUDES . 'column_left.php'); ?>

<!-- left_navigation_eof //-->

</table></td>

</tr>

</table></td>

<!-- body_text //-->

<td width="100%" valign="top"><table border="0" width="100%" cellspacing="0" cellpadding="0">

<tr>

<td width="100%"><table border="0" width="100%" cellspacing="0" cellpadding="2" class="topBarTitle">

<tr>

<td width="100%" class="topBarTitle"> <?php echo TOP_BAR_TITLE; ?> </td>

</tr>

</table></td>

</tr>

<?php

$product_info = tep_db_query("select p.products_id, pd.products_name, pd.products_description, p.products_model, p.products_quantity, p.products_image, pd.products_url, p.products_price, p.products_date_added, p.products_date_available, p.manufacturers_id from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_DESCRIPTION . " pd where p.products_id = '" . $HTTP_GET_VARS['products_id'] . "' and pd.products_id = '" . $HTTP_GET_VARS['products_id'] . "' and pd.language_id = '" . $languages_id . "'");

if (!tep_db_num_rows($product_info)) { // product not found in database

?>

<tr>

<td class="main"><br> <?php echo TEXT_PRODUCT_NOT_FOUND; ?> </td>

</tr>

<tr>

<td><br><?php echo tep_black_line(); ?></td>

</tr>

<tr>

<td align="right"><br><a href="<?php echo tep_href_link(FILENAME_DEFAULT, '', 'NONSSL'); ?>"><?php echo tep_image_button('button_main_menu.gif', IMAGE_BUTTON_MAIN_MENU); ?></a></td>

</tr>

<?php

} else {

tep_db_query("update " . TABLE_PRODUCTS_DESCRIPTION . " set products_viewed = products_viewed+1 where products_id = '" . $HTTP_GET_VARS['products_id'] . "' and language_id = '" . $languages_id . "'");

$product_info_values = tep_db_fetch_array($product_info);

 

$check_special = tep_db_query("select specials_new_products_price from " . TABLE_SPECIALS . " where products_id = '" . $product_info_values['products_id'] . "'");

if (tep_db_num_rows($check_special)) {

$check_special_values = tep_db_fetch_array($check_special);

$new_price = $check_special_values['specials_new_products_price'];

}

if ($new_price) {

$products_price = '<s>' . $currencies->format($product_info_values['products_price']) . '</s> <span class="productSpecialPrice">' . $currencies->format($new_price) . '</span>';

} else {

$products_price = $currencies->format($product_info_values['products_price']);

}

$products_attributes = tep_db_query("select popt.products_options_name from " . TABLE_PRODUCTS_OPTIONS . " popt, " . TABLE_PRODUCTS_ATTRIBUTES . " patrib where patrib.products_id='" . $HTTP_GET_VARS['products_id'] . "' and patrib.options_id = popt.products_options_id and popt.language_id = '" . $languages_id . "'");

if (tep_db_num_rows($products_attributes)) {

$products_attributes = '1';

} else {

$products_attributes = '0';

}

?>

<tr>

<td width="100%"><table border="0" width="100%" cellspacing="0" cellpadding="2">

<tr height="40">

<td class="pageHeading"> <?php echo $product_info_values['products_name']; ?> </td>

<td align="right" class="pageHeading"> <?php echo $products_price; ?> </td>

</tr>

<?php

if (PRODUCT_LIST_MODEL) {

echo ' <tr>' . "n" .

' <td colspan="2" class="pageHeading"> ' . $product_info_values['products_model'] . ' </td>' . "n" .

' </tr>' . "n";

}

?>

</table></td>

</tr>

<tr>

<td><?php echo tep_black_line(); ?></td>

</tr></table>

<form name="cart_quantity" method="post" action="<?php echo tep_href_link(FILENAME_PRODUCT_INFO, tep_get_all_get_params(array('action')) . 'action=add_product', 'NONSSL'); ?>">

<table border="0" width="100%" cellspacing="0" cellpadding="0">

<tr>

<td><table border="0" width="100%">

<tr>

<td class="main"><table border="0" cellspacing="0" cellpadding="2" align="right">

<tr>

<td class="main"><a href="java script:popupImageWindow('<?php echo tep_href_link(FILENAME_POPUP_IMAGE, 'pID=' . $product_info_values['products_id']); ?>')"><?php echo tep_image($product_info_values['products_image'], $product_info_values['products_name'], SMALL_IMAGE_WIDTH, SMALL_IMAGE_HEIGHT, 'align="right" hspace="5" vspace="5"'); ?></a></td>

</tr>

<tr>

<td align="center" class="smallText"><a href="java script:popupImageWindow('<?php echo tep_href_link(FILENAME_POPUP_IMAGE, 'pID=' . $product_info_values['products_id']); ?>')"><?php echo TEXT_CLICK_TO_ENLARGE; ?></a></td>

</tr>

</table><p><?php echo stripslashes($product_info_values['products_description']); ?></p>

 

<form name="formprice">

<input type="text" name="currentprice" value="price">

</form>

<?php

if ($products_attributes == '1') {

$products_options_name = tep_db_query("select distinct popt.products_options_id, popt.products_options_name from " . TABLE_PRODUCTS_OPTIONS . " popt, " . TABLE_PRODUCTS_ATTRIBUTES . " patrib where patrib.products_id='" . $HTTP_GET_VARS['products_id'] . "' and patrib.options_id = popt.products_options_id and popt.language_id = '" . $languages_id . "'");

echo '<b>' . TEXT_PRODUCT_OPTIONS . '</b><br>';

echo '<table border="0" cellpading="0" cellspacing"0">';

while ($products_options_name_values = tep_db_fetch_array($products_options_name)) {

$selected = 0;

$products_options = tep_db_query("select pov.products_options_values_id, pov.products_options_values_name, pa.options_values_price, pa.price_prefix from " . TABLE_PRODUCTS_ATTRIBUTES . " pa, " . TABLE_PRODUCTS_OPTIONS_VALUES . " pov where pa.products_id = '" . $HTTP_GET_VARS['products_id'] . "' and pa.options_id = '" . $products_options_name_values['products_options_id'] . "' and pa.options_values_id = pov.products_options_values_id and pov.language_id = '" . $languages_id . "'");

 

//heres an attempted change

if ($products_options_name_values['products_options_name'] == "Processor") { print ("Processor goes here");}

//end of attempt

 

echo '<tr><td class="main">' . $products_options_name_values['products_options_name'] . ': </td><td>' . "n" . '<select name ="id[' . $products_options_name_values['products_options_id'] . ']"onchange="updateprice()">"' . "n";

while ($products_options_values = tep_db_fetch_array($products_options)) {

echo "n" . '<option name="' . $products_options_name_values['products_options_name'] . '" value="' . $products_options_values['products_options_values_id'] . '"';

if ( ($products_options_values['options_values_price'] == 0 && $selected == 0) || ($cart->contents[$HTTP_GET_VARS['products_id']]['attributes'][$products_options_name_values['products_options_id']] == $products_options_values['products_options_values_id'])) {

$selected = 1;

echo ' SELECTED';

}

echo '>' . $products_options_values['products_options_values_name'];

if ($products_options_values['options_values_price'] != '0') {

echo ' (' . $products_options_values['price_prefix'] . $currencies->format($products_options_values['options_values_price']) .') ';

}

echo '</option>';

};

echo '</select></td></tr>';

}

echo '</table>';

}

?>

</td></tr></table></td>

</tr>

<?php

$reviews = tep_db_query("select count(*) as count from " . TABLE_REVIEWS . " where products_id = '" . $HTTP_GET_VARS['products_id'] . "'");

$reviews_values = tep_db_fetch_array($reviews);

 

if ($reviews_values['count'] > 0) {

?>

<tr>

<td class="main"><br><?php echo TEXT_CURRENT_REVIEWS . ' ' . $reviews_values['count']; ?><br> </td>

</tr>

<?php

}

 

if ($product_info_values['products_url']) {

?>

<tr>

<td class="main"><?php echo sprintf(TEXT_MORE_INFORMATION, tep_href_link(FILENAME_REDIRECT, 'action=url&goto=' . urlencode($product_info_values['products_url']), 'NONSSL', true, false)); ?><br> </td>

</tr>

<?php

}

 

if ($product_info_values['products_date_available'] > date('Y-m-d H:i:s')) {

?>

<tr>

<td align="center" class="smallText"><?php /*echo sprintf(TEXT_DATE_AVAILABLE, tep_date_long($product_info_values['products_date_available'])); */?></td>

</tr>

<?php

} else {

?>

<tr>

<td align="center" class="smallText"><?php/* echo sprintf(TEXT_DATE_ADDED, tep_date_long($product_info_values['products_date_added'])); */?></td>

</tr>

<?php

}

?>

<tr>

<td><br><?php echo tep_black_line(); ?></td>

</tr>

<tr>

<td><br><table border="0" width="100%" cellspacing="0" cellpadding="0">

<tr>

<?php

$get_params = substr(tep_get_all_get_params(), 0, -1);

echo ' <td class="main"> <a href="' . tep_href_link(FILENAME_PRODUCT_REVIEWS, $get_params, 'NONSSL') . '">' . tep_image_button('button_reviews.gif', IMAGE_BUTTON_REVIEWS) . '</a></td>' . "n" .

' <td align="right" class="main"><input type="hidden" name="products_id" value="' . $product_info_values['products_id'] . '">' . tep_image_submit('button_in_cart.gif', IMAGE_BUTTON_IN_CART) . ' </td>' . "n";

?>

</tr>

</table></td>

</tr>

<?php

if ( (USE_CACHE == 'true') && !SID) {

echo tep_cache_also_purchased(3600);

} else {

include(DIR_WS_MODULES . FILENAME_ALSO_PURCHASED_PRODUCTS);

}

}

?>

</table></form></td>

<!-- body_text_eof //-->

<td width="<?php echo BOX_WIDTH; ?>" valign="top"><table border="0" width="<?php echo BOX_WIDTH; ?>" cellspacing="0" cellpadding="0">

<tr>

<td><table border="0" width="100%" cellspacing="0" cellpadding="2">

<!-- right_navigation //-->

<?php require(DIR_WS_INCLUDES . 'column_right.php'); ?>

<!-- right_navigation_eof //-->

</table></td>

</tr>

</table></td>

</tr>

</table>

<script language="JavaScript">

function updateprice()

{

document.formprice.currentprice.value="new price";

}

</script>

<!-- body_eof //-->

 

<!-- footer //-->

<?php require(DIR_WS_INCLUDES . 'footer.php'); ?>

<!-- footer_eof //-->

<br>

</body>

</html>

<?php require(DIR_WS_INCLUDES . 'application_bottom.php'); ?>

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...