I have read similar questions on this site, but none that I can find matches my problem closely.

I manage a joomla 3.5.1 site and I wrote a javascript that, when I press a button, sends a mail by asynchronously calling a php page that I placed on the joomla root.

The php file echoes a result ("Mail sent" or the content of an exception, with a try/catch structure).

Let's say the function works properly and the echo inside the catch runs (then exits).

echo "Mail sent";

The AJAX call should set the inner html of a span to "Mail sent"

  url: 'phpname.php',
  type: 'POST',
  datatype: 'text',
  data: 'option=com_ajax&a1=mail&a2=from&a3=...',
  success: function(data){

As you can see, the datatype is text (I also tried json or html, with no discernible results), yet console.log gives me this:

    Mail sent

The html of the resulting div as seen in chrome inspector is


          Mail sent</span>

Of course, when I implement an if/else inside the success part of ajax in order to color that text green if it is "Mail sent" and red otherwise, I always get red text.

When I tested the php and js files outside joomla, they worked fine. I used no option=com_ajax then, but apparently joomla needs it.

How should I proceed?

3 Answers 3


in your php file there should be something like this:


header('Content-Type: application/json');

echo json_encode($data);


so you will clean output - warning and notices

set encoding


stop rendering further

  • Do I really need to encode my echo as json? I have no idea how to recognize a success json from a failure json in my javascript file. Can't I just have it be plain ASCII text?
    – Zachiel
    May 28, 2016 at 12:28
  • your js susses function will be something as function(data){ console.log(data.myvar)} - I mean it will be easier for you to work with really. js data var will be an object. It really good practice and it will be easy from first time you try it )
    – Alexandr
    May 28, 2016 at 12:36
  • I tried it, but I just get a generic Failed Instantiation Error.
    – Zachiel
    May 30, 2016 at 17:34
  • can you email me your extension or share via github or bitbucket so I can check and help?
    – Alexandr
    May 30, 2016 at 17:46
  • Unfortunately, not. I only have access to it from my work PC, which is in my jobplace, and there I can not access here. Moreover, I think I'm already stretching the rules by repeating by memory parts of what I've done for my company (while getting paid for it). In the end I have decided to check if the expected message is contained in the output, rather than if it matches perfectly, and it's working fine.
    – Zachiel
    May 30, 2016 at 18:55

Make these couple of changes and it should work without any issues.

Change your Mail Sent statement to the following:

$this->sendMessage(JFactory::getApplication()->enqueueMessage('Mail Sent.','success'));

Add the following function to your PHP file that sends the mail:

public static function sendMessage($message, $error = false, array $data = array())
        echo new JResponseJson($data);
        JFactory::getApplication()->enqueueMessage($message, 'error');
        $response = new JResponseJson($data, $message, true);
        foreach($response->messages['error'] as $i => $message){
            if($message instanceof Exception){
                $response->messages['error'][$i] = $message->getMessage();
        echo $response;

Change your JavScript Ajax request to the following:

var result = $ajax({
  url: "phpname.php",
  type: "POST",
  data: {option: com_ajax, a1: mail, a2: from, a3: ...},
  success: function(result){
      var json = jQuery.parseJSON(result);

That should allow your success message to show up in the System message container.


I just have this problem too.

I use this php at the begining of my AJAX PHP responder.


It will only affects that ajax call and will prevent spoiling the json response with a notification or warning message.

  • How does setting error reporting to 0 affect the Ajax?
    – Lodder
    Oct 11, 2016 at 9:50
  • 1
    when a fucntion return something like: echo json_encode($response); jexit(); and there is a plugin or a module that produce a warning or notificaction (PHP) this will be in the output and will spoil the json response. Oct 11, 2016 at 10:12
  • But I need the response because I want to either say "mail sent" or to show the error. This way I will always get "mail sent".
    – Zachiel
    Jan 9, 2017 at 18:50

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.