Luuk Wuijster Luuk Wuijster - 1 year ago 52
PHP Question

Ajax doesn't work anymore, while it did before without changing anything


I made a "news" site where you can post articles. And you can see them at the news page. I have a flag system, so you can flag it when you need to. When you click the flag button, a javascript confirm pops up asking you if you really want to flag that message. When you click confirm the id variable and article variable gets post to a php script that emails me with a link to delete it and the article.

But it doesn't work anymore after I changed my VPS from Ubunu 16.04 to Debian 8.

It doesn't pass the data to the php script. And I just don't know why.

This is my javascript code:

function Confirm(id){

if (confirm("Weet je zeker dat je dit bericht wilt vlaggen?")){
var vid = '#' + id ;

var bericht = $(vid).html();

type: "POST",
url: "flag.php",
data: {"bericht": bericht, "id": id },
success: function () {




And this is my PHP:


// this is just to test if the POST is empty or not. (its empty)

$id = $_POST["id"];
$link = " id=".$id."&&secure=S93ja432481Sjefdan23JS23asa923jWISQ128S212g";
$bericht = $_POST["bericht"];

$to = "";
$subject = "Flagged message";
$headers = "From:";

$compleet = "Bericht: \r\n \r\n " . $bericht . "\r\n \r\n" . $link;

if(!empty($bericht)) {

if (mail($to, $subject, $compleet, $headers)) {
echo 'Het bericht is geflaged, en word beken.';

} else {

echo 'Er is wat mis gegaan, probeer opnieuw.';


} else {

echo 'error 2';




If you want to see the site in action:

Its all in dutch but you should be able to understand it. (atleast I hope so)

Answer Source

Adding dataType: 'json' to your jQuery ajax would solve the problem.

Try modifying the error function in jQuery to this

error: function (jqXHR, exception) {
        var msg = '';
        if (jqXHR.status === 0) {
            msg = 'Not connect.\n Verify Network.';
        } else if (jqXHR.status == 404) {
            msg = 'Requested page not found. [404]';
        } else if (jqXHR.status == 500) {
            msg = 'Internal Server Error [500].';
        } else if (exception === 'parsererror') {
            msg = 'Requested JSON parse failed.';
        } else if (exception === 'timeout') {
            msg = 'Time out error.';
        } else if (exception === 'abort') {
            msg = 'Ajax request aborted.';
        } else {
            msg = 'Uncaught Error.\n' + jqXHR.responseText;

This functions shall help you to debug the problem.

You may want to send the response in JSON format.


if (mail($to, $subject, $compleet, $headers)) {
    echo json_encode(array('success'=>true,'msg'=>'Het bericht is geflaged, en word beken.'));
} else {
    echo json_encode(array('success'=>false,'msg'=>'Er is wat mis gegaan, probeer opnieuw.'));
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download