Best way to expand a form

Associate
Joined
19 Jul 2006
Posts
1,847
I have a form with name phone etc on that is emailed to me when the submit is cliciked.

However I want the user to be able to attach document to the form which will in turn be attached to the email.
I would like the form to expand so when the click add attachment it will bring up a choose file box on the page, and keep expanding if the user clicks add attachments.

similar to this page
http://www.howell-jones.com/online_services/application/4

any tutorials or guidance mainly in the expanding the form would be helpful.

thanks
 
Associate
OP
Joined
19 Jul 2006
Posts
1,847
Ok I got this working to start with
PHP:
<html>
<head>
  <title>Administration - upload new files</title>
</head>
<body>
<h1>Upload new news files</h1>
<form enctype="multipart/form-data" action="upload.php" method="post">
  <input type="hidden" name="MAX_FILE_SIZE" value="1000000">
  Upload this file: <input name="userfile" type="file">
</form>
</body>
</html>
With this
PHP:
<html>
<head>
  <title>Uploading...</title>
</head>
<body>
<h1>Uploading file...</h1>
<?php
$youremail = "[email protected]";
$tmp_name = $_FILES['userfile']['tmp_name'];
    $type = $_FILES['userfile']['type'];
    $name = $_FILES['userfile']['name'];
    $size = $_FILES['userfile']['size'];
    if (file_exists($tmp_name))
        {
         if(is_uploaded_file($tmp_name)) {
            $file = fopen($tmp_name,'rb');            //open the file
                 $data = fread($file,filesize($tmp_name));    //read the file
                 fclose($file);                    // close the file
                 $data = chunk_split(base64_encode($data));    // encode and split
                }
            $bound_text = "x".md5(mt_rand())."x";
            $bound = "--".$bound_text."\r\n";
            $bound_last = "--".$bound_text."--\r\n";
             $headers = "From: {$sendersname}<{$sendersemail}>\r\n"
            ."MIME-Version: 1.0\r\n"
              ."Content-Type: multipart/mixed; boundary=\"$bound_text\"";
            $message .= "If you can see this MIME than your client doesn't accept MIME types!\r\n"
              .$bound;
       
            $message .= "Content-Type: text/html; charset=\"iso-8859-1\"\r\n"
              ."Content-Transfer-Encoding: 7bit\r\n\r\n"
              .$sendcontent."\r\n"
              .$bound;
        
            $message .= "Content-Type: ".$type."; name=\"".$name."\"\r\n" 
              ."Content-Transfer-Encoding: base64\r\n"
              ."Content-disposition: attachment; file=\"".$name."\"\r\n" 
              ."\r\n"
              .$data
              .$bound_last; 
            }
       
    mail($youremail, $subject, $message, $headers);
	?>

This works with a single file upload.
However because I want the form to be dynamic I dont know how many uploads there will be. So I have changed to this for now
PHP:
<html>
<head>
  <title>Administration - upload new files</title>
</head>
<body>
<h1>Upload new news files</h1>
<form enctype="multipart/form-data" action="upload.php" method="post">
  <input type="hidden" name="MAX_FILE_SIZE" value="1000000">
  Upload this file: <input name="userfile[]" type="file">
  
  <input type="hidden" name="MAX_FILE_SIZE" value="1000000">
  Upload this file: <input name="userfile[]" type="file">
  <input type="submit" value="Send File">
</form>
</body>
</html>
and this
PHP:
<html>
<head>
  <title>Uploading...</title>
</head>
<body>
<h1>Uploading file...</h1>
<?php
$youremail = "[email protected]";

$i = count($_FILES['userfile']);


foreach($_FILES['userfile'] as $file){    
	$tmp_name = $_FILES['userfile']['tmp_name'];
    $type = $_FILES['userfile']['type'];
    $name = $_FILES['userfile']['name'];
    $size = $_FILES['userfile']['size'];


    if (file_exists($tmp_name))
        {
         if(is_uploaded_file($tmp_name)) {
            $file = fopen($tmp_name,'rb');            //open the file
                 $data = fread($file,filesize($tmp_name));    //read the file
                 fclose($file);                    // close the file
                 $data = chunk_split(base64_encode($data));    // encode and split
                }
            $bound_text = "x".md5(mt_rand())."x";
            $bound = "--".$bound_text."\r\n";
            $bound_last = "--".$bound_text."--\r\n";
             $headers = "From: {$sendersname}<{$sendersemail}>\r\n"
            ."MIME-Version: 1.0\r\n"
              ."Content-Type: multipart/mixed; boundary=\"$bound_text\"";
            $message .= "If you can see this MIME than your client doesn't accept MIME types!\r\n"
              .$bound;
       
            $message .= "Content-Type: text/html; charset=\"iso-8859-1\"\r\n"
              ."Content-Transfer-Encoding: 7bit\r\n\r\n"
              .$sendcontent."\r\n"
              .$bound;
        
            $message .= "Content-Type: ".$type."; name=\"".$name."\"\r\n" 
              ."Content-Transfer-Encoding: base64\r\n"
              ."Content-disposition: attachment; file=\"".$name."\"\r\n" 
              ."\r\n"
              .$data
              .$bound_last; 
            }
echo "$i";
        }
       
    mail($youremail, $subject, $message, $headers);
	?>

So I think I have turned the $_Files into an array?
However $i is coming back as 5 and not 2?
and its not attaching the files now.

I seam to have c****d it up
 
Back
Top Bottom