• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

What am I missing? Why can't I multiply variables with Ajax?

New Here ,
Apr 17, 2016 Apr 17, 2016

Copy link to clipboard

Copied

Every individual that works with CF tells me how easy AJAX is to work with. But, I beg to differ. I am trying to get a simple product page to calculate products * quantity. I spent a good 12 hours scouring over the internet and never found the solution.
I have a page called shoppingCartForm.cfm that calls shoppingCartAction.cfm into play. On the shoppingCartAction.cfm is where I am trying to calculate the sub total. I can get the individual variables to output correctly, yet they will not cooperate when I try to coax them into multiply them. Maybe I should use a little positive affirmation and call them rabbits. As usual, any help would be appreciated, I don't have much hair left.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>

<head>

    <title>Your Order</title>

</head>

<body>

<cfif isDefined("Form.submit")>

    <cfparam name="Form.itemID" default="">

    <cfoutput>

        You have ordered the following items:<br>

        <br>

        <cfloop index="i" list="#Form.itemID#">

            ProductName: #Form["product_" & i]#<br>

            Product Code: #Form["sku_" & i]#<br>

            Quantity: #Form["qty_" & i]#<br>

            Price: #form["price_" & i]#<br>

            Sub: #Form["price_" & i]# * #Form["qty_" & i]#

            <br>

        </cfloop>

    </cfoutput>

</cfif>

</body>

</html>

Page code for the page that call up the above page:

<html>

<head>

    <title>Shopping Cart</title>

</head>

<cfscript>

CartItems=2;

Cart = ArrayNew(1);

for ( i=1; i LE cartItems; i=i+1)

{

    Cart = StructNew();

    Cart.ID = i;

    Cart.Name = "Product " & i;

    Cart.SKU = i*100+(2*i*10)+(3*i);

    Cart.Qty = 3*i-2;

    Cart.Price = 10;

}

</cfscript>

<body>

Your shopping cart has the following items.<br>

You can change your order quantities.<br>

If you don't want any item, clear the item's check box.<br>

When you are ready to order, click submit.<br>

<br>

<cfform name="ShoppingCart" action="ShoppingCartAction.cfm" method="post">

<table>

    <tr>

    <td>Order?</td>

    <td>Product</td>

    <td>Code</td>

    <td>Quantity</td>

    <td>Price</td>

    <td>Sub Total</td>

    </tr>

    <cfset index ={}>

    <cfloop index="i" from="1" to="#cartItems#">

      

        <tr>

        <cfset productName= "product_" & Cart.ID>

        <cfset skuName= "sku_" & Cart.ID>

        <cfset qtyname= "qty_" & Cart.ID>

        <cfset pricename= "price_" & Cart.id>

      

        <td><cfinput type="checkbox" name="itemID" value="#Cart.ID#" checked>

             </td>

        <td><cfinput type="text" name="#productName#" value="#Cart.Name#" passThrough = "readonly = 'True'"></td>

        <td><cfinput type="text" name="#skuName#" value="#Cart.SKU#" passThrough = "readonly = 'True'"></td>

        <td><cfinput type="text" name="#qtyName#" value="#Cart.Qty#"></td>

        <td><cfinput type="text" name="#priceName#" value="#Cart.Price#"></td>

        </tr>

</cfloop>

    <tr>

        <td align=right><input type="submit" name="submit" value="submit"></td>

    </tr>  

      

</table>

</cfform>

</body>

</html>

TOPICS
Database access

Views

449

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Apr 21, 2016 Apr 21, 2016

Copy link to clipboard

Copied

Firstly, your code has nothing to do with AJAX, so clear your head of all things AJAX in this scenario. You are dealing with plain ColdFusion code.

Change your problem line to:

Sub: #Form["price_" & i] * Form["qty_" & i]#

Your problem was that you were outputting the value of each of the form variables separated by the text " * ", instead of outputting the result of a calculation.

Cheers

Eddie

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 24, 2016 Apr 24, 2016

Copy link to clipboard

Copied

rickclark54 wrote:

Sub: #Form["price_" & i]# * #Form["qty_" & i]#

Just another point to consider:

<cfif isNumeric(form["price_" & i]) and isNumeric(form["qty_" & i])>

<!--- Put EddieLotter's suggestion here --->

<cfelse>

<!--- Not a number--->

NaN

</cfif>

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Apr 25, 2016 Apr 25, 2016

Copy link to clipboard

Copied

LATEST

Yes, indeed. Take BKBK's point to heart. Always validate data before using them.

Cheers

Eddie

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation