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 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Jul 2012
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Confusion combining two forms, POST and move_uploaded_file

    Hi everyone

    I did some HTML and PHP several years ago but then moved towards network security. I'm trying to code an application now and have found some simple code that demonstrates what I'm trying to do (but failing miserably!).

    give.php
    PHP Code:
    <html>
    <!-- http://www.htmlgoodies.com/beyond/php/article.php/3472551/PHP-Tutorial-Uploading-Files.htm -->
    <!-- http://www.htmlgoodies.com/beyond/php/article.php/3472561 -->
        <head>
            <title>File Upload Form</title>
        </head>
        <body>
            This form allows you to upload a file to the server.<br>
            <form action="take.php" method="post" enctype="multipart/form-data"><br>
                Type (or select) Filename: <input type="file" name="uploadFile">
                <input type="submit" value="Upload the file and your name">
            </form>
            <form action="" method="post">
                What is your name?<br>
                <input type="text" name="name">
                <input type="submit" value="Confirm your name">
                <?php
                
    if (isset($_POST['name'])) {
                    echo 
    'You said your name is: '$_POST['name'];
                }
                
    ?>
            </form>
        </body>
    </html>
    take.php
    PHP Code:
    <html>
    <!-- http://www.htmlgoodies.com/beyond/php/article.php/3472551/PHP-Tutorial-Uploading-Files.htm -->
    <!-- http://www.htmlgoodies.com/beyond/php/article.php/3472561 -->
        <head>
        </head>
        <body>
            <form action="take.php" method="post" enctype="multipart/form-data"><br>
                <input type="hidden" name="MAX_FILE_SIZE" value="25000" />
            </form>
        </body>
    </html>
        and now the PHP page to handle the form:
    <html>
        <head>
            <title>Process Uploaded File</title>
        </head>
        <body>
        <?php
        
    if ( move_uploaded_file ($_FILES['uploadFile'] ['tmp_name'], "./images/{$_FILES['uploadFile'] ['name']}")  ) {
            echo 
    '<p> The file has been successfully uploaded </p>';
        } else {
            switch (
    $_FILES['uploadFile'] ['error']) {
                case 
    1:
                    print 
    '<p> The file is bigger than this PHP installation allows</p>';
                    break;
                case 
    2:
                    print 
    '<p> The file is bigger than this form allows</p>';
                    break;
                case 
    3:
                    print 
    '<p> Only part of the file was uploaded</p>';
                    break;
                case 
    4:
                    print 
    '<p> No file was uploaded</p>';
                    break;
            }
        }
        echo 
    '<br><br>On the first page, you said your name is ' 'WHAT GOES HERE?';
        
    ?>
        </body>
    </html>
    When I load give.php, I can select a file and upload it via the "Upload the file and your name" button.

    When I load give.php, I can enter my name and click "Confirm your name" which displays my name as it should.

    What I would like to do is:

    1. Select a file (but not click the "Upload the file and your name" button).

    2. Enter my name and confirm it by clicking the "Confirm your name" button.

    3. Click the "Upload the file and your name" and have the file upload and display my name on the second page.

    When I follow this sequence, the selected file (after step 1) is cleared when I click the button in step 2 to display my name. Is it possible to arrange for everything to happen in the sequence that I would like? How would I pass the name variable from give.php to take.php?

    As I said, it's quite a while since I did any PHP and don't know if what I am trying to do is possible.

    Thanks for your time and patience!
    Last edited by Nicodemus; 07-20-2012 at 01:00 PM. Reason: Change code tags to php tags

  • #2
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,106
    Thanks
    11
    Thanked 101 Times in 99 Posts
    you have 2 forms there and that is I assume the problem, make them into 1 form...

    Code:
    <form action="take.php" method="post" enctype="multipart/form-data"><br>
                Type (or select) Filename: <input type="file" name="uploadFile">
                What is your name?<br>
                <input type="text" name="name">
                <input type="submit" value="Confirm your name">
                <?php
                if (isset($_POST['name'])) {
                    echo 'You said your name is: '. $_POST['name'];
                }
                ?>
            </form>
    now on take.php you can process the uploaded file and echo <?=$_POST['name'];?> at the same time which I think what you meant ?
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #3
    New to the CF scene
    Join Date
    Jul 2012
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks firepages for the suggestion.

    I figured that the situation relates to the fact that there are two forms on the first page; one posting to itself (to display my name) and the second processing the uploaded file and confirming my name.

    Unfortunately, your suggestion doesn't work. I need to be able to:

    1. Select a file for upload on the first page but not click the upload button.

    2. Enter my name and click the button to display my name on the first page (unfortunately, this clears the name of the file selected for upload).

    3. Click the upload the file and name button which opens the second page and confirms the upload and prints out the name again.

    I am not sure if this can be done in the way that I am trying. I thought originally about nested forms but, when I was researching how to configure them, I came across some sites where they said that nested forms should never be used.

    I am open to other suggestions!

  • #4
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,106
    Thanks
    11
    Thanked 101 Times in 99 Posts
    2) Enter my name and click the button to display my name on the first page
    ahhh OK, well if you don't mind javascript ?
    Code:
    <script>
    function set_name(name){
    obj=document.getElementById(name);
    document.getElementyId('name').value=obj.value;
    document.getElementyId('display_name').innerHTML=obj.value;
    }
    </script>
    <div id="display_name"></div>
    <input type="hidden" id="name" name="name" value="" />
    <input type="button" name="button" value="submit name" onclick="set_name('name');" />
    if you dont want to use javascript you could use an iframe.. but that gets really messy
    Last edited by firepages; 07-22-2012 at 01:51 AM. Reason: stuffed up :)
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • Users who have thanked firepages for this post:

    Nicodemus (07-22-2012)

  • #5
    New to the CF scene
    Join Date
    Jul 2012
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I had a sneaking suspicion that JavaScript might be needed. The only thing I have against it is that users might have it disabled.

    It's funny (a coincidence, not ha ha!) that you mentioned an iframe. I was looking into frames a few days ago and maybe this will give me the impetus to do some more research.


  •  

    Posting Permissions

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