Search code examples
javascripthtmlcoldfusion

Looking to take Quantity from a text input and use OnClick="location.href" to push information to new page


Currently I am using this to submit a quantity of 1 product to my cart page with a simple Onclick add to cart button.

<td class="data_cell dataFontColor">
                <label onClick="location.href='cart/?PartNo=#NPart#&Qty=1'">Add to 
Cart</label></td>
            <td class="data_cell dataFontColor">
                 <cfif IsDefined ("getPrice2.Price") AND getPrice2.Price eq "">

I am trying to implement a text box to submit any quantity the user wants, but on submit the page moves but nothing is added to cart. I know it's something wrong with the Qty= in the OnClick, but can't seem to figure it out. Any help would be appreciated!

<td class="data_cell dataFontColor">
            <td><input type="text" name="Qty" size="4" value= "Qty" maxlength="9"/></td>
            <td class="data_cell dataFontColor">
            <label onClick="location.href='shop/?PartNo=#NPart#&Qty=#Trim (Qty)#'">Add to 
Cart</label></td>

Solution

  • The reason this is not working is because you can't do what you're attempting using ColdFusion which processes the page server-side. User typed inputs are processed client-side.

    First, I would add an id attribute to your input tag.

    <input type="text" id="Qty" name="Qty" size="4" value= "Qty" maxlength="9"/>
    

    Once your input has an ID, now you can now extract the value client-side using JavaScript.

    <label onClick="location.href='shop/?PartNo=#NPart#&Qty="+document.getElementById("Qty").value.trim()>Add to Cart</label>