Skip to main content
Inspiring
November 6, 2007
Question

Multiple buttons for same action page

  • November 6, 2007
  • 7 replies
  • 1808 views
I have a cfform with an action page.

I have two buttons at the bottom with different labels, like button 1 and button 2, that call this action page to insert data. After the insert, the page needs to be redirected page 1 if button 1 was selected, and redirected to page 2 if button 2 was selected.

How can I separate the two so that they go to different forms ?

I tried passing a variable in the cfform action, but it applies to both buttons, thus both go to the same place, and I cannot seem to separate.
    This topic has been closed for replies.

    7 replies

    Inspiring
    November 9, 2007
    As a follow up to JR "Bob" Dobbs
    Using type="submit" rather than type="button" makes sure that just the clicked on button gets submitted so you can test for it.
    If you still want to do client side validation, make sure your validateForm function returns true if all validates and false if not and then call it with onsubmit in the form tag.
    <form ... onsubmit = "return validateForm(this)">
    Inspiring
    November 6, 2007
    Show us the form tag with the "onsubmit" variable.



    Second, if you want to do validation, it would likely be faster and easier to simply use CFFORM and all the related CFFORM tags and then it will do validation for you since you don't have anything compliccated in your validations.

    trojnfnAuthor
    Inspiring
    November 6, 2007
    Here is a partial of my original javascript. I have to check for required fields, numerics, and some other stuff, so I put in in the javascript edit. But you are right, the edit is easier in the cfinput tag.

    <script language="javascript">

    function validateForm(pack_slip_form){

    var returnStatus = true;


    if (pack_slip_form.supplier_name.value == ""){
    alert("Please Enter The Supplier Name");
    pack_slip_form.supplier_name.focus();
    returnStatus(false);
    }


    if (returnStatus){
    pack_slip_form.submit();
    }

    returnStatus(true);
    }

    This is my form tag, from the other example :
    <cfform action="pack_slip_action.cfm method="post" name="pack_slip_form" onSubmit="return ValidateForm()">

    And here are my two buttons below :

    <input type="submit" name="button_1" value="SAVE AND ADD PACK SLIP" onClick="validateForm(document.pack_slip_form)">

    <input type="submit" name="button_2" value="ASSIGN TO PROCUREMENT" onClick="validateForm(document.pack_slip_form)">
    Inspiring
    November 7, 2007
    I agree with SafariTECH, take a look at using cfform instead of using your own scripts. Its pretty good for creating basic client side javascript validation.
    Note that you should always have server side validation because client side javascript can be bypassed.

    http://livedocs.adobe.com/coldfusion/8/htmldocs/help.html?content=Tags_f_13.html#2433198
    http://livedocs.adobe.com/coldfusion/8/htmldocs/help.html?content=Tags_i_07.html#1100379
    http://livedocs.adobe.com/coldfusion/8/htmldocs/help.html?content=validateData_01.html
    Inspiring
    November 6, 2007
    are you submitting the form with methd = GET or method = POST ?

    If you are using "GET" then there will not be a form scope to evaluate, which is why it is doing nothing.

    make sure the form tag on the first page excplicitly says method="post"
    Inspiring
    November 6, 2007
    You have to give the buttons different names - you can't use the same name if you want to differentiate them from each other.

    Then just use Dobbs's code and substitute the new names where he has "button1" and "button2"
    Inspiring
    November 6, 2007
    You have to disable the relocation if you want to see the CFDUMP, but Dobbs has the right idea you need to follow.

    @jdeline, the .x and .y annotations only occur if you use an image as a button, not on standard form buttons.
    trojnfnAuthor
    Inspiring
    November 6, 2007
    here are my buttons that I renamed and my action page below. it cannot find the buttons, nothging happens, screen is just blank

    ><input type="button" name="button_1" value="SAVE AND ADD PACK SLIP" onClick="validateForm(document.pack_slip_form")>
    <input type="button" name="button_2" value="ASSIGN TO PROCUREMENT" onClick="validateForm(document.pack_slip_form)">

    <cfif isDefined("form.button_1")>
    <cflocation url="upload_file.cfm?urdn_number=#form.urdn_number#&upload_first_time_flag=yes&user_site=#form.user_site#&user_id=#form.user_id#&priority=#form.priority#">
    <cfelseif isDefined("form.button_2")>
    <cflocation url="assign_urdn.cfm?urdn_number=#form.urdn_number#&priority=#form.priority#">
    </cfif>
    Inspiring
    November 6, 2007
    Possible issues:

    1.Your onclick is invalid onClick="validateForm(document.pack_slip_form")
    move the closing double quotes;

    2.Is your validateForm javascript function causing a problem.

    3. If neither of the above fixes things post all your code so we can see the whole process.
    Inspiring
    November 6, 2007
    Edit: fixed missing paranthesis
    trojnfnAuthor
    Inspiring
    November 6, 2007
    Here is my code for the two buttons that call the same action page. How does this translate to your example regarding form.button1, etc. ?

    <input type="button" name="submit_button" value="SAVE AND ADD PACK SLIP" onClick="validateForm(document.pack_slip_form)">

    <input type="button" name="submit_button" value="ASSIGN TO PROCUREMENT" onClick="validateForm(document.pack_slip_form)">
    Inspiring
    November 6, 2007
    See below.
    November 6, 2007
    Place <CFDUMP VAR=#form#> at the top of your action page. You will see the button name with a .x and .y suffix. You can use this value to point you to the proper redirection.
    trojnfnAuthor
    Inspiring
    November 6, 2007
    I added <CFDUMP VAR=#form#> to the top of the action page like you suggested and nothing happens, did not see anything. The action page does its insert and then does the cfloation url to direct to the next page.