SitePoint Sponsor

User Tag List

Results 1 to 7 of 7

Hybrid View

  1. #1
    SitePoint Enthusiast
    Join Date
    Sep 2010
    Posts
    43
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Managing multiple forms with submit buttons

    Hello and Good Morning.

    I'm working on a project where I have several forms each containing submit buttons.

    At the moment, I've defined all the submit buttons with name="Action" and when any given button is clicked, the formHandler.asp page only runs a segment of code if, for example, Action = "Save User". Here's a visual of what I mean:

    HTML Code:
    <form name="form1" id="form1" method="post" action="formHandler.asp">
         <input type="submit" name="Action" value="Save User" />
    </form>
    <form name="form2" id="form2" method="post" action="formHandler.asp">
         <input type="submit" name="Action" value="Save Category" />
    </form>
    PHP Code:
    Dim Action
    Action 
    Request.Form("Action")

    If 
    Action "Save User" Then
        
    Do stuff here
    End 
    If
    If 
    Action "Save Category" Then
        
    Do stuff here
    End 
    If 
    SO, I'm wondering if there's another way I can do this. How do I call a function when a button is clicked/form is submitted instead of the above example? The reason I want to do it differently is because I want to use input type="image" instead and I can't give that a value.

  2. #2
    SitePoint Enthusiast
    Join Date
    Sep 2010
    Posts
    43
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Lightbulb

    I've actually found a way to do this:

    Instead of calling a function for each form that is submitted, I've come to learn that the values passed from input type="image" are the x and y coordinates. Having said that, if the button is not pressed, the coordinate = "".

    Each time I submit a form I can check to see which of the button's values are not an empty string and do the code needed.

    Here's a visual representation:
    note: I also ran into a problem where I had 2 buttons per form (update, delete). This is also fixed with this code

    HTML Code:
    <form name="form1" id="form1" method="post" action="formHandler.asp">
        <input type="image" src="imgs/pic1.gif" name="saveUser" id="saveUser" />
        <input type="image" src="imgs/pic2.gif" name="deleteUser" id="deleteUser" />
    </form>
    <form name="form2" id="form2" method="post" action="formHandler.asp">
        <input type="image" src="imgs/pic3.gif" name="saveCategory" id="saveCategory" />
        <input type="image" src="imgs/pic4.gif" name="deleteCategory" id="deleteCategory" />
    </form>
    PHP Code:
    Dim userSaveuserDelete
    'The .x gets the value of the x coordinate
    userSave = Request.Form("userSave.x")
    userDelete = Request.Form("userDelete.x")

    If userSave <> "" Then
         Response.Write "User Saved"
    End If
    If userDelete <> "" Then
         Response.Write "User Deleted"
    End If 
    So, regardless of how many buttons and forms I have, I can also see which button is pressed.

  3. #3
    SitePoint Wizard bronze trophy
    Join Date
    Oct 2001
    Location
    Vancouver BC Canada
    Posts
    2,030
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    That coordinate idea is pretty cool but what if someone fills in a field and then pushes the enter button to submit the form. Are you able to trap the information to determine which form was submitted?

    I always use a hidden action input or when I'm doing a catalog a hidden id input to keep things simple. Checking the action or id on the server side the way you outlined in your post keeps it easy. I use Select Case statements to determine how the flow of the program will proceed depending on action. ie:

    Code:
    SELECT CASE action
         CASE "insert":
             'Code to insert a record
         CASE "update":
             'Code to update a record
         CASE "delete":
             'Delete the record
         CASE else:
             'A default action in case the page is opened without an action variable...
    END SELECT
    Andrew Wasson | www.lunadesign.org
    Principal / Internet Development

  4. #4
    SitePoint Enthusiast
    Join Date
    Sep 2010
    Posts
    43
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thumbs up

    Yes that's also a great idea however the main problem I was having was that I don't want regular buttons but instead want images for buttons.

    The way I described above with the coordinates would work in the exact same way as a regular form as far as obtaining the information from the input fields is concerned. it's a simple Request.Form("inputName").

    I will say that I do enjoy the Case statement. I find it nice and neat and it's great for situations where you're checking if the same variable is defined differently.

    Thanks!

  5. #5
    SitePoint Wizard bronze trophy
    Join Date
    Oct 2001
    Location
    Vancouver BC Canada
    Posts
    2,030
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    Do you want images for fields or just to submit the forms?

    HTML Code:
    <input type="image" src="image.gif" alt="Submit button">
    Andrew Wasson | www.lunadesign.org
    Principal / Internet Development

  6. #6
    SitePoint Enthusiast
    Join Date
    Sep 2010
    Posts
    43
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry, in my first example I left out the text input fields. But yes, I was just looking for the image submit button.

  7. #7
    SitePoint Wizard bronze trophy
    Join Date
    Oct 2001
    Location
    Vancouver BC Canada
    Posts
    2,030
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by sd_javascript View Post
    Sorry, in my first example I left out the text input fields. But yes, I was just looking for the image submit button.
    Oh, ok... Did you try the <input type="image" src="some-image.gif" alt="submit"> method?
    Andrew Wasson | www.lunadesign.org
    Principal / Internet Development


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •