skip to Main Content

I’ve recently started using and learning JS and jquery. I’m sending a user email from a form to my PHP page to check if any user exists in DB via ajax, then I get a response from my PHP file if the user exists or not and display the text in a div. It’s working fine, but because the page doesn’t refresh when the button for sending form data is pressed, ajax just stacks the response from PHP.

I probably need to clear the response every time the button is clicked first and then append the response but I can’t find the right command for it :/

JS

function sendnewpass() {
        var email = document.getElementById("useremail").value;

        $.ajax({
            type : "POST",
            url  : "login.php", //my php page
            data : { passemail : email },  //passing the email
            success: function(res){  //to do if successful
                $("#emailsentmsg").append(res);  //the the div that i put the response in
            }
        });

}

2

Answers


  1. Use the following:

    $("#emailsentmsg").html("");
    $("#emailsentmsg").append(res);
    

    Or shorter (thanks @Ivar):

    $("#emailsentmsg").html(res);
    
    Login or Signup to reply.
  2. Use empty() like:

    $("#emailsentmsg").empty().append(res);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search