tags:

views:

81

answers:

4

When I uncomment the alert the data is there... like:

{ 
      'Huishoudelijke hulp': 'Huishoudelijke hulp', 
      'Verpleging thuis': 'Verpleging thuis',
      'Verzorging thuis': 'Verzorging thuis',
      '24 uurs zorg': '24 uurs zorg',
      'Ondersteunende begeleiding': 'Ondersteunende begeleiding',
    }   

But instead of populating the key and the value it takes the whole var and start to create a key and value pair for each character.

You can see this in action here: http://www.zorgzuster-zeeland.nl/site/static/calendar_test.php

create a task in the calendar and then try to edit the task by clicking on it. It should populate the dropdown field properly.

When i create a static var with the same values the dropdown works.

static variable

var zvmlist = { 
      'Huishoudelijke hulp': 'Huishoudelijke hulp', 
      'Verpleging thuis': 'Verpleging thuis',
      'Verzorging thuis': 'Verzorging thuis',
      '24 uurs zorg': '24 uurs zorg',
      'Ondersteunende begeleiding': 'Ondersteunende begeleiding',
    }; 

This is my function, anybody has a clue?

$.get('get_zorgvormen.php', function(zvmlist) {
        //alert("Data Loaded: " + zvmlist);

            $.each(zvmlist, function(key, value) { 
              var selected='';
              if(key==eventdata.title){var selected='selected' }
              $('<option value="'+key+'" '+selected+'>'+value+'</option>').appendTo($('#calendar_edit_entry_form_title')); 

            }); 

        }); 
+2  A: 

Your server responds with Content-Type: text/html instead of application/json, so jQuery doesn't eval your object. You could specify the content type:

$.get('get_zorgvormen.php', function(zvmlist) {
    $.each(zvmlist, function(key, value) { 
        var selected = '';
        if(key==eventdata.title) { 
            selected = 'selected';
        }
        $('<option value="'+key+'" '+selected+'>'+value+'</option>')
            .appendTo($('#calendar_edit_entry_form_title')); 
   }); 
}, 'json'); 

But I would recommend you to fix your server side script to send the correct content-type and jquery will automatically recognize the json format and eval the response of the server.

Darin Dimitrov
i added the proper headers but still no luck<?phpheader('Cache-Control: no-cache, must-revalidate');header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');header("Content-type: application/json; charset=utf-8");header("Content-Transfer-Encoding: 8bit");$query = "SELECT id,zorgvormen_Naam FROM zorgvormen WHERE parent_zorgvormen_id=0";$result = mysql_query($query) or die(mysql_error());echo "{";while($row = mysql_fetch_array($result)){ echo "'". $row['zorgvormen_Naam']. "':'". $row['zorgvormen_Naam']. "',"; }echo "}";?>
Ritz
Your initial test link no longer makes an ajax call to this script.
Darin Dimitrov
i added . json at the end of my function. so i removed it again.
Ritz
A: 

You dont tell Jquery to expect a json format.

From the documentation:

jQuery.get( url, [ data ], [ callback(data, textStatus, XMLHttpRequest) ], [ dataType ] )

urlA string containing the URL to which the request is sent.

dataA map or string that is sent to the server with the request.

callback(data, textStatus, XMLHttpRequest)A callback function that is executed if the request succeeds.

dataTypeThe type of data expected from the server.

source

Try to specify that you expect Json:

$.get('get_zorgvormen.php', function(zvmlist) {
        //alert("Data Loaded: " + zvmlist);

            $.each(zvmlist, function(key, value) { 
              var selected='';
              if(key==eventdata.title){var selected='selected' }
              $('<option value="'+key+'" '+selected+'>'+value+'</option>').appendTo($('#calendar_edit_entry_form_title')); 

            }); 

        }, 'json');
DaNieL
i added this but still not working, thx for your effort
Ritz
A: 

try this:

$.ajax({
    "url":"get_zorgvormen.php",
    "dataType":"json",
    "type":"get",
    "success":function(zvmlist){
        // should alert [Object object] or something similar
        alert(zvmlist);
    },
    "error":function(msg){
        alert(msg);
    }
});
David Murdoch
id does alert Object object, how doe i pass de zvmlist to my each function then?
Ritz
it is maybe obvious for intermediate jquery guys, but i am a newbie in jquery
Ritz
it did the trick,it is working now.Thanks a lot David!
Ritz
Would you like to mark it as the accepted answer? :-)
David Murdoch
clicking the V i presume? did it.
Ritz
A: 

David Murdock provided "the trick". This is how my function looks now, and its working like a charm.

$.ajax({
        "url":"get_zorgvormen.php",
        "dataType":"json",
        "type":"get",
        "success":function(zvmlist){

            $.each(zvmlist, function(key, value) { 
              var selected='';
              if(key==eventdata.title){selected='selected' }
              $('<option value="'+key+'" '+selected+'>'+value+'</option>').appendTo($('#calendar_edit_entry_form_title')); 

            });
        },
        "error":function(msg){
            alert(msg);
        }
    });
Ritz