在AJAX和php之间发送数据

I have this AJAX

   function  getValFromDb() {
   var university = document.getElementById('category').value;
   var domain = document.getElementById('subcategory').value;
   var year = document.getElementById('an').value;
   var url = "modules/mod_data/tmpl/script.php";

  var params = 'uni='+university+'&fac='+domain+'&an='+year;
    if (window.XMLHttpRequest) { AJAX=new XMLHttpRequest(); } else { AJAX=new ActiveXObject("Microsoft.XMLHTTP"); }
    if (AJAX)
    {
        AJAX.open("POST", url, false);
        AJAX.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        AJAX.onreadystatechange = function()
        {
            if(AJAX.readyState == 4 && AJAX.status == 200)
            {
             document.getElementById('materie').innerHTML = AJAX.responseText;
            }
        }; 

    } AJAX.send(params);
    }

an send params here

<?php
defined('_JEXEC') or die;
$db = JFactory::getDbo();

$an=$_POST('an');
$fac=$_POST('fac');
$uni=$_POST('uni');
  $result1 =mysql_query("SELECT DISTINCT (materie) FROM drv_uni_$uni WHERE an='$an'");
while($row = mysql_fetch_array($result1)){
    $display_string .= "<option value=\"".$row['id']."\">". $row['materie'] ."</option>";
} 

  echo $display_string;
?>

POST from firebug

      Parametersapplication/x-www-form-urlencoded
      an    an-3
      fac   Finante Banci
      uni   ase
      Source
      uni=ase&fac=Finante Banci&an=an-3

And HEADERS

     Response Headers

        Connection  keep-alive
      Content-Encoding  gzip
       Content-Type text/html
       Date Sun, 30 Dec 2012 13:12:47 GMT
      Server    xServers
      Transfer-Encoding chunked
       X-Powered-By PHP/5.3.16

Request Headers

       Accept   text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
      Accept-Encoding   gzip, deflate
      Accept-Language   en-US,en;q=0.5
        Connection  keep-alive
     Content-Length 33
        Content-Type    application/x-www-form-urlencoded; charset=UTF-8
       Cookie   0dea1a59adf779fae552e16f3d646b24=91481693c6b3c8615aa9ce57461aa590; 33a66e84ea7ae1332853383606d933e1=2039ee6737b4252207431790f5f7a939
              DNT   1
        Host    www.domain.ro
       Referer  http://www.domain.ro/upload
       User-Agent   Mozilla/5.0 (Windows NT 6.1; rv:17.0) Gecko/20100101 Firefox/17.0

THE RESPONSE IS EMPTY!

the Js doesn.t receive anything from php. The PHP have take the values corectly but when it send it doesnt get back to js. If you wanna show me how to use JSON or to corect this code i apreciate.

You can debug the above scenario as follow.

  1. Check whether your PHP page is being hit by the AJAX query. To do that echo "something" and see the That shows on the firebug response tab. defined('_JEXEC') or die; might be stopping the script and etc.

  2. Make sure AJAX URL is correct and POST data are passed correctly to the PHP side.

  3. Check whether your MYSQL query is correct and it returns the data you want.

  4. Make sure you retrive the response from the PHP side and shows it in the AJAX success function.

  5. Watch out JS error during the process. To make sure you have no errors you can use firebug.

Try changing defined('_JEXEC') or die; to define('_JEXEC', 1) or die;. Note I have used define instead of defined. This has been an issue for a lot of people integrating Ajax in their extension as defined('_JEXEC') or die; usually blocks it for security reasons.