Guest User

Guest User

  • Tech Writer
  • 2.1k
  • 470.7k

How to read values in Ajax call and change status?

Nov 6 2019 7:11 AM
Hi Team
 
I have two buttons in my HTML, one accept value as being 0 and other one as being 1. Now i want to read these values in Ajax call request. When these are changed, it must show status message to a user the value has been changed from 0 to 1 or vice versa. Currenly my channels are only accepting either one fire first and can view these status on my dashboard channel in thingspeak. Here is the code both HTML and Javascript.
 
<!-----HTML Buttons------>
  1. <div class="col-md-2 text-center">  
  2. <button id="singlebutton" name="singlebutton" class="btn btn-primary">Subscribe</button> <br>  
  3. </div>  
  4. <!------  
  5. ---->  
  6. <br/>  
  7. <div class = "col-md-2 text-center">  
  8. <button id = "singlebtn" name="singlebtn" class="btn btn-primary">Unsubscribe</button> <br>  
  9. </div>  
<!----Jquery Buttons------>
  1. <script src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>  
  2. <script>  
  3. $(document).ready(function(){  
  4. $.ajax({  
  5. url:'https://api.thingspeak.com/update?api_key=D***&field8=0',  
  6. type:'GET',  
  7. data:{  
  8. format:'text'  
  9. },  
  10. success:function(response){  
  11. alert(response);  
  12. },  
  13. error:function() {  
  14. $('#error').text("There was an error processing your request.Please try again");  
  15. $('#singlebutton').append(data);  
  16. }  
  17. });  
  18. // second button for unsubscribe.  
  19. $(document).ready(function(){  
  20. $.ajax({  
  21. url:'https://api.thingspeak.com/update?api_key=D***&field8=1',  
  22. type:'GET',  
  23. data:{  
  24. format:'text'  
  25. },  
  26. success:function(response){  
  27. alert(response);  
  28. },  
  29. error:function(){  
  30. $('#error').text("There was an error processing your request.Please try again");  
  31. $('#singlebtn').append(data);  
  32. }  
  33. });  
  34. });  
  35. });  
  36. </script>  

Answers (25)