1

This is a follow-up to a previous question.

What I need to do is color the cell only after the process successfully updates the record.

$('#example1').on('blur', 'tr > td > .editForecast', function(e) 
{
  e.preventDefault();
  var forecastnewval = $(this).val();
  var processData = '';

  if(this.value !== $(this).attr('value'))
  {
    $.post('api/inlineEditProcess.php', {forecastnewval:forecastnewval}, function(data)
    {
     processData = data; // this is what I'm trying to get now
     console.log('this is inside ' + processData); // this prints processData
    }

    console.log('this is outside ' + processData); // this does not print processData

    $(this).css('background-color', '#99FF66'); // this changes the background color of the cell
  }
  else
  {
    console.log('nothing changed');
  }
});

If you'll notice in the code above, I'm trying to retrieve the variable processData from inside the $.POST and use it in another IF/ELSE statement.

The variable processData will be a string that either reads 'success' or 'fail'. This obviously comes from a PHP process script that runs an UPDATE query, which then returns the words specified.

I want to utilize processData in an IF/ELSE that will change the background color of the cell either to green (for success) or red (for fail).

I simply want to retrieve the variable that was created from within the $.POST.

Can I do this, and if so, how?

1
  • 1
    if you declare the variable processData outside the post function, it should work. You are probably going to print undefined right now because you declared the variable inside the function's scope Commented Jan 22, 2018 at 15:48

1 Answer 1

2

Your variable processData is enclosed within a function so will return null when evaluated outside of it.

$('#example1').on('blur', 'tr > td > .editForecast', function(e) {
  e.preventDefault();
  var forecastnewval = $(this).val();
  var processData = "";

  if (this.value !== $(this).attr('value')) {
    $.post('api/inlineEditProcess.php', {
      forecastnewval: forecastnewval
    }, function(data) {
      processData = data; // this is what I'm trying to get now
    });

    console.log(processData); // this does not print processData

    $(this).css('background-color', '#99FF66'); // this changes the background color of the cell
  } else {
    console.log('nothing changed');
  }
});

Sign up to request clarification or add additional context in comments.

2 Comments

I updated my code. Please see the console.logs - I am only able to print processData from inside the Post. I cannot access it outside of the post. Thoughts?
Yes, the post function was not closed correctly. Ammended in my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.