1

I am developing a joomla component. I added a drag and drop uploader with ajax. When a file is uploaded, a confirmation-mail should be sent. I get the right item of the database with $id = $app->input->getInt('id'); This works, But when the ajax call is done, the variable $id is set to null, so in the email the $customers->name is also null. Why is the complete model reloaded after the ajax-call?

I call the controller method:

Ajax Call

$.ajax({
    cache: false,
    url: "index.php?option=com_customercenter&task=mailAfterUpload&tmpl=component",
    dataType: "json",
    formData: {"companyID":"companyID"},
    success: function(data) {
        for(var i=0;i<data.length;i++) { 
            obj.createProgress(data[i]["name"],data[i]["path"],data[i]["size"]);
        }
    }
});

Controller

public function mailAfterUpload() {
    $model = $this->getModel();
    $response = $model->sendEmailAfterUpload();
}

Model

public function sendEmailAfterUpload() {
    $customers = $this->getCustomercenterData();

    $mailer = JFactory::getMailer();
    $config = JFactory::getConfig();
    $sender = array( 
        $config->get( 'mailfrom' ),
        $config->get( 'fromname' ) 
    );

    $mailer->setSender($sender);

    $user = JFactory::getUser();
    $recipient = $sender;

    $mailer->addRecipient($recipient);

    $body = "Der Kunde: <b>" . $customers->name . "</b> hat neue Daten übermittelt<br>Ordner '" . $customers->upload_folder . "' checken" ;
    $subject = $customers->name . " hat neue Daten übermittelt";
    $mailer->isHTML(true);
    $mailer->Encoding = 'base64';
    $mailer->setSubject($subject);
    $mailer->setBody($body);

    $send = $mailer->Send();
    if ( $send !== true ) {
        echo 'Error sending email: ' . $send->__toString();
    } else {
        echo 'Mail sent';
    }
}

Model sql-query

$app = JFactory::getApplication('site');
$id = $app->input->getInt('id');
2

By default, each http request is independent. You have to send all parameters again in each call.

Alternatively, if your client can keep session cookies, then you can store server-side variables in user session to keep an state between http requests.

$session = JFactory::getSession();
$session->set('myId', $myId);
...
$session = JFactory::getSession();
$myId = $session->get('myId');

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.