SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  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)

    change the action="" inside a form??

    im really going crazy trying to create something that seems so simple

    i have a form and it has 2 buttons one is "add to cart" and the other is "add to wishlist" depending on which button you click all the info needs to be sent to a different page.

    so is there a way i can change the value of action="" inside the form tag when i click on each submit button?

    if they click add to wish lists the form would need to be
    HTML Code:
    <form name="form1" action="process_wishlist.php" method="post">
    and if they click add to cart it would need to be
    HTML Code:
    <form name="form1" action="process_add_to_cart.php" method="post">
    so is there a way that i can use javascript so that when they click a button is changes the action to the correct path and then submits it???

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

  2. #2
    SitePoint Wizard chris_fuel's Avatar
    Join Date
    May 2006
    Location
    Ventura, CA
    Posts
    2,750
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This sounds a bit awkward. What's the difference in process_wishlist.php and process_ad_to_cart.php? The table they end up in?

  3. #3
    SitePoint Wizard chris_fuel's Avatar
    Join Date
    May 2006
    Location
    Ventura, CA
    Posts
    2,750
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    and for code:

    PHP Code:
    <?php
    if($_POST['submit'])
    {
        print 
    "submit";
    }
    // Not required, done to show verbosely what's going on
    else if ($_POST['submit2'])
    {
        print 
    "submit2";
    }
    exit;
    ?>
    <html>
    <head>
    <title>Form Test</title>
    </head>
    <body>
    <form action='<?php echo $_SERVER['PHP_SELF']; ?>' method="POST">
    <td colspan="2"><input type="submit" name="submit" /></td>
    <td colspan="2"><input type="submit" name="submit2" /></td>
    </form>
    </body>
    </html>
    By naming the buttons differently, PHP will process them in the manner desribed above. So if you name your buttons "wishlist_add" and "cart_add", you could do:

    PHP Code:
    if($_POST['wishlist_add'])
    {
        
    //add it to the wishlist
    }
    else
    {
        
    //add it to the cart instead

    and take the appropriate actions accordingly.

  4. #4
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    64 Post(s)
    Tagged
    0 Thread(s)
    You can use the onclick method of each of the buttons to access (and change) the action attribute of the form (document.getElementById('myFormId').action). Can't you?

  5. #5
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    <form blah blah blah>
    <input type="submit" value="Add to Wishlist" onclick="this.form.action='process_wishlist.php';"/>
    <input type="submit" value="Add to Cart" onclick="this.form.action='process_add_to_cart.php';"/>
    </form>

  6. #6
    SitePoint Wizard chris_fuel's Avatar
    Join Date
    May 2006
    Location
    Ventura, CA
    Posts
    2,750
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    True, you could do that, but I think it's something that should stay server side because you can realistically do it that way without much pain, making it odd to do the same thing with something that can easily be disabled.

  7. #7
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I agree 100%

  8. #8
    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 alot guys...i will definantly remember for next time. i Actually went ahead and change my process_add_to_cart.php and process_wishlist.php to just be one single page and do different things depending on Submit=""
    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
  •