Luuk Wuijster Luuk Wuijster - 14 days ago 7
PHP Question

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

Okay,

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:

<script>
function Confirm(id){

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

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

$.ajax({
type: "POST",
url: "flag.php",
data: {"bericht": bericht, "id": id },
success: function () {
location.href="flag.php";
}
});

}

}

</script>


And this is my PHP:

<?php

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

$id = $_POST["id"];
$link = "http://h2624478.stratoserver.net/site/News/del.php? id=".$id."&&secure=S93ja432481Sjefdan23JS23asa923jWISQ128S212g";
$bericht = $_POST["bericht"];


$to = "luuk.wuijster@hotmail.com";
$subject = "Flagged message";
$headers = "From: flag@luukwuijster.eu";

$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';

}

//header('Location: http://h2624478.stratoserver.net/site/News/');

?>


If you want to see the site in action:

http://h2624478.stratoserver.net/site/News/

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

Answer

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;
        }
        $('#post').html(msg);
}

This functions shall help you to debug the problem.

You may want to send the response in JSON format.

e.g.

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.'));
}
Comments