SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    Pragmatic Programmer halfasleeps's Avatar
    Join Date
    Feb 2006
    Location
    Altoona, PA. USA
    Posts
    1,945
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    form with different actions?

    i have a form and i need to submit buttons one to add the product to the cart and one to add the product to a wishlist so when i make the form

    <form action="">

    depending on which button i click i need a different action


    how can i do this?



    THANKS
    Altoona Design
    Freelance Flex developer for hire.
    ActionScript Programmer with 8 Years Experience.

  2. #2
    bronze trophy
    Join Date
    Dec 2004
    Location
    Sweden
    Posts
    2,670
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can't do this with HTML4 without resorting to scripting.

    However, with Web Forms 2.0 (which is still a draft) you should be able to do something like this:
    HTML Code:
    <form>
     <input type="submit" action="http://example.org/foo" value="foo">
     <input type="submit" action="http://example.org/bar" value="bar">
    </form>
    This is supported by Opera 9.

    To fall back to HTML4 browsers you can do something like this:
    HTML Code:
    <form action="fallback.php">
     <input type="submit" action="http://example.org/foo" name="action" value="foo">
     <input type="submit" action="http://example.org/bar" name="action" value="bar">
    </form>
    PHP Code:
    <?php
    if ($_GET['action'] === 'foo')
       
    header('Location: http://example.org/foo?' $_SERVER['QUERY_STRING']);
    else if (
    $_GET['action'] === 'bar')
       
    header('Location: http://example.org/bar?' $_SERVER['QUERY_STRING']);
    ?>
    Simon Pieters

  3. #3
    Pragmatic Programmer halfasleeps's Avatar
    Join Date
    Feb 2006
    Location
    Altoona, PA. USA
    Posts
    1,945
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks for your reply....im kind of confused about what you suggested....
    hmm ....can i put a form inside a form???


    something like this?
    HTML Code:
    <form name="form1" action="http://example.org/add_to_cart">
    <select name="t_options[132]">
    <option value="374">$2495.00&nbsp;-&nbsp;1 carat</option>
    <option value="372">$3495.00&nbsp;-&nbsp;1.5 carat</option>
    <option value="373">$5500.00&nbsp;-&nbsp;2 carat</option>
    </select>
    <input type="submit" value="add to cart">
    <form name="form2" action="http://example.org/add_to_wish">
    <input type="submit" value="add to wishlist">
    </form>
    </form>

    and make form2 enherit everything from form1???
    so if you clicked on the either add to wish list or add to cart it sends all the options you selected.
    make since? ...is this possible?
    Altoona Design
    Freelance Flex developer for hire.
    ActionScript Programmer with 8 Years Experience.

  4. #4
    bronze trophy
    Join Date
    Dec 2004
    Location
    Sweden
    Posts
    2,670
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No, you can't, at least not with the text/html serialization. What is it you don't understand with my suggestion? The fallback method for dealing with HTML4 browsers?

    Another option would be to add name=""s to the submit buttons, and let the server figure out which button was pressed, like so:
    HTML Code:
    <form action="add-to">
     <input type="submit" name="where" value="add to cart">
     <input type="submit" name="where" value="add to wishlist">
    </form>
    And then something like this:
    PHP Code:
    <?php
     
    if ($_GET['where'] === 'add to cart') {
       
    // add to cart
     
    } else if ($_GET['where'] === 'add to wishlist') {
       
    // add to wishlist
     
    }
    ?>
    Simon Pieters

  5. #5
    Pragmatic Programmer halfasleeps's Avatar
    Join Date
    Feb 2006
    Location
    Altoona, PA. USA
    Posts
    1,945
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok what i dont understand is if i have this...

    HTML Code:
    <form action="fallback.php">
     <input type="submit" action="http://example.org/foo" name="action" value="foo">
     <input type="submit" action="http://example.org/bar" name="action" value="bar">
    </form>
    when a button is clicked is it going to take me to fallback.php or is it going to take me to the action specified in the submit?...

    i assume i put this in fallback.php
    PHP Code:
    <?php 
    if ($_GET['action'] === 'foo'
       
    header('Location: http://example.org/foo?' $_SERVER['QUERY_STRING']); 
    else if (
    $_GET['action'] === 'bar'
       
    header('Location: http://example.org/bar?' $_SERVER['QUERY_STRING']); 
    ?>
    but if it takes me to the action specified in the submit then why do i need this fallback.php at all? or if it takes me to fallback.php then why do i put the action in the submit button??

    im a lil confused.....sorry
    Altoona Design
    Freelance Flex developer for hire.
    ActionScript Programmer with 8 Years Experience.

  6. #6
    bronze trophy
    Join Date
    Dec 2004
    Location
    Sweden
    Posts
    2,670
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If the browser supports Web Forms 2.0 it will take you to the action specified in the submit button. If it doesn't then it will take you to the fallback action.

    If all browsers supported WF2 then you would only need the first example I provided, but we're not there yet.
    Simon Pieters

  7. #7
    Pragmatic Programmer halfasleeps's Avatar
    Join Date
    Feb 2006
    Location
    Altoona, PA. USA
    Posts
    1,945
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok i get it but i have one last question,

    in the example you showed me, any GET variables will be passed from fallback.php to the correct page but the form is POST (but theres still some get variables being passed as well), can you please tell me how to pass the POST variables as well??


    thanks
    Altoona Design
    Freelance Flex developer for hire.
    ActionScript Programmer with 8 Years Experience.

  8. #8
    bronze trophy
    Join Date
    Dec 2004
    Location
    Sweden
    Posts
    2,670
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You use $_POST[] instead of $_GET[].
    Simon Pieters

  9. #9
    Pragmatic Programmer halfasleeps's Avatar
    Join Date
    Feb 2006
    Location
    Altoona, PA. USA
    Posts
    1,945
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks for your help
    Altoona Design
    Freelance Flex developer for hire.
    ActionScript Programmer with 8 Years Experience.


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
  •