Load a php file on ajax success

Hi,

I’ve asked a similar question yesterday, but now I am trying a different way.

I’ve got a div class called news. It contains some updates from users. When a user submits a new update, I want my new update to be appear at the top of the news class. I tried the following, but it doesn’t work the way I want. When I submit the form, news div is disappear (because my prepend() is empty, but I also want to load the latest.php because it contains this particular user’s last update)

$(document).ready(function () {
    $('#new').submit(function (e) {
        e.preventDefault();
        var form = $(this);
        var formData = form.serialize();
        $.ajax({
            type: 'POST',
            url: '/modules/feed/process.php', // the url where we want to POST
            data: formData, // our data object
            success: function () {
                $("#statustext").val(''); // I reset textarea's value here
                $(".news").prepend().load("/modules/feed/latest.php"); // I want to load latest.php at the top of the news div but I can't, because prepend() is empty. If I load it without prepend, the new status appears but the other updates disappear.
            }
        });
    });
});

Try using the following instead, to first retrieve the content and to then add it.

$.get("/modules/feed/latest.php", function(data) {
    $(data).prependTo(".news");
});
1 Like

try this:

$(".news").prepend('<div></div>').load("/modules/feed/latest.php");

or use @Paul_Wilkins solution which is better

1 Like

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