I need to fill form fields with post_meta from an ajax response.

Everything works properly.

I could hard-code the swapping of each piece of data.meta into its form field. But that approach is not reusable.

Is there a way to loop through the data.meta instead?

In some cases, there may not be meta for all the form fields. And in some cases, there may be meta that is not applicable to the form.

$.ajax({

    ...

    success: function (data) {

        if (data.status === 'success') {

            $('#title').val(data.title);

            $.each(data.meta, function(key,value) {

                  alert(key + '---' + value[0]); 

                  // if key is a form element, add the value to that element

                  // if key is a form element that is a select dropdown, 
                  //  then mark that option as selected

            });

        }
    }
});

Read more here: fill form fields with ajax response


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: