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;"> </div>
</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);
httpRequest.send();
}
function alertContents() {
if (httpRequest.readyState === XMLHttpRequest.DONE) {
if (httpRequest.status === 200) {
alert(httpRequest.responseText);
} else {
alert('There was a problem with the request.');
}
}
}
})();
</script>
</form>
EDIT
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.
<?php
$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);
$result->select($db->quoteName(array('trackandfieldresults')));
$result->from($db->quoteName('[TrackData]'));
$db->setQuery($result);
$row = $db->loadRowList();
echo $row['0']
?>
EDIT
Updated PHP File
<?php
$serverName = "ServerName";
$uid = "sqlusername";
$pwd = "sqlpassword";
$databaseName = "DBName";
$connectionInfo = array( "UID"=>$uid,
"PWD"=>$pwd,
"Database"=>$databaseName);
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn ) {
echo "Connection established.<br />";
}else{
echo "Connection could not be established.<br />";
die( print_r( sqlsrv_errors(), true));
}
?>