Posted By:
9 Oct, 2013 7:05 pm PDT

Creating a form with file attachments and submit it using php

Hi, I want to create a form that has one button to submit and the following fields

Name:
Email:
Address:
File attachment:

How can i create and submit the details of each text fields once the button is clicked?

I want to save the data and display them in a textarea. I also want to enter the data to a database. The database details are:

  1. DB Name: db
  2. User name: test
  3. List of columns:first_name, email, add, path

I also want the attachment to be moved to a folder 'files'. I have some other things to do. but these are the basic ones.

Thanks in advance.

Attachment:None


Please download the attached zip file and then unzip it. You will get two file upload.php and brian.sql. Please have a look at these two files. In case of more clarification required please reply.

<?php

if (isset($_POST['save'])) {
       $name = trim(strip_tags($_POST['name']));
       $email = trim(strip_tags($_POST['email']));
       $address = trim(strip_tags($_POST['address']));
       $file_name = trim(strip_tags($_FILES['file']['name']));
       $errors = array();
       if (empty($name))
             $errors['name'] = "Empty";
       if (empty($email))
             $errors['email'] = "Empty";
       if (empty($address))
             $errors['address'] = "Empty";
       if (empty($file_name))
             $errors['file'] = "Empty";
       else {
             $file_path = $_FILES['file']['tmp_name'];
             $file_content = addslashes(file_get_contents($file_path));
       }
       if (empty($errors)) {
             mysql_connect("localhost", "root", "root") or die("Can not connect

to DB");

             mysql_select_db("brian") or die("Can not select DB");
             $q = "insert into test values(null, '$name', '$email', '$address',
'$file_name', '$file_content')";
             mysql_query($q) or die($q . mysql_error());
             $success = "
   <table border=1 align=center width=50%>
    <tr><th colspan=2>Following values have been saved:</th></tr>
    <tr><th>Name</th><td>$name</td></tr>
    <tr><th>Email</th><td>$email</td></tr>
    <tr><th>Address</th><td>$address</td></tr>
    <tr><th>File</th><td>$file_name</td></tr>

</table>

";

} }

?>

<form action=upload.php method=post enctype='multipart/form-data'>
   <table border=1 align=center width=50%>
    <tr><th colspan=2>File Upload Form</th></tr>
    <tr>
     <th>Name</th>
     <td>
      <input type=text name=name value="<?php echo $name; ?>">
      <font color=red><?php echo $errors['name']; ?></font>
     </td>

</tr> <tr>

     <th>Email</th>
     <td>
      <input type=text name=email value="<?php echo $email; ?>">
      <font color=red><?php echo $errors['email']; ?></font>
     </td>

</tr>

    <tr>
     <th>Address</th>
     <td>
      <input type=text name=address value="<?php echo $address; ?>">
      <font color=red><?php echo $errors['address']; ?></font>
     </td>

</tr> <tr>

     <th>File</th>
     <td>
      <input type=file name=file>
      <font color=red><?php echo $errors['file']; ?></font>
      </td>

</tr> <tr>

     <th colspan=2>
      <input type=submit name=save value=Save>

</th> </tr>

   </table>
</form>

<?php

echo $success;
?>

 


Was it useful?  




Please login to reply to this problem.