Advertisement
Guest User

Untitled

a guest
Apr 18th, 2014
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.06 KB | None | 0 0
  1. $("#btn_go").on('click', function(){
  2. if(validateUserDetails() == false){
  3. return;
  4. }
  5. });
  6.  
  7. function validateUserDetails(){
  8. var bool = false;
  9.  
  10. $.ajax({
  11. url: 'response.php?type=validateUserDetails',
  12. type: 'POST',
  13. dataType: 'json',
  14. data: {name: $("#checkout_name").val(), email: $("#checkout_email").val(), "country": $("#checkout_country").val(),
  15. "city": $("#checkout_city").val()},
  16. success: function(data){
  17. console.log(data); // this is currently returning FALSE
  18. // Which is totally correct!
  19. if(data == true){ bool = true; }
  20. return trueOrFalse(bool);
  21. }
  22. });
  23. }
  24.  
  25. function trueOrFalse(bool){
  26. return bool;
  27. }
  28.  
  29. function validateUserDetails(){
  30.  
  31. return $.ajax({
  32. url: 'response.php?type=validateUserDetails',
  33. type: 'POST',
  34. async: false,
  35. dataType: 'json',
  36. data: {name: $("#checkout_name").val(), email: $("#checkout_email").val(), "country": $("#checkout_country").val(),
  37. "city": $("#checkout_city").val()},
  38. success: function(data){
  39. console.log(data); // this is currently returning FALSE
  40. }
  41. });
  42.  
  43. $("#btn_go").on('click', function(){
  44. validateUserDetails().done(function(data){
  45. if(data == "someValue")
  46. return "whatever you want";
  47. }
  48. });
  49.  
  50. function validateUserDetails(){
  51.  
  52. // asynchronous function
  53. $.ajax({...});
  54.  
  55. return undefined;
  56. }
  57.  
  58. function validateUserDetails() {
  59. var bool = false;
  60.  
  61. $.ajax({
  62. url: 'response.php?type=validateUserDetails',
  63. type: 'POST',
  64. async: false,
  65. dataType: 'json',
  66. data: {name: $("#checkout_name").val(), email: $("#checkout_email").val(), "country": $("#checkout_country").val(), "city": $("#checkout_city").val()},
  67. success: function(data) {
  68. console.log(data); // this is currently returning FALSE
  69. // Which is totally correct!
  70. if (data == true) {
  71. bool = true;
  72. }
  73. }
  74. });
  75.  
  76. return trueOrFalse(bool);
  77. }
  78.  
  79. function validateUserDetails() {
  80. var deferred = $.Deferred();
  81. var bool = false;
  82.  
  83. $.ajax({
  84. url: 'response.php?type=validateUserDetails',
  85. type: 'POST',
  86. dataType: 'json',
  87. data: {name: $("#checkout_name").val(), email: $("#checkout_email").val(), "country": $("#checkout_country").val(), "city": $("#checkout_city").val()},
  88. success: function(data) {
  89. console.log(data); // this is currently returning FALSE
  90. // Which is totally correct!
  91. if (data == true) {
  92. bool = true;
  93. }
  94. }
  95. complete: function () {
  96. deferred.resolve(trueOrFalse(bool));
  97. }
  98. });
  99.  
  100. return deferred.promise();
  101. }
  102.  
  103. function test() {
  104. var promise = validateUserDetails();
  105. promise.done(function(result) {
  106. console.log("Bool: " + result);
  107. });
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement