By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
426,083 Members | 1,608 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 426,083 IT Pros & Developers. It's quick & easy.

Problem when uploading a file in form

P: 63
Hi,
Ive created a registration page in which at the last it asks the user to upload the resume file. When the user clicks on the upload button to upload the file it automativcally submits all the data entered in the form. I want that when the user uploads the file after clicking the upload button it should display the message of successful upload on the same page itself. Then when the user clicks on the submit button the data should be entered in the database.
Ive attached the registration form file plz let me know how i can fix this problem.
I ve given the required code below.

Expand|Select|Wrap|Line Numbers
  1. <tr>
  2.     <td bgcolor="#ECE9D8"> <div align="center">
  3.       <span class="style14">
  4.       <input type="hidden" name="MAX_FILE_SIZE" value="8000000">
  5.        Select file: </span>
  6. <input type="file" name="data">
  7. <input type="submit" name="Submit" value="Upload File">
  8. </div>
  9.       </td>
  10.   </tr>
  11.   <tr>
  12.     <td height="38" bgcolor="#ECE9D8"><div align="center">
  13.  
  14.       <input name="Reset" type="reset" id="Reset" value="Reset" />
  15.       <input type="submit" name="Submit" value="Submit" />
  16.  
  17.     </div></td>
  18.   </tr>
  19.   </form>
  20.  
  21.  <?php
  22. } else {
  23. // check uploaded file size
  24. if ($_FILES['data']['size'] == 0) {
  25. die("ERROR: Zero byte file upload");
  26. }
  27.  
  28. // check if file type is allowed (optional)
  29. $allowedFileTypes = array("text/plain", "application/msword");
  30. if (!in_array($_FILES['data']['type'], $allowedFileTypes)) {
  31. die("ERROR: File type not permitted");
  32. }
  33.  
  34. // check if this is a valid upload
  35. if (!is_uploaded_file($_FILES['data']['tmp_name'])) {
  36. die("ERROR: Not a valid file upload");
  37. }
  38.  
  39. // set the name of the target directory
  40. $uploadDir = "./uploads/";
  41.  
  42. // copy the uploaded file to the directory
  43. move_uploaded_file($_FILES['data']['tmp_name'], $uploadDir .$_FILES['data']['name']) or die("Cannot copy uploaded file");
  44.  
  45. // display success message
  46. echo "File successfully uploaded. " ;
  47. }
  48.  
Mar 14 '08 #1
Share this Question
Share on Google+
11 Replies


ronverdonk
Expert 2.5K+
P: 4,258
Do you mean that the user has to click twice? Once for the upload and once for the passing of the form variables. Then you better make sure that you also have 2 forms.

Ronald
Mar 14 '08 #2

mageswar005
P: 72
hi,
i think your submit button name is submit, is it correct?
please change your submit button name.
Mar 14 '08 #3

ronverdonk
Expert 2.5K+
P: 4,258
hi,
i think your submit button name is submit, is it correct?
please change your submit button name.
Why do you care what the submit button name is when you don't test the $_POST['submit'] parm?

Another thing is that you have 2 type=submit buttons so, whichever you click, the form will be submitted despite the text on the button (submit or upload).

Ronald
Mar 14 '08 #4

P: 63
Why do you care what the submit button name is when you don't test the $_POST['submit'] parm?

Another thing is that you have 2 type=submit buttons so, whichever you click, the form will be submitted despite the text on the button (submit or upload).

Ronald
Yes i know the problem is because of 2 type=submit buttons. Is there any way by which this problem can be solved. On normal job sites when you upload the file it doesnt submits the dat.
Mar 14 '08 #5

Markus
Expert 5K+
P: 6,050
Yes i know the problem is because of 2 type=submit buttons. Is there any way by which this problem can be solved. On normal job sites when you upload the file it doesnt submits the dat.
Why do you need 2 submit buttons in the first place?

You're talking about uploading an image without refreshing the page.
You'll need to use a hiden iframe for that.

Can't say much more as im ill!

markusn00b ------------------> going to bed.
Mar 14 '08 #6

P: 63
Whats a hidden iframe and how to use it. Plz let me know im stuck up with this.
Mar 14 '08 #7

Markus
Expert 5K+
P: 6,050
Have a look at this
:)
Mar 14 '08 #8

ronverdonk
Expert 2.5K+
P: 4,258
Yes i know the problem is because of 2 type=submit buttons. Is there any way by which this problem can be solved. On normal job sites when you upload the file it doesnt submits the dat.
A submit button submits the form in which it is included. So when you use 2 of those, you make 2 forms, each with its own submit/upload button.

And when you don't know what an iframe is, I suggest that you follow Mark's advice and do some toturialing on HTML and PHP.

Ronald
Mar 14 '08 #9

P: 63
Thanks.
Iwill study the link you gave and if any problem persists i ll let u know. Its a very good link that you suggested.
Mar 16 '08 #10

ronverdonk
Expert 2.5K+
P: 4,258
Thanks.
Iwill study the link you gave and if any problem persists i ll let u know. Its a very good link that you suggested.
I hope you succeed. See you around.

Ronald
Mar 16 '08 #11

P: 63
Thanks the link realyy helped me a lot.
IMy problem got solved. Its uploading properly without submitting the data.
Thanks a lot.
Mar 20 '08 #12

Post your reply

Sign in to post your reply or Sign up for a free account.