I have a PHP file that will echo results in the end of the file. I want to fire some Javascript from an onclick event that will call this PHP file and print the results on screen. My Javascript looks like this, but I get a 500 error, and am not sure why.

What is incorrect in my syntax? Or what is the proper way to do this in Joomla 3.5?

<form id="HomePage" method="post">
<div style="padding-top: 10px;"><input type="submit" value="Submit" id="ajaxButton" /></div>
<div style="padding-top: 10px;">&nbsp;</div>
<script type="text/javascript">
(function() {
  var httpRequest;
  document.getElementById("ajaxButton").onclick = function() { makeRequest('Test.php'); };

  function makeRequest(url) {
    httpRequest = new XMLHttpRequest();

    if (!httpRequest) {
      alert('Giving up :( Cannot create an XMLHTTP instance');
      return false;
    httpRequest.onreadystatechange = alertContents;
    httpRequest.open('GET', url);

  function alertContents() {
    if (httpRequest.readyState === XMLHttpRequest.DONE) {
      if (httpRequest.status === 200) {
      } else {
        alert('There was a problem with the request.');

This is the contents of my Test.php. Does this look incorrect? If I add this directly to an article, it returns the result that I want, but for some reason I can't get the syntax correct for calling it from a separate article.

    $option = array(); 

    $option['driver']   = 'mssql';
    $option['host']     = 'host';
    $option['user']     = 'user';       
    $option['password'] = 'password';   
    $option['database'] = 'database';     
    $option['prefix']   = '';            

    $db = JDatabase::getInstance( $option );
    $result = $db->getQuery(true);
    $row = $db->loadRowList();
    echo $row['0']

Updated PHP File


$serverName = "ServerName"; 
$uid = "sqlusername";   
$pwd = "sqlpassword";  
$databaseName = "DBName"; 

$connectionInfo = array( "UID"=>$uid,                            

$conn = sqlsrv_connect( $serverName, $connectionInfo);

if( $conn ) {
     echo "Connection established.<br />";
     echo "Connection could not be established.<br />";
     die( print_r( sqlsrv_errors(), true));

1 Answer 1

  1. I just pasted your code into an article on the home page of a joomla 3.6 site and put a Test.php at the root of the too and it worked fine. To my mind if you are doing exactly that then there is a problem with your php file cause in this request Joomla is not really involved (Note for 500 errors the first thing I would look at would be the file's permissions).

  2. This approach is fine if your the only dev on the site and your php doesn't interact with Joomla at all. Joomla provides a simple way to make ajax requests with modules using the com_ajax interface: doc is here. This would be neater, more portable, less prone to accidental breakage, and give you access to the Joomla application - on the other hand, the Joomla app has to load fully so you'll lose some microseconds.

  • If I go to localhost/Test.php I get a 500 error and the page is not displayed. That would be indicative of permissions, right? Feb 23, 2017 at 14:28
  • Just comment out all your php and write echo "hello"; and if that throws a 500 then you've got permission issues. If it says hello then you've got a code issue and you should focus on getting your errors to display. Feb 24, 2017 at 0:48
  • The echo statement worked. My php file is exactly as it is in my OP, which works for your set-up. Could it be a setting I need to change in apache to permit this to occur? Feb 24, 2017 at 1:43
  • 1
    i just noticed you added the php in your test.php file to your op. That php won't work because it uses JDatabase! If you are calling the php file from some javascript then the Joomla application is not called in that request. Hopefully that makes sense. So, either you need to rewrite your db call to not use a joomla library, or rewrite the whole structure of your request using a module connected to com_ajax as mentioned in part 2 of my original answer. Feb 24, 2017 at 2:52

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.