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