0

I have a script which uses a jQuery AJAX call to return a number between 0-5 and simply changes the value of a textbox with the result. When the script returns 1-5 this works fine and prints the result in the textbox accordingly.

However, on the occasions the script returns 0, nothing gets printed. I'm guessing this is a default behaviour for treating 0 but the problem is 0 is a valid response for the script I am writing and I need it to actually print '0' in the textbox.

I can post my code if necessary but it's a standard basic jQuery AJAX call to a PHP script which echoes the result (0-5) and then the value of a textbox is updated using the AJAX response.

Edit: Code is below as requested

function fetchEWSSScore(input,category) {
    $.ajax({
        type: "POST",
        url: "EWSSTriggerLevel.php",
        data: "input=" + input + "&category=" + category,
        async: true,
        success: function(msg){                       
        EWSS_Score = msg;
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
         EWSS_Score = 'Invalid';
         alert(" Status: " + textStatus + "\n Error message: "+ errorThrown); 
         }
    });

    if (isNaN(EWSS_Score)) {
        EWSS_Score = 'Invalid';
        alert("An invalid response was given while attempting to fetch EWSS Score.");
        return false;
    } else {
    return EWSS_Score;
    }
    }


function calculateEWSSWeightedScore(input,category,weighted) {
    var EWSSScore = fetchEWSSScore(input,category);
    $(weighted).val(EWSSScore);
    }

$('#heartRate').change(function() {
    var category = "HR";
    var input = $(this).val();
    calculateEWSSWeightedScore(input,category,'#heartRateWeighted');
    });

Edit 2: Thanks to @FelixKling for pointing out a bug in my script I didn't even notice existed. However I still have the issue with 0 not being printed.

Roy
  • 705
  • 2
  • 11
  • 32
  • 6
    Yes, please post your code. – Felix Kling Jun 25 '13 at 09:11
  • You could convert your result to a string... But yes, some code would help – Brewal Jun 25 '13 at 09:14
  • javascript treat it as "false" result ? return it inside a json : json_encode({"result":"0"}); – IdanHen Jun 25 '13 at 09:16
  • 1
    @IdanHen: The response is text though and the string `"0"` is truthy. – Felix Kling Jun 25 '13 at 09:18
  • 1
    Uh? Your code shouldn't work at all, since `fetchEWSSScore` returns before the response from the Ajax call was received. Please refer to [this question](http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call) to learn how to work with callbacks. This could actually be your problem. – Felix Kling Jun 25 '13 at 09:51
  • 1
    Also make sure that the response actually contains `0` and is no empty. – Felix Kling Jun 25 '13 at 09:59
  • @FelixKling Thanks for the link but as you can see it's actually a synchronous AJAX request so this is not the issue; as I said it works perfectly when returning 1-5. In this instance I think the issue is 0 being treated as false. – Roy Jun 25 '13 at 10:00
  • 1
    You have `async: true,`, so how can it be synchronous? Even if it was evaluated as false, there is no part in your code where this would have an impact. You are never testing anything apart from `if (isNaN(EWSS_Score))` and `isNaN("0")` or `isNaN(0)` is surely `false`. – Felix Kling Jun 25 '13 at 10:02
  • @FelixKling Sorry I meant asynchronous. 0 is not meant to represent false, in the project I am coding for it is a valid value. Whatever is returned can range from 0-5 and they are all valid values. My issue is I want it to print the returned value from 0-5, but it only prints 1-5. – Roy Jun 25 '13 at 10:06
  • @FelixKling My apologies, I have re-read the link you sent me and I now see the issue with the code I've written and asynchronous AJAX requests. This looks like a good opportunity for me to learn how to use deferred objects/promises. Unfortunately it doesn't solve my issue with 0 not being printed :\ – Roy Jun 25 '13 at 10:15
  • 1
    Here, I created a jsFiddle based on your code but changed it to use promises. I cannot reproduce the problem though, it works fine for me: http://jsfiddle.net/VHnJA/. – Felix Kling Jun 25 '13 at 10:24
  • @FelixKling Thanks very much, I was about to have a stab at promises myself but this is a great stepping stone which will save me a lot of trial and error. I will integrate this new code and report back if I still have the 0 issue. – Roy Jun 25 '13 at 10:27
  • Try using `parseInt(msg)` in the response to force it to use the integer. – casraf Jun 25 '13 at 10:27
  • @FelixKling When I integrate your solution I can see using 'inspect element' in Chrome that it returns a number but all that is returned in the textboxes is [object Object]. How would I access the number to print in the textbox? – Roy Jun 25 '13 at 10:42
  • 1
    It's funny because I had the same problem first :D I forgot to define `EWSSScore` parameter in `fetchEWSSScore(input,category).done(function(EWSSScore) { ... });`. So inside the callback, `EWSSScore` was actually referring to the return value of `fetchEWSSScore`. In that line (line 17 in my code example), you can actually omit the `var EWSSScore =` part (I forgot to delete it). It's probably a similar problem in your code. – Felix Kling Jun 25 '13 at 10:48

1 Answers1

0

I would suggest to return a number between 1-6 and subtract 1 to produce the desired result. Would put it in the comments but I don't have the rep.

John Langan
  • 51
  • 1
  • 6
  • Unfortunately I don't have this flexibility because the numbers 0-5 are pulled from a database which is also used elsewhere so I cannot modify them – Roy Jun 25 '13 at 09:39