2

After I submit my form I want to retrieve some data from MySQL through an ajax request but I have some problems with my getJSON function.

This works:

$("form[id=bilddatenbank_sortiment_anlegen]").submit(function() {
    var array_sortimentsname;
    $.getJSON('inc/inc.sortimentsname.php', function(array_sortimentsname) {
        console.debug(array_sortimentsname);
    });
    return false;
});

I need to access array_sortimentsname outside of the getJSON function, how would I do this?

1
  • Have you set the headers in php to text/json? Also make a new variable like var parsedJson = null; and in the function to get the json you can do someting like parsedJson = array_sortimentsname; and then you can access it outside the function. Commented May 29, 2017 at 7:32

3 Answers 3

1
$("form[id=bilddatenbank_sortiment_anlegen]").submit(function() {

    var array_sortimentsname;
    var parsedJson = null;

    $.getJSON('inc/inc.sortimentsname.php', function(array_sortimentsname) {

        parsedJson = array_sortimentsname;
        //console.debug(array_sortimentsname);

    });

    console.debug(parsedJson);

    return false;

});

Like this? I get an my console: null

Yes, I have edited the header.

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

Comments

1

Okey, with this code, I got on the first submit null and after my second submit my data. Sync problems?

var array_sortimentsname; var parsedJson = null;

$("form[id=bilddatenbank_sortiment_anlegen]").submit(function() {

$.getJSON('inc/inc.sortimentsname.php', function(array_sortimentsname) {

    parsedJson = array_sortimentsname;
    //console.debug(array_sortimentsname);

});

console.debug(parsedJson);

return false;

});

Comments

0

You should declare a variable outside of the Ajax function, so you can access it when the result come form the server. Something like this:

let sortedData = null;
$("form[id=bilddatenbank_sortiment_anlegen]").submit(function() {

    $.getJSON('inc/inc.sortimentsname.php', function(array_sortimentsname)
    sortedData = array_sortimentsname
    console.debug(array_sortimentsname);
});

return false;
});

Comments

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.