Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Senior Coder djm0219's Avatar
    Join Date
    Aug 2003
    Location
    Wake Forest, North Carolina
    Posts
    1,294
    Thanks
    4
    Thanked 203 Times in 200 Posts

    Is it possible to submit a form and values in a new page/tab?

    Given the page below I'm trying to have Button 2 submit the form but have the submit and subsequent processing be done in a new page/tab.

    The small bit of JavaScript works correctly to open a new page/tab but the form values don't appear to be available in the new page/tab nor does the new page/tab know that Button 2 was pressed.

    Is there a method of having a form from a page, along with all of its values and the button that was pressed, available on a new page/tab?

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="content-type" content="text/html; charset=ISO-8859-1" />
    <title>Form Test</title>
    <script type="text/javascript">
    function Print_Selected(TheForm) {
    
    	PrintWin = window.open(document.getElementById(TheForm).action,'_blank');
    	PrintWin.focus();
    }
    </script>
    </head>
    <body>
    <form action="FormTest.php" method="post" id="testform">
    <p><input type="submit" name="button1" id="button1" value="Button 1"/>
    <input onclick="Print_Selected('testform');return(false);" type="submit" name="button2" id="button2" value="Button 2" /></p>
    <p>
    <input type="checkbox" name="box1" value="box1" />CheckBox 1
    <input type="checkbox" name="box2" value="box2" />CheckBox 2
    </p>
    </form>
    </body>
    </html>
    FWIW this is a small part of a much larger application and the form and processing are all handled with PHP but the problem is really a basic HTML problem that isn't related to PHP.
    Last edited by djm0219; 03-29-2009 at 04:41 PM. Reason: Changed to resolved
    Dave .... HostMonster for all of your hosting needs

  • #2
    Senior Coder twodayslate's Avatar
    Join Date
    Mar 2007
    Location
    VA
    Posts
    1,042
    Thanks
    67
    Thanked 39 Times in 39 Posts
    twitter | Quality Hosting - $5.95/mo*
    Feel free to PM me!

  • Users who have thanked twodayslate for this post:

    djm0219 (03-28-2009)

  • #3
    Senior Coder djm0219's Avatar
    Join Date
    Aug 2003
    Location
    Wake Forest, North Carolina
    Posts
    1,294
    Thanks
    4
    Thanked 203 Times in 200 Posts
    Thanks for the pointer. A one line change to what I had and presto. Much appreciated.

    Actually that's not quite true. The target attribute for a XHTML strict document doesn't support target and without it things don't work as expected. Here's a complete copy of the actual code I'm testing with and it fails if I remove the target attribute from the form tag which I need to do to keep the page XHTML strict compliant.

    Code:
    <?php
    
    if (isset($_POST['button2'])) {
        print 'Button 2 was pressed';
        exit;
    }
    
    ?>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="content-type" content="text/html; charset=ISO-8859-1" />
    <title>Form Test</title>
    <script type="text/javascript">
    
    function Print_Selected(TheForm) {
    
    	PrintWin = window.open("",document.getElementById(TheForm).action,'_blank');
        var a = window.setTimeout("document."+TheForm+".submit();",10);
        document.TheForm.submit();
    
    }
    </script>
    </head>
    <body>
    <form action="FormTest.php" method="post" id="testform" target="myNewWin">
    
    <p><input type="submit" name="button1" id="button1" value="Button 1"/>
    
    <input onclick="Print_Selected('testform');return(false);" type="submit" name="button2" id="button2" value="Button 2" /></p>
    
    <p>
    <input type="checkbox" name="box1" value="box1" />CheckBox 1
    <input type="checkbox" name="box2" value="box2" />CheckBox 2
    </p>
    </form>
    </body>
    </html>
    Last edited by djm0219; 03-29-2009 at 12:19 AM. Reason: Clarified that the tip doesn't work for a XHTML strict document
    Dave .... HostMonster for all of your hosting needs

  • #4
    Senior Coder djm0219's Avatar
    Join Date
    Aug 2003
    Location
    Wake Forest, North Carolina
    Posts
    1,294
    Thanks
    4
    Thanked 203 Times in 200 Posts

    Resolved

    Thanks to a friend of mine a solution which allows the page to remain XHTML strict compliant is included below in case someone else comes along looking for a similar solution. I was making it more complicated than it needed to be.

    PHP Code:
    <?php

    if (isset($_POST['button1'])) {
        print 
    'Button 1 was pressed';
        print 
    '<p><a href="FormTest.php">Back</a>';
        exit;
    }

    if (isset(
    $_POST['button2'])) {
        print 
    'Button 2 was pressed';

        if (isset(
    $_POST['box1'])) {
            print 
    '<p />Checkbox 1 was selected';
        }

        print 
    '<p><a href="FormTest.php">Back</a>';
        exit;
    }

    ?>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="content-type" content="text/html; charset=ISO-8859-1" />
    <title>Form Test</title>
    <script type="text/javascript">

    function Print_Selected(BatchForm) {

        document.forms[BatchForm].target='batchprint';
        document.forms[BatchForm].submit();
        return false;

    }

    </script>
    </head>
    <body>
    <form action="FormTest.php" method="post" id="testform">

    <p>

    <input type="submit" name="button1" id="button1" value="Button 1"/>
    <input value="Button 2" onclick="Print_Selected('testform');" name="button2" type="submit" />

    </p>
    <p>
    <input type="checkbox" name="box1" value="box1" />CheckBox 1
    <input type="checkbox" name="box2" value="box2" />CheckBox 2
    </p>
    </form>
    </body>
    </html>
    Dave .... HostMonster for all of your hosting needs


  •  

    Posting Permissions

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