SitePoint Sponsor

User Tag List

Results 1 to 15 of 15

Thread: Two submit buttons, One form

  1. #1
    SitePoint Enthusiast Red Blaze's Avatar
    Join Date
    Feb 2006
    Location
    Texas
    Posts
    94
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Two submit buttons, One form

    Hi, I'm having an issue with a form that I'm sure it can be fixed with PHP.

    I want one button to refresh the content of the site, so it can make calculations in the form. The other button, I want it to take the user to the next step.

    I'm not sure if "Nesting" forms will help? I tried that, but just by looking at the code of a "nested form", it didn't make sense.

    I tried looking around google, and searching the forums, but most of them only do actions, such as changes in the database, or echoes. None had the information I was looking for.

    I did try this:

    PHP Code:
    <?php
    if($_POST['update']){
    $page '';
    }elseif(
    $_POST['continue']{
    $page 'step2.php';
    }
    The action starts out empty.
    PHP Code:
    action="<?php echo $page?>"
    I click update, it works fine. Updates the calculations. And the action attribute is still empty. As the if statements says.
    But I click on continue, it refreshes the page, with the action attribute value of step2.php. I click on either button, update or continue, they both go to step2.php.

    I would use header(step2.php), but wouldn't my form variables become empty?

    I appreciate the help. Thank you.
    ~RB

  2. #2
    SitePoint Addict greg76's Avatar
    Join Date
    Aug 2004
    Location
    Los Feliz, CA
    Posts
    256
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    try with placing your buttons outside <form> and </form> tags and run some javascript on them, eg.

    Code:
    <input type="submit" name="Submit" value="Go to 2nd Step" onClick = "document.FormName.submit()">
    and
    Code:
    <input type="submit" name="Submit" value="Edit" onClick="window.location.href = 'http://www.whateverHere.com'"">
    you might need to change the type from 'submit' to 'button'

    Let me know if it worked!
    G.

  3. #3
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,513
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    This seems to work:

    test1.php:
    PHP Code:
    <?php

    if( isset($_GET['updtBtn']) )
    {
        
    $calc_result $_GET['valA'] * 2;
    }
    elseif( isset(
    $_GET['contBtn']) )
    {
        
    $query_str =  "valA=$_GET[valA]";
        
    header("Location: test2.php?$query_str");
    }
    else
    {
        
    $calc_result "";
    }


    ?>

    <html>
    <head><title></title>
    </head>
    <body>

    <div>test1.php</div>

    <form name="f" method="get" action="test1.php">

    <div>calculation:
    <input type="text" name="valA" value="<?php echo $calc_result;?>"/>
    </div>

    <input type="submit" name="contBtn" value="continue"/>
    <input type="submit" name="updtBtn" value="update"/>
    </form>

    </body>
    </html>
    test2.php
    PHP Code:
    <html>
    <head><title></title>
    </head>

    <body>
    <div>test2.php</div>

    <?php
    echo $_GET['valA'];
    ?>

    </body>
    </html>
    Edit:

    changed some variable names to make it clearer
    Last edited by 7stud; Mar 23, 2006 at 15:37.

  4. #4
    SitePoint Enthusiast Red Blaze's Avatar
    Join Date
    Feb 2006
    Location
    Texas
    Posts
    94
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you guys. I'll try these out ASAP and return with results. =)

  5. #5
    SitePoint Enthusiast Red Blaze's Avatar
    Join Date
    Feb 2006
    Location
    Texas
    Posts
    94
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, I'm currently trying Greg's technique. It almost worked. The "Go to 2nd Step" button works, however, the edit button just erases all the form variables from the previous page. See, this is my structure:

    Choose items by check boxes => Choose quantities of items (step 1 starts here) => Customer shipping information (step 2) => etc

    @ 7stud:
    I'm not using GET functions, just POST, for security reasons. I didn't want any information displayed on the address.

  6. #6
    Sean N Pixel Inception's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA
    Posts
    280
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This will work for you.

    PHP Code:
    <form action="test.php" method="post">
        <input type="submit" name="edit" value="Edit">
        <input type="submit" name="submit" value="Submit">
    </form>
    <?php
    $edit 
    $_POST['edit'];
    $submit $_POST['submit'];
    ?>
    Now just make an if statement if(!empty($edit)){ etc.
    Sean @ Pixel Inception, Inc. www.pixelinception.net
    Web Design & Web Development

  7. #7
    SitePoint Enthusiast Red Blaze's Avatar
    Join Date
    Feb 2006
    Location
    Texas
    Posts
    94
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It does direct it to step2.php, and with the right variables. But when I want to update step 1 with the edit/update button, it use to removes everything, now it just doesn't do anything.

    Here, this is what I'm talking about.

    The code should already be in the text box. Click login, click view album, select some photos, click place order.

    If you click "Go to Step 2", it works. Click on Update Price, it does nothing.

    As for your solution, Pixel Inception, !empty means Not Empty, right?

  8. #8
    SitePoint Enthusiast Red Blaze's Avatar
    Join Date
    Feb 2006
    Location
    Texas
    Posts
    94
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I figured it out. I guess I never gave javascript a chance. I searched google again but this time for a javascript rather than PHP and found this site:

    http://www.rgagnon.com/jsdetails/js-0018.html

    Thank you guys for the tips. Really appreciate it. I don't know what to give back to this community but a donation or something.

    Considering how much I ask for help just shows me how much I lack in PHP knowledge.

  9. #9
    Sean N Pixel Inception's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA
    Posts
    280
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The code will only work if javascript is enabled. It is a small minority that doesnt have it enabled but if they dont your order form wont work. And yes !empty means not empty.
    Sean @ Pixel Inception, Inc. www.pixelinception.net
    Web Design & Web Development

  10. #10
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,513
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I figured it out. I guess I never gave javascript a chance.
    Roughly 10% of all users have js enabled, and if users followed MS's recommendations to disable js, that would jump to 90%. That's alright as long as your page degrades gracefully when the user doesn't have js enabled. For instance, you would need to provide a default page in the action attribute, so the form would work for non-js users.

    @ 7stud:
    I'm not using GET functions, just POST, for security reasons. I didn't want any information displayed on the address.
    Well, save the value in a session variable instead:

    test1.php:
    Code:
    <?php 
    session_start();
    
    if( isset($_POST['updtBtn']) )
    {
        $calc_result = $_POST['valA'] * 2;
    }
    elseif( isset($_POST['contBtn']) )
    {
    	$_SESSION['valA'] = $_POST['valA'];
    	header("Location: test2.php");
    }
    else
    {
        $calc_result = "";
    }
    
    ?>
    
    <html>
    <head><title></title>
    </head>
    <body>
    
    <div>test1.php</div>
    
    <form name="f" method="post" action="test1.php">
    
    <div>calculation:
    <input type="text" name="valA" value="<?php echo $calc_result;?>"/>
    </div>
    
    <input type="submit" name="contBtn" value="continue"/>
    <input type="submit" name="updtBtn" value="update"/>
    </form>
    
    </body>
    </html>
    test2.php:
    Code:
    <?php
    session_start();
    ?>
    
    <html>
    <head><title></title>
    </head>
    
    <body>
    <div>test2.php</div>
    
    <?php
    echo $_SESSION['valA'];
    ?>
    
    </body>
    </html>
    Last edited by 7stud; Mar 24, 2006 at 14:39.

  11. #11
    Sean N Pixel Inception's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA
    Posts
    280
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by 7stud
    Roughly 10% of all users have js enabled
    Where did you get those numbers? Its better not to use JS when you dont need to, like using window.location instead of header("Location.
    Sean @ Pixel Inception, Inc. www.pixelinception.net
    Web Design & Web Development

  12. #12
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,513
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Where did you get those numbers?
    http://www.w3schools.com/browsers/browsers_stats.asp

  13. #13
    Sean N Pixel Inception's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA
    Posts
    280
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You meant to say roughly 10% of all users have js disabled.
    Sean @ Pixel Inception, Inc. www.pixelinception.net
    Web Design & Web Development

  14. #14
    SitePoint Enthusiast Red Blaze's Avatar
    Join Date
    Feb 2006
    Location
    Texas
    Posts
    94
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    haha, you nearly scared me there! Geez, still 10% out of ALL internet users is a big number.

  15. #15
    Sean N Pixel Inception's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA
    Posts
    280
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Red Blaze
    haha, you nearly scared me there! Geez, still 10% out of ALL internet users is a big number.
    Scared me aswell especially because most browsers come with it enabled out of the box and most users dont even know what javascript is let alone knowing how to disable it. Good thing it was a typo!
    Sean @ Pixel Inception, Inc. www.pixelinception.net
    Web Design & Web Development

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
  •