A current project of mine involves developing a PHP script that automatically sends birthday emails to individuals. To complement this, I am also working on creating a user-friendly web interface where I can toggle the program on or off, monitor who is celebrating their birthdays today, and check for any failed email deliveries. While the PHP functionality on the server side is functioning as intended, I am encountering difficulties with the HTML and Javascript components, particularly in regards to an Ajax request that fetches data from the server. The issue seems to be isolated to Internet Explorer 8, as I am not receiving a response from the server despite the readyState transitioning to 4. The status remains at 0, and responseText comes back empty.
After conducting research online, it appears that many people recommend utilizing JQuery to address similar issues. However, I have been unsuccessful in implementing JQuery across various browsers. Although I am interested in learning more about it due to its perceived simplicity, my primary focus currently is figuring out how to resolve the problem without relying on JQuery.
edit
In response to inquiries within the comments section, rearranging the if and else statements did not alter the outcome. Similarly, changing the connection from 'open' to 'close', which was originally meant to be close but modified in a moment of frustration, yielded the same results. Additionally, I have now included the relevant server-side PHP code. Upon making a request, the server responds with string data when the action is switchonoff, provides only a header (sans text) in case of clearlog, and returns a JSON array for initialize. Regardless of the request type, the server's HTTP status consistently remains at 0.
<script type="text/javascript">
function loadXMLDoc(action) {
var parameters = "action="+encodeURI(action);
var xmlhttp;
if (window.XMLHttpRequest) {
xmlhttp=new XMLHttpRequest();
}
else {
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("POST",'http://mailer.test/App/postscript.php',true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.setRequestHeader("Content-length", parameters.length);
xmlhttp.setRequestHeader("Connection", "open");
xmlhttp.send(parameters);
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState == 4 && xmlhttp.status==200) {
if(action == 'switchonoff'){
document.getElementById("onoffbutton").innerHTML=xmlhttp.responseText;
} else if(action == 'initialize'){
var response = JSON.parse(xmlhttp.responseText);
document.getElementById("onoffbutton").innerHTML=response['onoff'];
document.getElementById("birthdays").innerHTML=response['birthdays'];
document.getElementById("errors").innerHTML=response['errors'];
} else if(action == 'clearlog'){
document.getElementById("errors").innerHTML="";
}
}
}
}
window.onload = function() {
loadXMLDoc('initialize');
}
</script>
edit, here's the php script
<?php
include "settingschanger.php";
include "customercsv.php";
if (!isset($_POST['action'])) {
$_POST['action'] = 'dummy';
}
$post = $_POST['action'];
if(strcmp($post, "initialize") == 0) {
$settings = new SettingsChanger();
$onoff = $settings->getOnOff();
$csv = new CustomerCSV();
$csv->openFile((__DIR__).CustomerCSV::FILENAME);
$todaysbirthdays = $csv->getTodaysBirthdays();
$birthdays = "";
foreach ($todaysbirthdays as $row) {
$birthday = "";
foreach ($row as $data) {
$birthday .= $data . " ";
}
$birthdays .= $birthday . "<br />";
}
$errorLogArray = $settings->getErrorLog();
$errors = "";
foreach($errorLogArray as $line) {
$errors .= $line . "<br />";
}
$result = json_encode(array('onoff'=>$onoff, 'birthdays'=>$birthdays, 'errors'=>$errors));
header("HTTP/1.1 200 OK");
print $result;
}
if(strcmp($post, "switchonoff") == 0) {
$settings = new SettingsChanger();
$result = $settings->changeOnOff();
header("HTTP/1.1 200 OK");
print $result;
}
if(strcmp($post, "clearlog") == 0) {
$settings = new SettingsChanger();
$settings->clearLog();
header("HTTP/1.1 200 OK");
}
?>