Page 1 of 1

trying to implement a javascript call in my php code

Posted: Wed Mar 28, 2007 1:48 pm
by PastorHank
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I have an order form I am trying to get to auto calculate the total sales price based on quantity.  I am trying to use a javascript function to calculate the total priced based on the quantity entered.

I have this in [syntax="html"]
<head>
<meta http-equiv="Content-Language" content="en-us">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Order Page - Bloodlines of the Legends</title>
 <script>
 function total()
  {
   var qty= document.form1.quantity.value;
   var price=document.form1.price.value;
   var total=qty*pxeach;
   document.form1.totalprice.value=total;
   }
</script>

</head>
in my form I have this[/syntax]

Code: Select all

echo "<p align='center'>Quantity <input type='text' name='quantity' size='20' value=$quantity_ordered></p>";
/* I seed $quantity_ordered with a default value of 1 */

echo "<p align='center'>&nbsp;Price Each<input type='text' name='pxeach' size='20' value=$price_each></p>";
/* I pass the price each with the form that calls this form */

echo "<p align='center'>Total <input type='text' name='totalprice' size='20' onFocus='total()' value=$total_price ></p>";
The "onFocus='total()' isn't changing my total price. I know I'm missing something, could someone please point me in the right direction.

thank you


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Wed Mar 28, 2007 2:51 pm
by feyd
The values of the fields are strings. Javascript may not understand how to multiply strings, I can't remember offhand.

Posted: Wed Mar 28, 2007 3:00 pm
by RobertGonzalez
The event only fires when focus is brought to that form field. Do you have Firebug for Firefox installed on your machine. Or the Web Developer extension? Both would give you detailed information as to what is happening in the javascript calls.

Posted: Wed Mar 28, 2007 3:05 pm
by RobertGonzalez
Oh, and your javascript is using variables that don't exist.

Code: Select all

function total()
{
    var qty= document.form1.quantity.value; // This exists in the form
    var price=document.form1.price.value; // This however, doesn't. There is not field names price
    var total=qty*pxeach; // You are now multiplying with a null value
    document.form1.totalprice.value=total;
}
Try this (it worked for me)

Code: Select all

<html>
<head>
<meta http-equiv="Content-Language" content="en-us">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Order Page - Bloodlines of the Legends</title>
 <script>
function getTotal()
{
   var f = document.form1;
   var qty = f.quantity.value;
   var price = f.price.value;
   var total = qty * price;
   document.form1.totalprice.value=total;
}
</script>
</head>

<body>
<form name="form1">
<p align='center'>Quantity <input type='text' name='quantity' size='20' value="2" /></p>
<p align='center'>&nbsp;Price Each<input type='text' name='price' size='20' value="20" /></p>
<p align='center'>Total <input type='text' name='totalprice' size='20' onFocus='getTotal()' /></p>
</form>
</body>
</html>

Posted: Wed Mar 28, 2007 3:57 pm
by PastorHank
It was the varialbe names....thanks to everyone who replied....

It also dawned on me after I got it to work, that all I really had to do was add a field to the page that called the order form and I could skip a step....oh well,

Thanks again