Dismiss
Announcing Stack Overflow Documentation

We started with Q&A. Technical documentation is next, and we need your help.

Whether you're a beginner or an experienced developer, you can contribute.

Sign up and start helping → Learn more about Documentation →

I can't call an arrays passed from the php json encode. I would concatenate 2 select state-contry-city. How can I do this??

This is the code:

I have 2 selects. I send the first value to an action:

php action into my controller:

  public function regioniProvinceComuniAction(){

         $response = $this->getResponse();
         $request = $this->getRequest();

         if ($request-> isPost()){
            $post_data = $request->getPost();
            $regione = (int)$post_data['regione'];
            $province = (int)$post_data['province'];                
         }

        if(isset($regione)){
            $prov = $this->getProvincia($regione);                
        }

         if(isset($province)){
            $com = $this->getComuni($province);                
        }

          $response->setContent(\Zend\Json\Json::encode(array('prov' => $prov, 'com' => $com)));
          return $response;    
     }

This is my js file:

  $("select#Regione").change(function () {
       var regione = $("select#Regione option:selected").attr('value');
       $.ajax({
           type: 'POST',
           url: '/zf-tutorial/public/regioni-province-comuni',
           datatype: 'json',
           data: { regione: regione },
           success: function (data) {

           alert (data);  
           }               
       });
  });

An example of the alert function is like this:

{"prov":[{"id":"10","nome":"Genova"},{"id":"8","nome":"Imperia"},{"id":"11","nome":"La Spezia"},{"id":"9","nome":"Savona"}],"com":[]}

I tried to populate my second select via data.post like this, but the values are "undefined":

success: function (data) { 
             $('select#Provincia option').each(function(){$(this).remove()});
             for(var i=0; i<data.length; i++){
                 $("select#Provincia").append('<option value="' + data[i].prov.id + '">' + data[i].prov.nome + '</option>');
             }

i tried to do

for (var i = 0; i < data.prov.length; i++) {
                    $("select#Provincia").append('<option value="' + data.prov[i].id + '">' + data.prov[i].nome + '</option>');
                }

if i set manually the var data, works fine, but with the response data doesn't work. i tried to do:

var prov = data.prov;
$("div#id").html(prov);

but it doesn't work. works only like this:

$("div#id").html(data);

and the output is the same

{"prov":[{"id":"10","nome":"Genova"},{"id":"8","nome":"Imperia"},{"id":"11","nome":"La Spezia"},{"id":"9","nome":"Savona"}],"com":[]}
share|improve this question
    
check my answer here: stackoverflow.com/questions/25720697/… – dixromos98 Sep 26 '14 at 12:55
up vote 0 down vote accepted

You need to loop through the items in data.prov. Change your for loop to this:

for (var i=0; i<data.prov.length; i++){
    $("select#Provincia").append('<option value="' + data.prov[i].id + '">' + data.prov[i].nome + '</option>');
}

jsFiddle Demo

EDIT

Solution found in comments: Can you try to change datatype in your $.ajax() call to dataType. I'm not sure if the case is important. Sounds like data is a string and not an object

share|improve this answer
    
hi, thanks but doesn't work! if i set manually the data var, works fine, but if i use the data.post response, doesn't work :( – Alessandro Corradini Sep 25 '14 at 15:34
    
@AlessandroCorradini, is the output of the data.post response different then what you provided above? – labue Sep 25 '14 at 15:36
    
nope, it's the same! i just created a div element and i put the data output with the function: $("#id").html(data); and the output is: {"prov":[{"id":"10","nome":"Genova"},{"id":"8","nome":"Imper‌​ia"},{"id":"11","nom‌​e":"La Spezia"},{"id":"9","nome":"Savona"}],"com":[]} – Alessandro Corradini Sep 25 '14 at 15:58
    
i tried to do like this in js file: var prov = data.prov; $("#id").html(prov); but doesn't work. – Alessandro Corradini Sep 25 '14 at 16:23
    
@AlessandroCorradini, can you try to change datatype in your $.ajax() call to dataType. I'm not sure if the case is important. Sounds like data is a string and not an object – labue Sep 25 '14 at 16:38

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.