0

I have a small javascript function and I am using ajax on it.

     function checklogin() {

     var unp = $("#input-36").val();
     var pwp = $("#input-35").val();

     $.ajax({
         url: '<?php echo site_url()."/login/main"?>',
         type: 'post',
         data: {
             un: unp,
             pw: pwp
         },
         success: function(data) {
             document.getElementById("errorid").innerHTML = data;
         },
         error: function(err, req) {
             alert("Your browser broke!");
         }
     });
     return false;
 }

What I want is: I want to return false only if there are some values in my data variable. Otherwise I want to return true.

But I can't return false inside this function:

success: function(data) {
             document.getElementById("errorid").innerHTML = data;
         },

So how should I do that?

Chris G
  • 787
  • 6
  • 20
pavithra
  • 316
  • 1
  • 5
  • 19
  • Don't get it. Please update your answer. – Léo Martin Jan 13 '16 at 14:25
  • Success and error are callbacks. They are called after post call result. They are asynchronous.. You can't return true or false. – Lipsyor Jan 13 '16 at 14:27
  • Use a temporary variable to store the data from the ajax call, then use a simple if statement after the ajax call to check for your 'values' and return false. – Rick Jan 13 '16 at 14:29
  • You are storing the value of `data` after getting the response, so why can't you check if `document.getElementById("errorid").innerHTML` is empty or no and then return necessary value? – Domain Jan 13 '16 at 14:29
  • return json response by login/main method and console.log(data). – MasoodRehman Jan 13 '16 at 14:55

0 Answers0