11540 sujets

JavaScript, DOM et API Web HTML5

Bonjour,

J'ai une fonction qui envoie des données POST notamment un array (les valeurs d'une liste multiple), mais je ne parviens pas à récupérer ce tableau dans mon fichier php appelé...

Voici mon code :


function courtier_change() {

	$.post("../ajax/ajax_maj_courtier.php", {
		recherche : true,
		is_courtier : $('#is_courtier').val() // valeur de mon select
		
	}, function(data){
		eval(data);
	});
	
} // end courtier_change




et dans ajax_maj_courtier.php



<?php
	session_start();
	
	$is_courtier = $_POST['is_courtier'];
	
	$retour_ajax.= "alert('".$is_courtier[0]."');"; // je souhaite récupérer le 1er élément de mon select
       
       echo utf8_encode($retour_ajax);




Je ne reçois aucun alert.... une idée ?
Modifié par SolMJ (03 Oct 2008 - 12:08)
Je ne suis pas sûr que l'alert() javascript de la réponse soit évalué et lancé par la fonction de callback...

Pourquoi ne pas plutôt utiliser :

function(data){
    alert("Data Loaded: " + data);
  }

et enlever l'alert() dans $retour_ajax en ne gardant que $is_courtier[0] ?
l'alert est évalué, ça fonctionne pour les autres données, c'est le transfert du tableau qui pose problème visiblement... Smiley ohwell
Trouvé :

en fait il faut indiquer en option quel est le type de données retournées par la réponse.
Extrait de la doc jquery :
a écrit :

dataType String Default: Intelligent Guess (xml or html)
The type of data that you're expecting back from the server. If none is specified, jQuery will intelligently pass either responseXML or responseText to your success callback, based on the MIME type of the response. The available types (and the result passed as the first argument to your success callback) are:

* "xml": Returns a XML document that can be processed via jQuery.
* "html": Returns HTML as plain text; included script tags are evaluated when inserted in the DOM.
* "script": Evaluates the response as JavaScript and returns it as plain text. Disables caching unless option "cache" is used. Note: This will turn POSTs into GETs for remote-domain requests.
* "json": Evaluates the response as JSON and returns a JavaScript Object.
* "jsonp": Loads in a JSON block using JSONP. Will add an extra "?callback=?" to the end of your URL to specify the callback. (Added in jQuery 1.2)
* "text": A plain text string.


Modifier le code d'origine en :

function courtier_change() {
	$.post("../ajax/ajax_maj_courtier.php", {
        	recherche : true,
               is_courtier : $('#is_courtier').val() 
	}, function(data){
		eval(data);
	}, [#red]"script"[/#]);

}
Tu as raison le fait ajouter l'option "script" doit simplement dispenser certainement d'utiliser eval()...
PiR2 a écrit :
Trouvé :

en fait il faut indiquer en option quel est le type de données retournées par la réponse.
Extrait de la doc jquery :

dataType String Default: Intelligent Guess (xml or html)
The type of data that you're expecting back from the server. If none is specified, jQuery will intelligently pass either responseXML or responseText to your success callback, based on the MIME type of the response. The available types (and the result passed as the first argument to your success callback) are:

* "xml": Returns a XML document that can be processed via jQuery.
* "html": Returns HTML as plain text; included script tags are evaluated when inserted in the DOM.
* "script": Evaluates the response as JavaScript and returns it as plain text. Disables caching unless option "cache" is used. Note: This will turn POSTs into GETs for remote-domain requests.
* "json": Evaluates the response as JSON and returns a JavaScript Object.
* "jsonp": Loads in a JSON block using JSONP. Will add an extra "?callback=?" to the end of your URL to specify the callback. (Added in jQuery 1.2)
* "text": A plain text string.


Modifier le code d'origine en :

function courtier_change() {
	$.post("../ajax/ajax_maj_courtier.php", {
        	recherche : true,
               is_courtier : $('#is_courtier').val() 
	}, function(data){
		eval(data);
	}, [#red]"script"[/#]);

}


j'ai pas trop compris en quoi c'est censé m'aider, mais merci de ta participation Smiley biggrin (comme je l'ai dit, mon code de retour est bien évalué)

Sinon j'ai contourné le problème, j'ai transformé mon tableau en chaine avant de le "poster".
Modifié par SolMJ (03 Oct 2008 - 15:12)