...

View Full Version : Confusion combining two forms, POST and move_uploaded_file



Nicodemus
07-20-2012, 10:47 AM
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


<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


<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!

firepages
07-21-2012, 12:40 AM
you have 2 forms there and that is I assume the problem, make them into 1 form...



<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 ?

Nicodemus
07-21-2012, 09:52 AM
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!

firepages
07-22-2012, 12:49 AM
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 ?


<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

Nicodemus
07-22-2012, 02:32 PM
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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum