I retrieve all the form data with:
$jinput = JFactory::getApplication()->input;
$formData = new JRegistry($jinput->post->getArray());
then decode it and convert to my custom array with:
$formDataAsJson = json_decode($formData);
$arrayOfFields = array();
foreach($formDataAsJson as $name => $value) {
if(!empty($value) && $name !== 'submit') {
$arrayOfFields[] = '<strong>'.$name.'</strong>: '.$value;
}
}
and then email with:
$mailer = JFactory::getMailer();
...
$mailer->setBody(implode('<br/>',$arrayOfFields));
At my form there is one input with type="file"
and all the code works if I don't touch this input and fill only inputs with type="text"
and check inputs with type="radio"
. However, if I choose a file by using input type="file"
, the code stops working and I don't receive an email.
If I choose some file but send $formData
as JSON without decoding it and converting to an array e.g. with:
$mailer->setBody($formData);
then I receive an email. At that, there is no name for my input type="file"
inside of JSON line.
So it looks like the input type="file"
is not being pushed to the JSON line, but why does the code stop working in this case? This is strange. How can I decode form data, convert it to my array and send an email even if I choose a file? Or how to exclude input type="file"
from ->post->getArray()
?
The code for uploading and sending a file:
jimport('joomla.filesystem.file');
$file = $jinput->files->get('nameOfFileInput');
if(!empty($file['name'])) {
$filename = JFile::makeSafe($file['name']);
$src = $file['tmp_name'];
$dest = JPATH_SITE . DIRECTORY_SEPARATOR . "tmp" . DIRECTORY_SEPARATOR . $filename;
if(JFile::upload($src, $dest)) {
$mailer->addAttachment($dest);
}
}
Joomla 3.5.1