Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Storing Data from a php form back to another php form already opened.

Posted on 2004-09-09
8
265 Views
Last Modified: 2006-11-17
I have a form created using PHP and HTML. Within the form is a link to another form in PHP that opens and allows a file to be downloaded. See code for the MAIN FORM(This is opened first by user):

************************************************************************
<table width="99%" align="center" cellpadding="0" cellspacing="7" bgcolor=#ccccff style="border: solid #333399 1px; color: 000000;">

                          <tr align="center" valign="middle">
            <td colspan=4 nowrap><a href="attachment2.html" target="_blank"><img src="IMAGES/paperclip.jpg" alt="Download File" border="0"></a>
              Download Attachment &amp; Enter File Name below
              <input type="text" name="attachment"></td></tr>
                  </table>
************************************************************************

When they click the paperclip it opens attachment2.html file. This code allows the user to browse and download a file. See code below(This opens second):



************************************************************************
<form enctype="multipart/form-data" action="upload.php" method="post">
    <input type="hidden" name="MAX_FILE_SIZE" value="100000000" />
    Choose a file to upload: <input name="userfile" type="file" />
    <input type="submit" value="Upload File" />
      <input type="reset" value="Reset" />
</form>
************************************************************************


This then runs another php file (upload.php) and uploads the file. See code(code just runs, message is displayed and user can click close button):


************************************************************************
<?php
$uploadDir = 'uploads/';
$uploadFile = $uploadDir . $_FILES['userfile']['name'];
echo "<pre>";
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadFile))
{
    echo "<h3>File is valid, and was successfully uploaded.</h3> ";
}
else
{
   
    echo "<h3>Upload conflict! File was not uploaded!</h3>" ;
}
echo '
<form>
<center><input type="button" value="Close" onClick="javascript:top.close()"></center>
</form>';
echo "</pre>";
?>

***********************************************************************
HERE IS THE QUESTION: How do I get it to pull the name of the file that is chosen in the ATTACHMENT2. html (userfile field) and enter it automatically in the MAIN FORM (attachment field)?

0
Comment
Question by:JoE0526
  • 5
  • 3
8 Comments
 

Expert Comment

by:reice0116
ID: 12021070
If I understand you correctly, you wish to change a text-input value on a parent window from a child window.

The easiest way is to use "opener"
for info on "opener" see http://www.webreference.com/js/tutorial1/opener.html

on the form place an 'onSubmit' which uses opener to assign the value of the file input to the text input OR do the same thing from the page that uploads the file.
0
 

Author Comment

by:JoE0526
ID: 12021219
I wish to take the name of the file that is being defined (downloaded) in the userfile textbox from the attachment2.html file and send it back to the MAIN form and have it fill in and store in the attachment field within the MAIN FORM so that the user will not have to manually enter the name of the file he/she has downloaded. When the user actually submits the main form, the name of the file that was downloaded will then be added to the field in the MYSQL database via PHP.
0
 

Expert Comment

by:reice0116
ID: 12021478
an example:

first page:

<form name="myform" > 
  <input type="text" name="attachment">
  <a href="test2.php" target="_blank">test2</a>
</form>

click on test2 and ....
second page:
<head>
<script>

      function doit() {
            alert(document.myform2.myfile.value);
            window.opener.document.myform.attachment.value = document.myform2.myfile.value;
      }
</script>
</head>

<body>
<form name="myform2" onSubmit="doit();">
      <input name="myfile" type="file">
      <input type="submit">
</form>
</body>


Then the user picks a file, when they click submit, the filename goes into the original page's textbox
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

Expert Comment

by:reice0116
ID: 12021551
additionally:

you may want to only add it to the field after the upload is completed, in which case just move the code into the if statement assuring that the upload was successful.
0
 

Author Comment

by:JoE0526
ID: 12044329
Ok I got this working using alpha characters(see example "p" in code) in regards to storing a name of a file into a textbox on the previous page, but I can't get it to work when I am trying to use the \ character as in an example file name m:\test\allphy.jpg. I want to store just the file name, not the directory locations. Have tried various thing with no success. Your thoughts????


function carryvar(){
var name = document.attach.userfile.value;
var c = name.lastIndexOf("p");
var finalname = name.substring(c);
alert(finalname);

window.opener.document.request.attachment.value = finalname;





}
0
 

Expert Comment

by:reice0116
ID: 12044953
You could use regular expressions to separate the last name from the string OR

what I probably would do is store $_FILES['userfile']['name']  instead of the form field value.

window.opener.document.request.attachment.value = $FILES['userfile']['name']
(since the page that the uploader form submits to is in the same window - 'opener' should function the same)

This way you can also make sure that the file itself was uploaded successfully before transferring it over to the form.

Let me know if that does it.
0
 

Author Comment

by:JoE0526
ID: 12045339
I have already done this, but I want to manipulate the file name to just give me the actual file name and not the directory for information purposes. The directory will vary from user to user and I don't really care about that. I can code to eliminate this process on an unsuccessful download, but I need to recognize the last "\" to determine the actual file name....
0
 

Accepted Solution

by:
reice0116 earned 50 total points
ID: 12047234
$_FILES['userfile']['name'] only gives you the file name... php does this so that you can add, for example, "uploads/" in front of it like in your example above.

if you do an "echo $_FILES['userfile']['name']" you will get just the filename printed to the screen....


<script>
window.opener.document.request.attachment.value = "<?php echo $_FILES['userfile']['name']; ?>";
</script>
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question