1

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

1 Answer 1

2

You can get file input data with this:

$files = $input->files->get('inputName');

You don't have to use JRegistry and then json_decode it. You will get array from $jinput->post->getArray() in first place.

So your code could be

$jinput = JFactory::getApplication()->input;
$formData = $jinput->post->getArray();
$arrayOfFields = array();
foreach($formData as $name => $value) {
    if(!empty($value) && $name !== 'submit') {
        $arrayOfFields[] = '<strong>'.$name.'</strong>: '.$value;
    }
}

But about the problem with filling file input, it should work either way.

File input is not included in $jinput->post->getArray(), therefore the problem relies somewhere else.

I suggest you to turn on error reporting, to see if any errors are thrown. You can see how it's done in this answer. First point should do it, depending on your server configuration.

Also, you can wrap your code into try-catch, to catch any thrown errors.

try {
    // Your code here
    // Create arrayOfFields and send email 
} catch (Exception $e) {
    echo 'Caught exception: ',  $e->getMessage(), "\n";
}

Let me know how it will go.

5
  • Yes, you was right: $formData is an array and I can retrieve it without new JRegistry(). But a letter still is not being sent if I choose some file and I don't get any error with catch (Exception $e) or with maximum Joomla error reporting. At that the file for sending is being uploaded successfully to destination folder.
    – stckvrw
    Commented Apr 13, 2016 at 19:02
  • Okay. So it seems that uploading a file causes this. Please show some more code and try to move file uploading after sending email. If it sends this way, then it's definately file uploading what causes this.
    – Rene Korss
    Commented Apr 13, 2016 at 19:10
  • But uploading and sending a file both are ok if I send $formData as JSON with new JRegistry(). And are not ok only if I try send it as an array. I mean when choosing a file. I added more code to the question. How can I move the file after uploading?
    – stckvrw
    Commented Apr 13, 2016 at 19:18
  • Seeing your code, don't move anything. Did you added file uploading to try-catch block? Is file uploaded correctly?
    – Rene Korss
    Commented Apr 13, 2016 at 19:45
  • Oh, I got it. It's my fault. I have a few forms on my page and all the while I was testing only one of them. It has only field for email and for file. Email I remove from the array before sending (because it's in the header) and file as you said is not included in the array so the form was not sending for the array was empty. Now everything is ok. Thank you for your help!
    – stckvrw
    Commented Apr 13, 2016 at 22:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.