简体   繁体   中英

jquery ajax response not sent

I have a form that submits its content with post method to php script, which returns an xml file. I find problem in detecting what is wrong, I haven't seen any error in firefox console, this is the jquery code:

$(document).ready(function() {
$('form').submit( function() {
//alert('bonjour');
var pr= $('#prenom').val(); 
var te=$('#tech').val();
var sk=$('#skills').val() ; 
$("#form1").ajaxError(function(){
alert("An error occured!");
});
$.ajax({
url: "filter.php",
type: "POST",
data: { prenom: pr , tech: te , skills: sk },

dataType: "xml",
success: function(data) {
//alert(data);
$(data).find('collaborateur').each(function(){

var id = $(this).attr('id');
var urlPhoto = $(this).find('urlphoto').text();
var nb_cv = $(this).find('nb_cv').text();
var poste = $(this).find('poste').text();
var nb_ann_exp = $(this).find('nb_ann_exp').text();
var identite = $(this).find('identite').text();
var nom = $(this).find('nom').text();
var prenom = $(this).find('prenom').text();
$('<div class="items" id="link_'+id+'"></div>').html('<a href="'+urlPhoto+'"  

style="float:left">'+nom+'</a><span>'+nom+' '+prenom+'</span>').appendTo('#result');

}

);
//$('#result').html(data);
alert('Load was performed.');
},
fail: function(){alert ('fail');}
});
});
}); 

the php code for generating xml filter.php is below

 header('Content-type: text/xml');
 echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
 echo '<collaborateurs>';
 include("connection.php");
 require("functions.php");
 $tab = array();
 $tab = filter($_POST['prenom'], $_POST['tech'], $_POST['skills']);
 // print_r($tab);
 // exit;
 foreach  ($tab as $key => $value)
 {
 echo '<collaborateur id="'.$key.'">';
 echo '<nom>'.$value['nom'].'</nom>';
 echo '<prenom>'.$value['prenom'].'</prenom>';
 echo '<nb_cv>'.$value['nb_cv'].'</nb_cv>';
 echo '<poste>'.$value['poste'].'</poste>';
 echo '<nb_ann_exp>'.$value['nb_exp'].'</nb_ann_exp>';
 echo '<identite>'.$value['ident'].'</identite>';
 // echo '<nb_visite>'.$value['nb_visite'].'</nb_visite>';
 echo '</collaborateur>';
 } 
 echo '</collaborateurs>';

the filter function

function filter($pren, $tech, $comp)
{global $connection;
$tab = array();
$query="SELECT  DISTINCT c.id_c,`prenom`,`nom`,`nb_exp`,`ident`,`poste` , NomPhoto,   

nb_cv  FROM `collaborateur` c, techno_mai m , competence_cle cc  where( prenom like     

'$pren'  or (m.lib_tech like '$tech' and m.id_c =c.id_c )  or (cc.id_c = c.id_c and   

cc.lib_cc like '$comp')) ";

$c12=mysql_query($query, $connection)or die(mysql_error());
$i =0;
while($fil= mysql_fetch_array($c12))
{
$tab[$i]['prenom'] =$fil['prenom'];
$tab[$i]['nom']= $fil['nom'];
$tab[$i]['nb_exp']=$fil['nb_exp'];
$tab[$i]['ident']=$fil['ident'];
$tab[$i]['poste']=$fil['poste'];
$tab[$i]['NomPhoto']=$fil['NomPhoto'];
$tab[$i]['nb_cv']=$fil['nb_cv'];
$tab[$i]['nom']=$fil['nom'];
$tab[$i]['prenom']=$fil['prenom'];
$i++;
}
return $tab;

}

I tested the php code in a form without ajax and it renders a correct xml file. the server output (xml file) without ajax

<collaborateurs>
<collaborateur id="0">
<nom>semi</nom>
<prenom>troc</prenom>
<nb_cv>0</nb_cv>
<poste>tech</poste>
<nb_ann_exp>12</nb_ann_exp>
<identite>dis</identite>
</collaborateur>
<collaborateur id="1">
<nom>tarek</nom>
<prenom>fellah</prenom>
<nb_cv>0</nb_cv>
<poste>dev</poste>
<nb_ann_exp>2</nb_ann_exp>
<identite>dev web</identite>
</collaborateur>
<collaborateur id="2">
<nom>ahmed</nom>
<prenom>hamza</prenom>
<nb_cv>0</nb_cv>
<poste>dev web</poste>
<nb_ann_exp>0</nb_ann_exp>
<identite>dev web</identite>
</collaborateur>
</collaborateurs>

And thanks in advance.

You are setting

data: { prenom: pr , te: tech , sk: skills },

but then retriving

 $tab = filter($_POST['prenom'], $_POST['tech'], $_POST['skills']);

should be

data: { prenom: pr , tech: te , skills: sk},

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM