Upload file having Browse button and Submit button on same form

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Codeman II

    Upload file having Browse button and Submit button on same form

    Hi there,

    I am building a form where the user must upload a picture and fill in his
    details.
    Now I have a problem as all of this is on the same form.

    How will I be able to have the Browse button to open the "file browse"
    dialog
    and the Submit button to submit the form data.

    As I believe you can not use GET but must use POST method for form.
    Is there any means that I can use GET cause I need to get form variables
    from URL as well.


    Thanks ALOT!


  • Mazin07

    #2
    Re: Upload file having Browse button and Submit button on same form

    Codeman II wrote:[color=blue]
    > Hi there,
    >
    > I am building a form where the user must upload a picture and fill in his
    > details.
    > Now I have a problem as all of this is on the same form.
    >
    > How will I be able to have the Browse button to open the "file browse"
    > dialog
    > and the Submit button to submit the form data.
    >
    > As I believe you can not use GET but must use POST method for form.
    > Is there any means that I can use GET cause I need to get form variables
    > from URL as well.
    >
    >
    > Thanks ALOT!
    >
    >[/color]

    Put the submit button in the same form as the upload thing.
    You can't use GET for uploads. 30 megs of data doesn't work well in a
    URI. Why can't you get the variables from POSTDATA?

    Comment

    • ZeldorBlat

      #3
      Re: Upload file having Browse button and Submit button on same form

      As far as I know, browsers put the "Browse" button there automatically
      when you have a file input element:

      <input type="file" name="myFile"/>

      Comment

      • Chung Leong

        #4
        Re: Upload file having Browse button and Submit button on same form

        Codeman II wrote:[color=blue]
        > As I believe you can not use GET but must use POST method for form.
        > Is there any means that I can use GET cause I need to get form variables
        > from URL as well.[/color]

        Of course you can. Just put the GET parameter in the URL in the action
        attribute. Or if you want to avoid mixing GET with POST, put the
        variables in hidden fields.

        Comment

        • Marius III

          #5
          Re: Upload file having Browse button and Submit button on same form

          Thanks for all the help.

          I figure that the Browse button is placed there with the INPUT TYPE="FILE"
          Tag.

          But if the user submit that form and not all form data is filled in then I
          display a page which tells him to "Fill in all your fields" with a BACK
          link. Now how can I save the data when the user click the Back link. Cause
          normally if he clicks my back link the form is reset and all previous
          entered data is lossed.

          Thank you!


          Comment

          • scott

            #6
            Re: Upload file having Browse button and Submit button on same form

            What I do for form validation is I call the same page for display,
            validation and database entries.

            In the action part of the html form I use:
            <form action="<?php echo $HTTP_SERVER_VA RS['PHP_SELF'];?>" method="post"
            enctype="multip art/form-data" name="add_fleet " id="add_fleet" >

            This way when the submit button is clicked, the page calls itself.
            To make this work you will need to set a 'hidden field':
            <input name="mode" type="hidden" id="mode" value="add">

            Then the whole process is as simple as checking to see if:
            $_POST['mode'] == 'add'
            if it does branch off to validate the fields and if the fields fail set
            an $error variable. Then before you do your database insert, check to see:
            isset($error)
            If it is then do not do an insert but continue to display the form with
            the fields filled out with what they already entered. If $error is not
            set, then continue with the INSERT and if all goes well just redirect
            the page to whatever you want such as a confirmation page.

            I hope this is not too confusing. Here is some code that works.
            ############### ############### ############### ############### ####
            <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
            "http://www.w3.org/TR/html4/loose.dtd">
            <html>
            <head>
            <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
            <title>Untitl ed Document</title>
            </head>

            <body>
            <table width="500" border="1" align="center" cellpadding="0"
            cellspacing="0" >
            <tr>
            <td align="center">
            <?php
            ##### Check if form was Submitted #####
            if($_POST['mode'] == 'add'){
            ### Retrieve the variables ###
            if($_POST['field1'] == ""){
            $error = "* Please fill in Field #1<br>";
            }else{
            $field1 = $_POST['field1'];
            }
            if($_POST['field2'] == ""){
            $error .= "* Please fill in Field #2<br>";
            }else{
            $field2 = $_POST['field2'];
            }
            if($_FILES['file']['name'] == ""){
            $error .= "* Please select a File to upload<br>";
            }else{
            $file_name = $_FILES['file']['name'];
            /* Process file*/
            }
            ### If fields are valid then attempt an insert ###
            ### If not set $error and bypass the insert ###
            if(!isset($erro r)){
            $insert = "INSERT INTO $table(
            Field1,
            Field2,
            FileName)VALUES (
            '$field1',
            '$field2',
            '$file_name')";
            if(!$result = mysql_query($in sert)){
            $error .= "Error in inserting<br>";
            }else{ ### If insert goes well, redirect to another page ###
            ?>
            <script language="JavaS cript">
            <!--
            self.location=" <?php echo "confirmation.p hp";?>"
            //-->
            </script>
            <?
            }
            }//if(!isset($erro r)){
            ### If there was an error display it ###
            if(isset($error )){
            echo "<table width=\"300\" border=\"0\" cellspacing=\"0 \"
            cellpadding=\"0 \">";
            echo "<tr>";
            echo "<td bgcolor=\"#FF00 00\">".$error." </td>";
            echo "</tr>";
            echo "</table>";
            }
            }//if($_POST['mode'] == 'add'){
            ?>
            <form action="<?php echo $HTTP_SERVER_VA RS['PHP_SELF'];?>"
            method="post" enctype="multip art/form-data" name="add_field s"
            id="add_fields" >
            <table width="500" border="1" cellspacing="0" cellpadding="0" >
            <tr>
            <td width="50%" align="right">F ield #1: </td>
            <td><input name="field1" type="text" id="field1" value="<?php
            echo $field1;?>"></td>
            </tr>
            <tr>
            <td align="right">F ield #2: </td>
            <td><input name="field2" type="text" id="field2" value="<?php
            echo $field2;?>"></td>
            </tr>
            <tr>
            <td align="right">F ile Upload: </td>
            <td><input type="file" name="file">
            <br><?php echo $file_name;?></td>
            </tr>
            <tr align="center">
            <td colspan="2"><in put type="submit" name="Submit"
            value="Submit">
            <input type="reset" name="Submit2" value="Cancel"
            onClick="self.l ocation='redire ct.php'">
            <input name="mode" type="hidden" id="mode" value="add"> </td>
            </tr>
            </table>
            </form></td>
            </tr>
            </table>
            </body>
            </html>
            ############### ############### ############### ############### #######



            Marius III wrote:[color=blue]
            > Thanks for all the help.
            >
            > I figure that the Browse button is placed there with the INPUT TYPE="FILE"
            > Tag.
            >
            > But if the user submit that form and not all form data is filled in then I
            > display a page which tells him to "Fill in all your fields" with a BACK
            > link. Now how can I save the data when the user click the Back link. Cause
            > normally if he clicks my back link the form is reset and all previous
            > entered data is lossed.
            >
            > Thank you!
            >
            >[/color]

            Comment

            Working...