Search code examples
javascriptcoldfusioncoldfusion-2016

Passing an input from form into Coldfusion Query


Hello I am trying to pass a variable who's value would come from a form input to a Query to retrieve a value and pass back to the form as a hidden value.Very confusing, and I hope I am overthinking this. I am getting a Passed_Lot_Number is undefined error.

Here is the code I have so far:

<CFOUTPUT>
    <cfquery name = "OutputDetails" datasource = "#Application.PrimaryDataSource#">
         SELECT ShippingAdviceID
         FROM ShippingAdvice
         WHERE CustomerID =  #Passed_CustomerID#
         AND LotNumber = #Passed_Lot_Number#
    </cfquery>
        <td align="left" colspan="1">
        <input class="frm3" type="text" id="Outstanding_Passed_LotNumber" size="3" maxlength="6" tabindex="25">
          <form name="Show_SampleLogSheet" class="frm" action="/Buying/Shipping_Advice/Index.cfm" method="post">
            <input type="hidden" name="Passed_CustomerID" value="#Passed_CustomerID#">
            <input class="frm3" type="text" name="Passed_Lot_Number" size="3" maxlength="6" tabindex="25">
          </form>
        </td>
</CFOUTPUT>

Forgive me, this code is really old and I have been tasked to adding some more functionality to it. I really appreciate any help.

Thank you

Edit:

Here is some updated code:

<CFOUTPUT>
   <td align="left" colspan="1">
     <input class="frm3" type="text" id="Outstanding_Passed_LotNumber" size="3" maxlength="6" tabindex="25" style="background-color: ##838383;border:1px solid ##000000; color:white">
        <form name="Show_SampleLogSheet" class="frm" action="/Buying/Shipping_Advice/Index.cfm" method="post" style="display: inline">
          <input type="hidden" name="Passed_CustomerID" value="#Passed_CustomerID#">
          <input class="frm3" type="text" name="Passed_Lot_Number" size="3" maxlength="6" tabindex="25">
             <cfif structKeyExists(form, "Passed_Lot_Number ")>
                <cfquery name = "OutputDetails" datasource = "#Application.PrimaryDataSource#">
                    SELECT ShippingAdviceID
                    FROM tblShippingAdvice
                    WHERE CustomerID =  #Passed_CustomerID#
                    AND LotNumber = #Passed_Lot_Number#
                 <cfreturn Passed_ShippingAdviceID />
                </cfquery>
            </cfif>
        <input type="hidden" name="Passed_ShippingAdviceID" value="#Passed_ShippingAdviceID#">
      </form>
   </td>
</CFOUTPUT>

Solution

  • First of all, you have checked this condition structKeyExists(form, "Passed_Lot_Number "). This means, after submitting the form, the inside of the condition code will be executed.

    But, You have given code doesn't have to submit button. Please add the submit button.

    After submitting the form, we can able to get the form fields value like as below,

    form.Passed_CustomerID and form.Passed_Lot_Number
    

    And you must to put the <cfreturn Passed_ShippingAdviceID /> code after the <cfquery> tag.

    I have added a code. Hope, this will help.

    <cfoutput>
        <cfparam name="Passed_ShippingAdviceID" default="0">
        <cfif structKeyExists(form, "submit")>
            <cfquery name = "OutputDetails" datasource = "#Application.PrimaryDataSource#">
                SELECT ShippingAdviceID
                FROM tblShippingAdvice
                WHERE CustomerID =  "#form.Passed_CustomerID#"
                AND LotNumber = "#form.Passed_Lot_Number#"
            </cfquery>
            <cfset Passed_ShippingAdviceID = OutputDetails.ShippingAdviceID>
        </cfif>
        <form name="Show_SampleLogSheet" class="frm" action="/Buying/Shipping_Advice/Index.cfm" method="post" style="display: inline">
            <input class="frm3" type="text" id="Outstanding_Passed_LotNumber" size="3" maxlength="6" tabindex="25" style="background-color: ##838383;border:1px solid ##000000; color:white">
            <input type="hidden" name="Passed_CustomerID" value="#Passed_CustomerID#">
            <input class="frm3" type="text" name="Passed_Lot_Number" size="3" maxlength="6" tabindex="25">
            <input type="hidden" name="Passed_ShippingAdviceID" value="#Passed_ShippingAdviceID#">
            <input type="submit" value="submit" name="submit">
        </form>
    </cfoutput>
    

    Thanks,