0

EDIT: This is solved. Thank's to everyone who pointed me towards the error log files. Amazing community, this got answered so much faster than I expected. I'm embarred to say... it was a semicolon.

As a study project, I'm working on a home automation based on a Raspberry Pi. I installed Apache and built a small website that functions as a GUI. On click, data will be sent via POST-request to ajax.php (in the root folder) which then writes stuff into text files (that then will be read by a python script to do things with the GPIOs - not very elegant, but that's the task now).

Some relevant code:

$("#R0Light1_on").click(function(){

    var Value = $(this).val();
    var data = {'id': 'R0Light1_on', 'value': Value};
 
      $.ajax({
 
 type: 'POST',
 url: 'ajax.php',
 data: data,
 
 success: function(){
  console.log("#R0Light1_on", data);

   $("#R0Light1_on").addClass("on");
  $("#R0Light1_off").removeClass("off");

  },

 error: function() {alert('ERROR: $.ajax #R0Light1_on');}
 
      });
});
<?php
 
if (isset($_POST['id'])) {

 switch ($_POST['id']){

  case 'R0Light1_on':
      R0Light1_on();
  break;
 
  case 'R0Light1_off':
      R0Light1_off();
  break;

 }

}


function R0Light1_on() {

 $content = "Not relevant to this Question";
 $fp = fopen("/var/www/html/shares/pi/R0Light1_on.txt","wb");
 fwrite($fp,$content);
 fclose($fp);

 exit;
}


function R0Light1_off() {
...
}


?>
<button type="button" id="R0Light1_on"  value="1">an</button>

AJAX worked for a month now. 200 ok, all the time and $_POST['id'] did it's job. Then yesterday, it stopped doing so. I don't know what I did, but now it's throwing 500 Internal Server Error messages at me. Even using back-up files doesn't help me and I feel like I read everything Google could find me.

One Thing do I know, though: when I remove the if(isset($_POST['id']))-part, the HTTP-request is successful again (it just also make's the php-script useless, as obvious).

Btw, changing everything to GET doesn't help either (data is visible in URL, but the error stays the same).

Thanks for your time, I'd really appreciate any kind of help.

M.Grae
  • 21
  • 5

2 Answers2

0

The issue might be related permissions on the text file, or that it's failing to write to it. Adding permissions to the file would help resolve the issue.

One way to fix this yourself is by checking your apache/php error logs. That should give you a pretty clear indication where the problem lies.

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
idanoo
  • 96
  • 5
0

A 500 error is an internal server error.

You can check your PHP error_log file in the root directory of where the script runs or alternatively at the top of your script, add this to enable error reporting and see the real error that the 500 error translates to:

error_reporting(E_ALL); @ini_set('display_errors', 1);

It is most likely a PHP fatal error or syntax error.

contrid
  • 950
  • 9
  • 18