1

How to get the formDate in the controller?

my HTML

<form id="myform" >
    <input type="number" name="jform[option][1]">
    <input type="number" name="jform[option][2]">
</form>

my js

var form     = document.getElementById('myform'),
    formData = new FormData(form);

var xhr = new XMLHttpRequest();
xhr.open('GET', window.location.pathname+'?option=com_mycomp&task=view.mytask&id=567&'+Joomla.getOptions('csrf.token')+'=1', true);
            
xhr.setRequestHeader('Content-Type', 'application/json; charset=utf-8');
//xhr.setRequestHeader('Content-Type', 'x-www-form-urlencoded');
xhr.setRequestHeader('Cache-Control', 'no-cache');
            
xhr.send(formData);

Method in controller

public function mytask() 
{
    Session::checkToken('get') or die();
    
    $app    = Factory::getApplication();
    $id= $app->input->get('id', '0', 'INTEGER'); // it exists
        
    $form = $app->input->get('jform', array(), 'ARRAY'); // it doesn't exist
    
    exit;
}
1

I finally solved this problem by simply removing the headers from the request.

var form     = document.getElementById('myform'),
    formData = new FormData(form);

var xhr = new XMLHttpRequest();
xhr.open('GET', window.location.pathname+'?option=com_mycomp&task=view.mytask&id=567&'+Joomla.getOptions('csrf.token')+'=1', true);
            
xhr.setRequestHeader('Content-Type', 'application/json; charset=utf-8'); // REMOVED!
xhr.setRequestHeader('Cache-Control', 'no-cache');  // REMOVED!
            
xhr.send(formData);

Now I get the form array as expected!

And similarly can be done with jQuery:

var form     = document.getElementById('myform'),
    formData = new FormData(form);

jQuery.ajax({
    timeout: 6000,
    url: window.location.pathname+'?option=com_mycomp&task=view.mytask&id=567&'+Joomla.getOptions('csrf.token')+'=1',
    data: formData,
    type: 'post',
    processData: false,
    contentType: false,
    dataType: 'json',
    async: true,
    success:function(result) {console.log(result)}
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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