0

Hey All,

I have the a page that takes information for a ticket, and i wish to add the functionality of an upload button for attachments. I have tried making this work but because of using multiple posts , this obviously won't work (at least with what i know).

Is there a way that i could create an upload file section (simple browse button etc) that when i click the submit button for the main form, the file gets uploaded at the same time.

I have also heard that it would be possible with AJAX. Does anybody know anything about AJAX at all and know if this would be a possibility.

I will post some of my code here for review - but i don't really have much as of now (the code posted below is for the upload functionality only - the file is pretty big with other stuff)

$upload_dir = "/opt/lampp/htdocs/xampp/Support/test/files"; // Directory for file storing
// filesystem path
$web_upload_dir = "/xampp/Support/test/files"; // Directory for file storing

if (isset($_POST['fileframe']))
{
   $result = 'ERROR';
   $result_msg = 'No FILE field found';
   if (isset($_FILES['file']))  // file was send from browser
      {
         if ($_FILES['file']['error'] == UPLOAD_ERR_OK)  // no error
         {
         $filename = $_FILES['file']['name']; // file name
                                                                        move_uploaded_file($_FILES['file']['tmp_name'], $upload_dir.'/'.$filename);
// main action -- move uploaded file to $upload_dir
$result = 'OK';
}
elseif ($_FILES['file']['error'] == UPLOAD_ERR_INI_SIZE)
$result_msg = 'The uploaded file exceeds the upload_max_filesize directive in php.ini';
  else
$result_msg = 'Unknown error';

 // This is a PHP code outputing Javascript code.
echo '<script language="JavaScript" type="text/javascript">'."\n";
echo 'var parDoc = window.parent.document;';

if ($result == 'OK')
{
echo 'parDoc.getElementById("upload_status").value = "file successfully uploaded";';
echo 'parDoc.getElementById("filename").value = "'.$filename.'";';
echo 'parDoc.getElementById("filenamei").value = "'.$filename.'";';
//echo 'parDoc.getElementById("upload_button").disabled = false;';
}
else
{
echo 'parDoc.getElementById("upload_status").value = "ERROR: '.$result_msg.'";';
}

//      exit(); // do not go futher
}
// FILEFRAME section END

$TicketsHTML .= '<tr><td class="box_content_text"><strong>Upload Files:</strong></td><td>
<form action="<?PHP_SELF?>" target="upload_iframe" method="post" enctype="multipart/form-data">
<input type="hidden" name="filename" value="true">
<input type="file" name="file" id="file" onChange="jsUpload(this)">

<script type="text/javascript">
function jsUpload(upload_file)
{
    upload_field.form.submit();
    upload_field.disabled = true;
    return true;
}
</script>
<iframe name="upload_iframe" style="width: 400px; height: 100px; display: none;">
</iframe>
</td></tr>';

Basically my code here allows me to submit my ticket and the data is inserted correctly into my DB, but the upload file stuff does not work as expected. No files are uploaded at all. Any ideas? Mistake i have made in my code?

Thanks in advance

2
Contributors
4
Replies
5
Views
7 Years
Discussion Span
Last Post by hielo
0

On your php you have: if (isset($_POST['fileframe'])) but on your html you have <input type="hidden" name="filename" value="true"> so change the name of the hidden field to name="fileframe"

0

I made your suggested change, and that would make sense... but for whatever reason the code still does not upload the given file when i click submit ticket.

Is there something else i am missing that is obvious? Thanks again for your input and time.

0

Alright, my attempt at doing this so far has failed... my want/need it the following for my form.

Name:
DOB:
Location:
Picture: (upload picture)

Submit

I am using a post for the submit button - that basically reloads the page (which will then list the added profile - the information is taken and inserted into the db from the form --- this works... adding a image/upload/attachment is my sticking point!)

Basically, my main idea is to have a form with some input fields, but also with the option of uploading a file (up to 3). I cannot find any examples of this being done. Am i missing something really easy in html? Or do i have to use more advanced things?

Any ideas/comments would be greatly appreciated.

Thanks in advance

0

but for whatever reason the code still does not upload the given file when i click submit ticket.

Are you sure it is uploading to the right file? Upon closer inspection you have $TicketsHTML.='...<form action="<?PHP_SELF?>"...' It should be: $TicketsHTML.='...<form action="' . $_SERVER['PHP_SELF'] . '"...' On another note, on line 16 of your original post, you are setting $result to 'OK', BUT you do not know for a fact that the file was actually moved. You must check the return value of move_uploaded_file(). $result = move_uploaded_file(...) ? 'OK':'Error'; Lastly, also be sure the destination folder has write permissions.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.