0

Below is my JQuery code. I just added an 'alert' to test whether data is retrieved correctly or not. But this code is not working. I think it is simply not able to get inside the file "filldistricts.php".

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.js" />

<script type="text/javascript">
$(document).ready(function(){
    $("#inDistrict").change(function(){
    $.getJSON("filldistricts.php",function(j){
        alert(j[0]);
    })
  })
})
</script>

Below I am giving the code of "filldistricts.php":

<?php
    $query = "SELECT districtid FROM districtmaster";
    try
    {
        $result = mysql_query($query);
        $c = "";

        while($row = mysql_fetch_assoc($result))
        {
            $c = $c.$row['districtid'].",";
        }
        $c = $c."Select";
        echo json_encode(array($c));
    }
    catch(exception $ex)
    {
        echo "<script type='text/javascript'>alert('".$ex."');</script>";
    }
?>

Where is the problem?

2 Answers 2

2

Maybe php is returning an exception? I thinks that doens't work with getJSON because it's not JSON..

Sign up to request clarification or add additional context in comments.

1 Comment

There is a Try..Catch. Why I am not getting an exception alert?
0

Since you're using getJSON in jQuery, the server return type should be JSON string, thus you cannot put javascript and alert() when there's an error.

Thus instead of

echo "<script type='text/javascript'>alert('".$ex."');</script>";

use

echo json_encode(array($ex)) ;

that will allow you to see the exception properly.

2 Comments

I removed the try..catch altogether and added error_reporting(E_ALL^E_NOTICE) on top.
did you try calling the php file from browser directly?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.