Php array from mySql wont display in DOM as json object

I dont know what to call this problem . I have a jquery ajax
get statement:

<code>
  function fill_contact(evt) <!--display click event-->

            {

             var country_id = evt.target.id
             document.getElementById('country_name').firstChild.data = country_id
                 

                            <!-- Perform an asynchronous HTTP (Ajax) request. -->     
                                 $.ajax({ 

                                             url:'../core/database/mapString.php', <!--the script to call to get data  -->                                                                         

                                            data:{"country_name": country_id }, <!-- pass the country name as a name/value pair.  -->
                                            type:'GET',
                                           dataType: 'json',                <!-- data format -->                                                                       
                                            success: function(json_data){    <!--Function for showing data. -->

                                            <!--    Update html content -->      
                              console.log(json_data);                                                                                

                                <!--Set output element html -->                                                                               

                                  $('#firstname').html(json_data.firstname);
                                  $('#lastname').html(json_data.lastname);
                                  $('#email').html(json_data.email);
                                  $('#photo').html(json_data.photo);
                                  },
                                  <!--error  handling. -->                                   
                                  error:function(xhr, status, error) {
                                            //var err  = eval("(" + xhr.responseText + ")");
                                    $('<divid="fb-root" />').html(xhr.responseText).prependTo('body');
                                    },             

                              }); 

            }</code>

This works and sends the country_name to the server and the
sql gets it and processes it. When I echo the result, it shows the correct
result. If the country clicked has no information ie. if empt, then the array
posts the correct values object and they appear in the correct places in the
DOM. If however the country has information: then it doesn’t display. ???

<code>
 if (isset($_GET["country_name"])) { 

    $country_name = $_GET["country_name"];

            /** run prepare to select records **/    
            $stmt =$dbh->prepare("SELECT firstname, lastname, email, photo FROM reps WHERE country = :country_name");
            $stmt->bindParam(':country_name',$country_name, PDO::PARAM_STR);
            $stmt->execute();

            /**Values to fill up the form **/                             
            $result= $stmt->fetch();              
            }

            /**create the array **/

            $json_data=array();
             
            /**Check if there is data, if not give default data **/                     

            if(empty($result)){

            $json_data  = array( 'firstname' =>'www.aeegroup.co.za',
                             'lastname' =>' ',
                             'email' => 'africa@aeegroup.co.za',
                             'photo' => '../images/Afrizone2.png');
            } 
            else {                     //******* How do I build this array from database results? *******//

            $json_data  = array( 'firstname' =>$result['firstname'],
                                         'lastname' => $result['lastname'],
                                         'email' => $result['email'],
                                          'photo' => $result['photo']);
                            /**Test if the info is there **/
                            echo $json_data['firstname'] . " " . $json_data['lastname'] . '<br/>';
                            echo $json_data['email'] . '<br />';   
            }

            /**built in PHP function to encode the data in to JSON format  **/         
            header('Content-Type:application/json');    
             echo json_encode($json_data);
             return $json_data;         /*** close the database connection ***/
            $dbh =null;       </code>

Could you help me with where I am missing it?

Thanks in advance for your valuable time.

A quotation mark?
Notice in your post the “=>” after 'email. is red instead of black like the ones before it.

Thanks for pointing that out. It was only in this post. Fixed it up like in the origonal. Now it shows properly.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.