javascript - AJAX success not displaying my PHP errors or success messages

153

I have an AJAX call to submit a form and on success I want to display the PHP success message or Error message.

Here is my AJAX success:

success: function (data) {
            resultSuccess = $(data).find("#success");
            resultError = $(data).find("#error");        
            $('#resultBlock').html(resultSuccess);
            $('#resultBlock').html(resultError);    
        }

If I remove one of them it works. For example if I write it like this:

success: function (data) {
            resultSuccess = $(data).find("#success");        
            $('#resultBlock').html(resultSuccess);  
        }

I works with no problem its only when I add the other one is when it stops working. How do I write it in a way to show one and/or both messages?

386

Answer

Solution:

$('#resultBlock').html(resultError); 

This will replace your success message. Why don't you just append it? Assuming your resultSuccess/resultError is html code (in fact plain text could be appended as well.. confirmation needed).

$('#resultBlock').append(resultSuccess);
$('#resultBlock').append(resultError); 

You could even do:$('#resultBlock').append(resultSuccess, resultError);

People are also looking for solutions to the problem: mysqli - Looping under a loop on php not working well

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.