Skip to main content
Inspiring
January 23, 2012
Answered

1 form with 2 buttons to go to 2 different pages

  • January 23, 2012
  • 3 replies
  • 4802 views

Hi. I have 1 form I'm using on a page for adding some information. I have an Add button at the bottom that goes to an action page that inserts the data into an Access Database and displays what was inserted. I want to use the same form, but I want to have another button that goes to a different page, but still inserts the data into the database. How do I do this? Below is what I have for the beginning of the form and the button I click on to add it to the database. I won't put all the stuff inside the form so I keep this concise.

<cfform name="AddECNumber" action="add_new_ec_number_action.cfm" method="post">

<input type="button" value="Add" onclick="verify();">

Andy

    This topic has been closed for replies.
    Correct answer BKBK

    jamie61880 wrote:

    Hi. I have 1 form I'm using on a page for adding some information. I have an Add button at the bottom that goes to an action page that inserts the data into an Access Database and displays what was inserted. I want to use the same form, but I want to have another button that goes to a different page, but still inserts the data into the database. How do I do this? Below is what I have for the beginning of the form and the button I click on to add it to the database. I won't put all the stuff inside the form so I keep this concise.

    <cfform name="AddECNumber" action="add_new_ec_number_action.cfm" method="post">

    <input type="button" value="Add" onclick="verify();">

    <script type="text/javascript">

    function verify()

    {

       document.myform.action ="add_new_ec_number_action.cfm";

       document.myform.submit();

    }

    function sendIt()

    {

       document.myform.action ="some_other_action_page.cfm";

       document.myform.submit();

    }

    </script>

    <cfform name="myform" method="post">

    <cfinput type="text" name="txt"><br>

    <cfinput type="button" name="submitBtn" onclick="verify()" value="Add"><br>

    <cfinput type="button" name="submitBtn" onclick="sendIt()" value="Insert">

    </cfform>

    3 replies

    BKBK
    Community Expert
    BKBKCommunity ExpertCorrect answer
    Community Expert
    January 24, 2012

    jamie61880 wrote:

    Hi. I have 1 form I'm using on a page for adding some information. I have an Add button at the bottom that goes to an action page that inserts the data into an Access Database and displays what was inserted. I want to use the same form, but I want to have another button that goes to a different page, but still inserts the data into the database. How do I do this? Below is what I have for the beginning of the form and the button I click on to add it to the database. I won't put all the stuff inside the form so I keep this concise.

    <cfform name="AddECNumber" action="add_new_ec_number_action.cfm" method="post">

    <input type="button" value="Add" onclick="verify();">

    <script type="text/javascript">

    function verify()

    {

       document.myform.action ="add_new_ec_number_action.cfm";

       document.myform.submit();

    }

    function sendIt()

    {

       document.myform.action ="some_other_action_page.cfm";

       document.myform.submit();

    }

    </script>

    <cfform name="myform" method="post">

    <cfinput type="text" name="txt"><br>

    <cfinput type="button" name="submitBtn" onclick="verify()" value="Add"><br>

    <cfinput type="button" name="submitBtn" onclick="sendIt()" value="Insert">

    </cfform>

    Inspiring
    January 24, 2012

    BKBK,

         Thank you. This works great! I do have one more question though, now. Once the 2nd button is clicked, I would like it to go to an existing page I already have set up. This page is called "add_item_existing_eco.cfm". Normally, to get to this page, the user would have clicked a link to get here. Then this page just adds an item to the ECO.

         But now, I want the page with the form I talked about above to go to this page also, once the form is submitted. So I could add the Inserting code to the "add_item_existing_eco.cfm" page and display the info, but how am I going to prevent the info. from when a link is clicked, to not insert more data.

        I would need an If Statement that says if the "Add new Item Button" was clicked from the Add ECO page, then just insert the data and display the info. Elseif the info. came from a link, then just display the data. How can I say If the "Add new Item Button" was clicked from the Add ECO page? Does this make sense? Or should I just create a new page for when this "Add new Item Button" is clicked?

    Andy

    BKBK
    Community Expert
    Community Expert
    January 25, 2012

    jamie61880 wrote:

    BKBK,

         Thank you. This works great! I do have one more question though, now. Once the 2nd button is clicked, I would like it to go to an existing page I already have set up. This page is called "add_item_existing_eco.cfm". Normally, to get to this page, the user would have clicked a link to get here. Then this page just adds an item to the ECO.

         But now, I want the page with the form I talked about above to go to this page also, once the form is submitted. So I could add the Inserting code to the "add_item_existing_eco.cfm" page and display the info, but how am I going to prevent the info. from when a link is clicked, to not insert more data.

        I would need an If Statement that says if the "Add new Item Button" was clicked from the Add ECO page, then just insert the data and display the info. Elseif the info. came from a link, then just display the data. How can I say If the "Add new Item Button" was clicked from the Add ECO page? Does this make sense? Or should I just create a new page for when this "Add new Item Button" is clicked?

    Yes, the question is clear. There are 2 possible scenarios:

    1) From now onwards, an insert will only be done by means of the form button. Coming to the action page by means of a link will no longer lead to an insert.

    Form page:

    <script type="text/javascript">

    function verify()

    {

       document.myform.action ="add_new_ec_number_action.cfm";

       document.myform.submit();

    }

    function sendIt()

    {

       document.myform.action ="add_item_existing_eco.cfm";

       document.myform.submit();

    }

    </script>

    <cfform name="myform" method="post">

    <cfinput type="text" name="txt"><br>

    <cfinput type="button" name="submitBtn" onclick="verify()" value="Add"><br>

    <cfinput type="button" name="submitBtn" onclick="sendIt()" value="Insert">

    <cfinput type="hidden" name="isUpdated" value="yes">

    </cfform>

    Page add_item_existing_eco.cfm:

    <cfif isDefined("form.isUpdated") and form.isUpdated>

    <!--- Insert the data --->       

    </cfif>

    <!--- Display the info --->

    2) An insert can be triggered by the link or by the form-button, which ever arrived first at the page dd_item_existing_eco.cfm.

    I do believe the best solution in this scenario is to use the database. Start, for example, by determining which column in the database table can be used to verify that an insert has occurred. A datetime column is ideal for this purpose.

    Then add a query, like the following, to verify whether an insert has already occurred in a specific time.

    <!--- Number of inserts done in the past 6 hours (Assumption: doneDate registers datetime of each insert) --->

    <cfquery name="verifyInsert" datasource="myDSN">

        select count(*) as numberOfInserts

        from myTBL

        where doneDate > #createODBCDatetime(dateAdd("h",-6,now()))#

    </cfquery>

    <cfif verifyInsert.numberOfInserts EQ 0><!--- No insert has yet occurred --->

    <!--- Code to do insert --->

    </cfif>

    <!--- In all cases, display info --->

    adamaas
    Known Participant
    January 24, 2012

    Normally it would be easier to put all ur codes in one page.

    If u do it, then the code would appear as such:

    <cfif IsDefined("form.nameofbutton")>

    /*

    ur coding for add in database here

    */

    <cflocation url="http:///"> <!---Here if there is no problem in ur addition in database, this will re-direct ur actual page to another page u mention in the url--->

    </cfif>

    Like u've said, u want it concise,

    then put this code <cflocation> after the addition code.

    When u click on the button, this will check for js first, then this will insert into ur database and then redirect u to the page u want.

    Hope it helps u to advance.

    Inspiring
    January 23, 2012

    Instead of two buttons, consider a dropdown or radio buttons.  It will be easier to code and probably clearer to your users.

    If you take that route, insert the record and base your display on what option the user chose.