0

How to use dataType in ajax when you sending data to server and returning value is echo json_encode("1") which is text not json, please check the below code for ajax:

 $.ajax({
                       type: "GET",
                       url: "http://www.pzishkstan.com/MobileApps/controller.php?city="+$city+"&disease="+$disease+"&name="+$name
                       crossDomain: true,
                       contentType: "application/x-www-form-urlencoded; charset=utf-8",
                       dataType: "json",
                       data:'type=register',
                       success: function(data) {


                           if(data=="1"){

                                alert("yes");

                           }else{
                              alert("no");
                           }
                       },
                       error: function(e) {
                           alert('Error: ' + e.message);
                       }
                    });

and my php code:

    $name=$_GET["name"];
                  $city=$_GET["city"];
                  $disease=$_GET["disease"];
                  $birth=$_GET["birth"];
                  $start=$_GET["start"];
                  $graduate=$_GET["graduate"];
                  $email=$_GET["email"];
                  $mobile=$_GET["mobile"];
                  $pass=$_GET["pass"];
                  $web=$_GET["web"];
                  $certi=$_GET["certificate"];
                  $social=$_GET["social"];
                  $uni=$_GET["uni"];
                  $note=$_GET["note"];
                  $gender=$_GET["gender"];

            $explode = explode('/', $_GET["image"]); 

           $image="upload/".array_pop($explode).".png"; 



 $queryNumber="INSERT INTO doctors (Name,Birthdate,StartDate,GraduateDate,Mobile,Email,password,proPic,Website,Gender,Certifications,Socials,City,
  GraduationUniversityName,Notes,regDate) values('$name','$birth','$start','$graduate','$mobile','$email','$pass','$image','$web','$gender','$certi','$social','$city','$uni','$note',
  CURRENT_TIMESTAMP)";

                    $numberOfRows = mysqli_query($link,$queryNumber) or die(mysql_error());


                    if($numberOfRows>0){

                        $query=mysqli_query($link,"select * from doctors where Mobile='$mobile'");
                        while($row=mysqli_fetch_array($query)){
                            $id=$row["DoctorsId"];
                            mysqli_query($link,"INSERT INTO specializationdoctor (doctorId,specializationId) values($id,$disease)");
                        }

                       echo json_encode(array("response" => "1"));


                    }
                else{
                        echo json_encode(array("response" => "0"));
                    }

My error is :

enter image description here

I have been searched in google I couldn't find any perfect answer.

ROR
  • 271
  • 3
  • 29
  • 1
    `echo json_encode("1")` **will** give you JSON, as will `echo json_encode(array("response" => "1"));` – Quentin Feb 05 '15 at 18:58
  • 1
    You need to use the Net tab in your browser's developer tools to find out what the server is actually returning. – Quentin Feb 05 '15 at 18:59
  • **Danger**: You are **vulnerable to [SQL injection attacks](http://bobby-tables.com/)** that you need to [defend](http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php) yourself from. – Quentin Feb 05 '15 at 18:59
  • @Quentin what's your advise ? – ROR Feb 05 '15 at 19:04
  • My advice is: "use the Net tab in your browser's developer tools to find out what the server is actually returning" and to follow the links to learn about SQL injection. – Quentin Feb 05 '15 at 19:06
  • Also to look at the JavaScript error console, and the other arguments that get passed to the error handler by jQuery. – Quentin Feb 05 '15 at 19:07

1 Answers1

1

in php you can write:

  if(....){
              echo json_encode(array("name" => "yair", "email" => "yair@yair.com"));
            }
        else{
             echo json_encode(array("name" => "unknown", "email" => "unknown@unknown.com"));
            }

in javascript:

....
success: function(e){
     alert('Your name is '+e.name);
     alert('Your email is '+e.email);
     else .....
}

hope that helps :)

Yair.R
  • 795
  • 4
  • 11
  • yes I agree with you , you're right but if I want to send back multi values like name and email and password and location for the user , how ? please your advise \ – ROR Feb 05 '15 at 18:37
  • seems like you have an error in your php file. Catch the exception and see how to fix it. – Yair.R Feb 05 '15 at 18:52
  • check it out please now – ROR Feb 05 '15 at 18:57
  • I can't check it like that, you have to turn on the error_log and see in which line it crash.. – Yair.R Feb 05 '15 at 18:58