tags:

views:

44

answers:

2

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  A: 

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

Grsm
There is a Try..Catch. Why I am not getting an exception alert?
RPK
A: 

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.

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