473,387 Members | 1,493 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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!
Oct 12 '05 #1
5 5873
Codeman II wrote:
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!


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?
Oct 12 '05 #2
As far as I know, browsers put the "Browse" button there automatically
when you have a file input element:

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

Oct 13 '05 #3
Codeman II wrote:
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.


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.

Oct 13 '05 #4
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!
Oct 13 '05 #5
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_VARS['PHP_SELF'];?>" method="post"
enctype="multipart/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>Untitled 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($error)){
$insert = "INSERT INTO $table(
Field1,
Field2,
FileName)VALUES(
'$field1',
'$field2',
'$file_name')";
if(!$result = mysql_query($insert)){
$error .= "Error in inserting<br>";
}else{ ### If insert goes well, redirect to another page ###
?>
<script language="JavaScript">
<!--
self.location="<?php echo "confirmation.php";?>"
//-->
</script>
<?
}
}//if(!isset($error)){
### 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=\"#FF0000\">".$error."</td>";
echo "</tr>";
echo "</table>";
}
}//if($_POST['mode'] == 'add'){
?>
<form action="<?php echo $HTTP_SERVER_VARS['PHP_SELF'];?>"
method="post" enctype="multipart/form-data" name="add_fields"
id="add_fields">
<table width="500" border="1" cellspacing="0" cellpadding="0">
<tr>
<td width="50%" align="right">Field #1: </td>
<td><input name="field1" type="text" id="field1" value="<?php
echo $field1;?>"></td>
</tr>
<tr>
<td align="right">Field #2: </td>
<td><input name="field2" type="text" id="field2" value="<?php
echo $field2;?>"></td>
</tr>
<tr>
<td align="right">File Upload: </td>
<td><input type="file" name="file">
<br><?php echo $file_name;?></td>
</tr>
<tr align="center">
<td colspan="2"><input type="submit" name="Submit"
value="Submit">
<input type="reset" name="Submit2" value="Cancel"
onClick="self.location='redirect.php'">
<input name="mode" type="hidden" id="mode" value="add"> </td>
</tr>
</table>
</form></td>
</tr>
</table>
</body>
</html>
################################################## #################

Marius III wrote:
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!

Oct 14 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Don | last post by:
How do I retrieve the full path (C:\.....filename) of a file uploaded to a php script on the server. I think $_FILES will only provide 'name', which is only the filename itself. Thanks, Don ...
1
by: mattrapoport | last post by:
I have a form with an input type='file' element. I click on the Browse button of my file upload element, select a file, and hit open. The file name appears in the textbox portion of the file...
3
by: Bijoy Naick | last post by:
I've written a simple file upload user control in VB .NET. It comprises of an InputFile HTML Server Control, an Upload button and a message label. User clicks on the Browse button of the...
0
by: Raven Jones | last post by:
Heya all, I'm working on a web-based application (using ASP.NET and C# on .NET 1.1.4322, supporting only IE6 for Windows) that allows for file uploads. Screen real estate is at a premium, so I...
0
by: SEMIH DEMIR | last post by:
Sitelerden birinde verilen yabancı kaynakli bir scriptti duzenledim yanlız birseyin içinden bir turlu cıkamadım işin aslı ilk defa persistin upload componentini kullanacam yanlız suanki haliyle...
9
by: Wayne Smith | last post by:
I've come up against a major headache that I can't seem to find a solution for but I'm sure there must be a workaround and I would really be grateful of any help. I'm currently building a web...
18
by: walterbyrd | last post by:
I am trying to develop an app where: the same file, in the same place, will be uploaded, and then processed. Everything I can find about uploading a file, uses a form that requires the user to...
12
by: GuangXiN | last post by:
I want the file upload element disappear, instead of it, I place a text box and a button with my own css defination. but it doesn't work on IE7. What should I do now? <form action="upload.php"...
43
by: bonneylake | last post by:
Hey Everyone, Well this is my first time asking a question on here so please forgive me if i post my question in the wrong section. What i am trying to do is upload multiple files like gmail...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.