Home reading json created with php using ajax
Reply: 1

reading json created with php using ajax

Challadia Chawki
1#
Challadia Chawki Published in 2017-12-07 20:22:54Z

good day guys. so i'm stuck with this this code and apparently i didn't know how to transfer the json created with php to my ajax code because it keeps giving me errors.

here's my php code:

if($_GET["type"]== "doc"){
    if(isset($_GET["name"])){
        $doc= $con->getDoctors($_GET["gov"],$_GET["munip"],$_GET["spec"],$_GET["name"]);
        $output ="[";
        foreach($doc as $d){
            if($d != end($doc))
                $output .=$d->inquire().",";
            else
                $output .=$d->inquire();
        }
        $output .="]";
        echo json_encode($output);
    }

so the php does give a valid json i've checked it with jsonlint and the transfer occurs correctly to the browser with i simply print out the response directly without any operations done to it. apparently the problem is with the JSONparse function in js, not sure but since i'm not that experience with both ajax and rendering json with php i'm stuck here.

here is the js code:

<body>
<div class="result">
</div>
<script>
var div = document.getElementsByClassName("result")[0];
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function(){
    var output = '';
    var response = JSON.parse(xhr.responseText);
    if(this.readyState == 4 && this.status==200){
        for(var i in response){
            output +='<div class="res"><h4>'+response[i].last+" "+response[i].first+'</h4><span>';
            output += response[i].address+"</span><span>";
            var phone = response[i].phone;
            for(var j in phone){
                if(phone[j] != phone[phone.length -1])
                    output += phone[j]+ " - ";
                else
                    output += phone[j];
            }
            output += "</span></div>"
        }
    }
   };
  xhr.open('GET','php/inquire.php?
  type=doc&gov=tunis&munip=kram&spec=oncology&name=chawki',true);
  xhr.send();
  </script>
  </body>
  </html>

here is the error i keep getting

Uncaught SyntaxError: Unexpected end of JSON input
at JSON.parse (<anonymous>)
at XMLHttpRequest.xhr.onreadystatechange (test.html:34)
at test.html:50

so i hope you guys have an answer for my problem. thanx all for your time and help.

the inquire function:

public function inquire(){
    $doctor = new stdClass();
    $doctor->phones = new stdClass();
    $doctor->work_days = new stdClass();
    $doctor->first = $this->fname;
    $doctor->last = $this->lname;
    if($this->work_mail)
        $doctor->email = $this->work_mail;
    else
        $doctor->email = $this->email;
    $doctor->speciality = $this->speciality;
    $doctor->degree = $this->degree;
    $doctor->address = $this->address;
    $doctor->phones = json_decode($this->connection->getPhones("doc",$this->id));
    $doctor->work_days = json_decode($this->connection->getTime("doc",$this->id));
    return json_encode($doctor);
}

the get phones function:

public function getPhones($user,$id){
    try{
        if($user=="doc"){
            $stmt = $this->con->prepare("SELECT * from doc_phone where user= ?");
            $stmt->execute(array($id));
        }elseif($user=="phar"){
            $stmt = $this->con->prepare("SELECT * from phar_phone where user= ?");
            $stmt->execute(array($id));
        }
        $output = new stdClass();
        while($res = $stmt->fetch()){
            if($res->phone1)
                $output->num1 = $res->phone1;
            if($res->phone2)
                $output->num2 = $res->phone2;
            if($res->phone3)
                $output->num3 = $res->phone3;
        }
        return json_encode($output);
    }catch(PDOException $e){
        throw new Exception($e->getMessage());
    }
}

the getTime function:

public function getTime($user,$id){
    try{
        if($user=="doc"){
            $stmts = $this->con->prepare("SELECT * from doc_time where user= ?");
            $stmts->execute(array($id));
        }elseif($user=="phar"){
            $stmts = $this->con->prepare("SELECT * from phar_time where user= ?");
            $stmts->execute(array($id));
        }
        $output = new stdClass();
        $output->days = new stdClass();
        $output->time = new stdClass();
        $output->time->mid_week = new stdClass();
        $output->time->weekend = new stdClass();
        while($stmt = $stmts->fetch()){
            if($stmt->sun)
                $output->days->sun = $stmt->sun;
            if($stmt->mon)
                $output->days->mon = $stmt->mon;
            if($stmt->tue)
                $output->days->tue = $stmt->tue;
            if($stmt->wed)
                $output->days->wed = $stmt->wed;
            if($stmt->thu)
                $output->days->thu = $stmt->thu;
            if($stmt->fri)
                $output->days->fri = $stmt->fri;
            if($stmt->sat)
                $output->days->sat = $stmt->sat;
            if(strlen($stmt->mid_week_time)== 23){
                $output->time->mid_week->morning = substr($stmt->mid_week_time,0,11);
                $output->time->mid_week->evening = substr($stmt->mid_week_time,12,11); 
                $output->time->mid_week->whole = "";
            }else{
                $output->time->mid_week->morning = "";
                $output->time->mid_week->evening = ""; 
                $output->time->mid_week->whole = $stmt->mid_week_time;
            }
            if(strlen($stmt->weekend_time)== 23){
                $output->time->weekend->saturday = substr($stmt->weekend_time,0,11);
                $output->time->weekend->sunday = substr($stmt->weekend_time,12,11); 
            }elseif(strlen($stmt->weekend_time)== 11 && $stmt->sat){
                $output->time->weekend->saturday = substr($stmt->weekend_time,0,11);
                $output->time->weekend->sunday = ""; 
            }elseif(strlen($stmt->weekend_time)== 11 && $stmt->sun){
                $output->time->weekend->saturday = "";
                $output->time->weekend->sunday = substr($stmt->weekend_time,0,11); 
            }else{
                $output->time->weekend->saturday = "";
                $output->time->weekend->sunday = ""; 
            }
        }
        return json_encode($output);
    }catch(PDOException $e){
        throw new Exception($e->getMessage());
    }
}
Barmar
2#
Barmar Reply to 2017-12-07 20:35:55Z

You shouldn't be concatenating strings before creating the JSON. Construct an array and call json_encode() on that.

    $doc= $con->getDoctors($_GET["gov"],$_GET["munip"],$_GET["spec"],$_GET["name"]);
    $output = array();
    foreach($doc as $d){
        $output[] = json_decode($d->inquire());
    }
    echo json_encode($output);

If you call json_encode() on a string, then when Javascript calls JSON.parse() it just gets a string, not an array.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.317831 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO