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
Use the following:
Or shorter (thanks @Ivar):
Use empty() like: