trying to implement a javascript call in my php code

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
PastorHank
Forum Contributor
Posts: 117
Joined: Sat Jun 03, 2006 7:58 am
Location: Texas Hill Country

trying to implement a javascript call in my php code

Post 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]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

The values of the fields are strings. Javascript may not understand how to multiply strings, I can't remember offhand.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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>
PastorHank
Forum Contributor
Posts: 117
Joined: Sat Jun 03, 2006 7:58 am
Location: Texas Hill Country

Post 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
Post Reply