0

I use the following Ajax call to fetch translations from a db.
The call itself works and returns the below result.

Can someone tell me how I can access the values from the array in the Ajax result so that I can assign them to my variables ? I either get an error or empty variables and couldn't find a way to make this work (I am new to arrays).

Note: The id in this case is a unique identifier so the Ajax call always returns only one row.

Ajax call:

$.ajax({
    type: 'POST',
    url: 'fetchTrans.php',
    data: {
        transId: transId
    },
    success: function(result){                  
        var transDE = ''; // should be result['de']
        var transEN = ''; // should be result['en']
        var transDA = ''; // should be result['da']
        var transES = ''; // should be result['es']
        var transFR = ''; // should be result['fr']
        var updateDate = ''; // should be result['updateDate']
        
        $('#transId').val(transId);
        $('#langDE').val(transDE);
        $('#langEN').val(transEN);
        $('#langDA').val(transDA);
        $('#langES').val(transES);
        $('#langFR').val(transFR);
        $('#updateDate').val(updateDate);
    }
});

Ajax result (example):

array (
  'de' => 'Ziffer',
  'en' => 'digit',
  'da' => 'cifret',
  'es' => 'dígito',
  'fr' => 'chiffre',
  'updateDate' => '2020-05-09 19:40:58',
)
3
  • What is the result that is received in JS? Commented Jun 23, 2020 at 10:24
  • @VLAZ: result is what I posted above. It is the result from a PHP MySQLi query. Commented Jun 23, 2020 at 10:28
  • But what is the data that JS gets, that is my question. What is result - is it a string? Is it wrapped in something? Commented Jun 23, 2020 at 10:29

1 Answer 1

1

What I'd do if I where you:

First encode the array as json in PHP:

echo json_encode($array);

so that you will end up with a JSON returned in the ajax call.

Then I'd change the AJAX accordingly (note that I add a JSON type not to rely on the jquery dataType autodetection:

$.ajax({
    type: 'POST',
    url: 'fetchTrans.php',
    dataType: 'JSON',
    data: {
        transId: transId
    },
    success: function(result){
        $('#transId').val(transId);
        $('#langDE').val(result.de);
        $('#langEN').val(result.en);
        $('#langDA').val(result.da);
        $('#langES').val(result.es);
        $('#langFR').val(result.fr);
        $('#updateDate').val(result.updateDate);
    }
});

Also, as you note I skipped assigning the result to variables. Unless you don't need those variable for something else in the response it is completely useless

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

2 Comments

Thanks a lot for this - this looks great. Will try now and report back.
This works fantastic - thanks again ! Will accept this as the right answer as soon as it is possible.

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.