AJAX Response not returning any data when I use @mention

Below is my AJAX for updating rows in DB:

<script type='text/javascript'>
    $("#editstatus_form").submit(function(e) {
        $.ajax({
            type: "POST",
            url: "../../scripts/universal/update-status.php",
            data: $("#editstatus_form").serialize(), // serializes the form's elements.
            success: function(data) {
                $("#update_status").html(data);
                $("#updated_post").slideDown();
                setTimeout(function() {
                    $('#updated_post').slideUp();
                }, 3000);
            }
        });
        e.preventDefault();
    });
</script>

Now this code below is update-status.php

$sqlid = mysqli_real_escape_string($conn, $_POST['sqlid']);
$updated_status = mysqli_real_escape_string($conn, $_POST['updated_status']);
$date_edited = mysqli_real_escape_string($conn, $_POST['date_edited']);
$updateSQL = sprintf("UPDATE forum SET content_posting='$updated_status', date_edited='$date_edited' WHERE sqlid= '$sqlid'");

$input = $_POST['updated_status'];

preg_match_all("/#(\S{1,})/", $input, $matches);

foreach($matches[1] as $match){
    if (!file_exists('../../hashtag/' . $match)) {
        mkdir('../../hashtag/' . $match, 0755, true);
        copy("hashtagcontainer.php", "../../hashtag/$match/index.php");
    }
}

$Result1 = mysqli_query($conn, $updateSQL) or die(mysql_error());
if (!function_exists('convertAll')) {

    function convertAll($str) {
        $regex = "/[@#]([\w\.\-]+)/";
        $hrefs = ['#' => '../../hashtag/', '@' => '../../users/'];
        $result = preg_replace_callback($regex, function($matches) use ($hrefs) {
            return sprintf('<a href="%s%s/" style="color:#039BE5;text-decoration:none;">%s</a>', $hrefs[$matches[0][0]], strtolower($matches[1]), $matches[0]);
        }, $str);
        return($result);
    }
}
$status = convertAll($_POST['updated_status']);
echo nl2br($status);

?>

This all works (it updates the posts and everything) until a user tries to tag someone using the @.

If someone includes that in their post it will update in the DB and give me a 200 Response but this $("#update_status").html(data); does not work. Rather it empties #update_status.

Can anyone help me out here?

That should be mysqli_error() here… I don’t have an old enough PHP version at hand, but that would explain the empty response. Strange that it updates the DB correctly though… hm…

Note that calling die() by itself won’t produce an error code (and neither does a failed SQL query), so you’ll still have to call http_response_code(500) manually at this point.

Anyway, the convertAll() function and the JS are looking fine to me, so I think we can isolate the problem to the DB query… try adjusting the error response and see if you get a more meaningful value now.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.