skip to Main Content

I want to pass an array to my AJAX function within the success function of an AJAX call. This array is data from my database using a global $wpdb; because this is easy to use and I know how to. Is there a way to pass this array to the AJAX function? Or is there a way to just get the data in AJAX?

I got this in my script tag and it is working:

    jQuery(document).ready(function(){

        jQuery('#date-select').on('change',function(){

        var seldate= jQuery(this).val();


        if(seldate!=''){

            var seldateEdited = seldate.replace("T", " ");

            jQuery.ajax({

                type:'POST',

                url:'http://esr-berlin.de/wp-admin/admin.php?page=einteilung_erstellen',

                data:'var='+seldate,

                success:function(html){

                            var output = '<option value="">Wähle HSR1</option>';

                            jQuery.each(html.data, function(i,s){
                                var newOption = s;

                                output += '<option value="' + newOption + '">' + newOption + '</option>';
                            });

                            jQuery('#hsrPop').empty().append(output);


                     }

            }); 

        }

    });
});

EDIT

I forgot something important. For the array or especially for the sql query I need a variable that I have in javascript. So I basically need another AJAX Call, right? How would I do that?

2

Answers


  1. You can encode the array in the PHP file and return this. It will just return a string to your AJAX function

    json_encode($array)
    

    https://www.php.net/manual/en/function.json-encode.php

    You can then use to convert it into a Javascript array for processing

    JSON.parse(json)
    

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse

    Login or Signup to reply.
  2. Assuming Your server side code(admin.php) something like:-

    $result = array();
    
    //your code here to fetch data dynamically
    
    $result['status'] = True; //based on your conditions optional
    $result['data'] = array(0=>"option1",1=>"option2",2=>"option3") ;
    echo json_encode($result);
    

    Hope this will help.

    jQuery(document).ready(function()
    {
            jQuery('#date-select').on('change',function()
            {
                var seldate= jQuery(this).val();
                    if(seldate!='')
                    {
                        var seldateEdited = seldate.replace("T", " ");
                        jQuery.ajax({
                                            type:'POST',
                                            dataType:'json',         //added to receive json format data
                                            url:'http://esr-berlin.de/wp-admin/admin.php?page=einteilung_erstellen',
                                            data:'var='+seldate,
                                            success:function(html)
                                            {     
                                             //here you can check if html.status is True/False .. optional
                                                    var output = '<option value="">Wähle HSR1</option>';
                                                    jQuery.each(html.data, function(i,s)
                                                    {
                                                        var newOption = s;
                                                        output += '<option value="' + newOption + '">' + newOption + '</option>';
                                                    });
                                                    jQuery('#hsrPop').empty().append(output);
                                             }
    
                                    }); 
    
                    }
    
            });
    });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search