Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
61
rated 0 times [  64] [ 3]  / answers: 1 / hits: 150325  / 10 Years ago, sun, may 11, 2014, 12:00:00

My button uses AJAX to add information to the database and change the button text. However, I wish to have the button disabled after one click (or else the person can spam the information in the dataabase). How do I do this?



HTML



<button class=btn btn-lg btn-primary id='roommate_but' onclick='load(<?php echo $user_id;?>)'><span class=glyphicon glyphicon-plus></span> Add Person</button>


Javascript / AJAX / JQuery



<script src=https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js></script>

function load(recieving_id){
if (window.XMLHttpRequest){
xmlhttp = new XMLHttpRequest();
} else{
xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
}

xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200){
document.getElementById(roommate_but).innerHTML = xmlhttp.responseText;


}

}

xmlhttp.open('GET','include.inc.php?i=' + recieving_id, true);

xmlhttp.send();


}

More From » jquery

 Answers
27

*Updated



jQuery version would be something like below:



function load(recieving_id){
$('#roommate_but').prop('disabled', true);
$.get('include.inc.php?i=' + recieving_id, function(data) {
$(#roommate_but).html(data);
});
}

[#71074] Friday, May 9, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
zainsagez

Total Points: 555
Total Questions: 99
Total Answers: 96

Location: Honduras
Member since Sat, Jul 24, 2021
3 Years ago
;