IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

AJAX Discussion :

[AJAX] Syntaxe ajax et javascript


Sujet :

AJAX

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre à l'essai
    Homme Profil pro
    Inscrit en
    Janvier 2013
    Messages
    4
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Corrèze (Limousin)

    Informations forums :
    Inscription : Janvier 2013
    Messages : 4
    Par défaut [AJAX] Syntaxe ajax et javascript
    Bonjour
    Je fait appel à une fonction javascript dans un tableau pour insérer une nouvelle ligne.
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    function AddRow()
    {
    	var i = parseFloat(document.forms["form"].elements["i"].value);
        var newRow = document.getElementById('tableau').insertRow(i+1); // Nouveau TR
        var newCell = newRow.insertCell(0); // Nouveau TD
        newCell.innerHTML = '<input type="text" id="produit" name="produit" onkeyup="ajax_showOptions(this, getProduit, event);" size="45" autocomplete="off"><input type="hidden" id="produit_hidden'+i+'" name="produit>">';
        newCell = newRow.insertCell(1); // Nouveau TD
        newCell.innerHTML = '<input name="quantite'+i+'" id="quantite'+i+'" type="text" value="1" size="10" style="text-align:right;" onkeydown="sendData2('+i+'), sendData3('+i+');" onkeyup="calcul_montant('+i+');">';
        newCell = newRow.insertCell(2); // Nouveau TD
        newCell.innerHTML = '<td><span id="condi'+i+'>"></span></td>';
        newCell = newRow.insertCell(3); // Nouveau TD
        newCell.innerHTML = '<td><span id="prix'+i+'"></span></td>';
        newCell = newRow.insertCell(4); // Nouveau TD
        newCell.innerHTML = '<input name="montant'+i+'" id="total'+i+'" type="text" size="10" maxlength="8" style="text-align:right;"/>';
        document.forms["form"].elements["i"].value = i+1; // Ajout de 1 dans le champ de nombre de ligne
        document.getElementById('tableau').className='tableau'; // On applique la classe donnees_tableau à la ligne créée
    }
    Sur le 1 er input j'utilise une fonction d’auto-complétion qui me permet de choisir un produit, mais j'ai cette erreur :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     getProduit is not defined
    ajax_showOptions(this, getProduit, event);
    Je pense que c'est juste un problème de syntaxe mais je trouve pas
    Merci d'avance.

  2. #2
    Modérateur

    Avatar de NoSmoking
    Homme Profil pro
    Inscrit en
    Janvier 2011
    Messages
    17 207
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Isère (Rhône Alpes)

    Informations forums :
    Inscription : Janvier 2011
    Messages : 17 207
    Par défaut
    Bonsoir,
    difficile de dire à la simple vue de ton code, mais qu'est getProduit, il est défini où, fonction ou variable...

  3. #3
    Membre à l'essai
    Homme Profil pro
    Inscrit en
    Janvier 2013
    Messages
    4
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Corrèze (Limousin)

    Informations forums :
    Inscription : Janvier 2013
    Messages : 4
    Par défaut
    Alors je vais essayer d'être un peu plus explicite
    Voilà la classe javascript qui permet l'éxecution de la fonction ajax_showOptions()
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    	/************************************************************************************************************
    	(C) www.dhtmlgoodies.com, April 2006
     
    	This is a script from www.dhtmlgoodies.com. You will find this and a lot of other scripts at our website.	
     
    	Terms of use:
    	You are free to use this script as long as the copyright message is kept intact. However, you may not
    	redistribute, sell or repost it without our permission.
     
    	Thank you!
     
    	www.dhtmlgoodies.com
    	Alf Magne Kalleland
     
    	************************************************************************************************************/	
     
    	var ajaxBox_offsetX = 0;
    	var ajaxBox_offsetY = 0;
    	var ajax_list_externalFile = '../ajax/ajax-list.php';	// Path to external file
    	var minimumLettersBeforeLookup = 1;	// Number of letters entered before a lookup is performed.
     
    	var ajax_list_objects = new Array();
    	var ajax_list_cachedLists = new Array();
    	var ajax_list_activeInput = false;
    	var ajax_list_activeItem;
    	var ajax_list_optionDivFirstItem = false;
    	var ajax_list_currentLetters = new Array();
    	var ajax_optionDiv = false;
    	var ajax_optionDiv_iframe = false;
     
    	var ajax_list_MSIE = false;
    	if(navigator.userAgent.indexOf('MSIE')>=0 && navigator.userAgent.indexOf('Opera')<0)ajax_list_MSIE=true;
     
    	var currentListIndex = 0;
     
    	function ajax_getTopPos(inputObj)
    	{
     
    	  var returnValue = inputObj.offsetTop;
    	  while((inputObj = inputObj.offsetParent) != null){
    	  	returnValue += inputObj.offsetTop;
    	  }
    	  return returnValue;
    	}
    	function ajax_list_cancelEvent()
    	{
    		return false;
    	}
     
    	function ajax_getLeftPos(inputObj)
    	{
    	  var returnValue = inputObj.offsetLeft;
    	  while((inputObj = inputObj.offsetParent) != null)returnValue += inputObj.offsetLeft;
     
    	  return returnValue;
    	}
     
    	function ajax_option_setValue(e,inputObj)
    	{
    		var i = document.getElementById("i").value;
    		if(!inputObj)inputObj=this;
    		var tmpValue = inputObj.innerHTML;
    		if(ajax_list_MSIE)tmpValue = inputObj.innerText;else tmpValue = inputObj.textContent;
    		if(!tmpValue)tmpValue = inputObj.innerHTML;
    		ajax_list_activeInput.value = tmpValue;
    		if(document.getElementById(ajax_list_activeInput.name + '_hidden'+i))document.getElementById(ajax_list_activeInput.name + '_hidden'+i).value = parseInt(inputObj.id); 
    		ajax_options_hide();
    	}
     
    	function ajax_options_hide()
    	{
    		if(ajax_optionDiv)ajax_optionDiv.style.display='none';	
    		if(ajax_optionDiv_iframe)ajax_optionDiv_iframe.style.display='none';
    	}
     
    	function ajax_options_rollOverActiveItem(item,fromKeyBoard)
    	{
    		if(ajax_list_activeItem)ajax_list_activeItem.className='optionDiv';
    		item.className='optionDivSelected';
    		ajax_list_activeItem = item;
     
    		if(fromKeyBoard){
    			if(ajax_list_activeItem.offsetTop>ajax_optionDiv.offsetHeight){
    				ajax_optionDiv.scrollTop = ajax_list_activeItem.offsetTop - ajax_optionDiv.offsetHeight + ajax_list_activeItem.offsetHeight + 2 ;
    			}
    			if(ajax_list_activeItem.offsetTop<ajax_optionDiv.scrollTop)
    			{
    				ajax_optionDiv.scrollTop = 0;	
    			}
    		}
    	}
     
    	function ajax_option_list_buildList(letters,paramToExternalFile)
    	{
     
    		ajax_optionDiv.innerHTML = '';
    		ajax_list_activeItem = false;
    		if(ajax_list_cachedLists[paramToExternalFile][letters.toLowerCase()].length<=1){
    			ajax_options_hide();
    			return;			
    		}
     
     
     
    		ajax_list_optionDivFirstItem = false;
    		var optionsAdded = false;
    		for(var no=0;no<ajax_list_cachedLists[paramToExternalFile][letters.toLowerCase()].length;no++){
    			if(ajax_list_cachedLists[paramToExternalFile][letters.toLowerCase()][no].length==0)continue;
    			optionsAdded = true;
    			var div = document.createElement('DIV');
    			var items = ajax_list_cachedLists[paramToExternalFile][letters.toLowerCase()][no].split(/###/gi);
     
    			if(ajax_list_cachedLists[paramToExternalFile][letters.toLowerCase()].length==1 && ajax_list_activeInput.value == items[0]){
    				ajax_options_hide();
    				return;						
    			}
     
     
    			div.innerHTML = items[items.length-1];
    			div.id = items[0];
    			div.className='optionDiv';
    			div.onmouseover = function(){ ajax_options_rollOverActiveItem(this,false) }
    			div.onclick = ajax_option_setValue;
    			if(!ajax_list_optionDivFirstItem)ajax_list_optionDivFirstItem = div;
    			ajax_optionDiv.appendChild(div);
    		}	
    		if(optionsAdded){
    			ajax_optionDiv.style.display='block';
    			if(ajax_optionDiv_iframe)ajax_optionDiv_iframe.style.display='';
    			ajax_options_rollOverActiveItem(ajax_list_optionDivFirstItem,true);
    		}
     
    	}
     
    	function ajax_option_list_showContent(ajaxIndex,inputObj,paramToExternalFile,whichIndex)
    	{		
     
    		if(whichIndex!=currentListIndex)return;
    		var letters = inputObj.value;
    		var content = ajax_list_objects[ajaxIndex].response;
    		var elements = content.split('|');
    		ajax_list_cachedLists[paramToExternalFile][letters.toLowerCase()] = elements;
    		ajax_option_list_buildList(letters,paramToExternalFile);
    	}
     
    	function ajax_option_resize(inputObj)
    	{
    		ajax_optionDiv.style.top = (ajax_getTopPos(inputObj) + inputObj.offsetHeight + ajaxBox_offsetY) + 'px';
    		ajax_optionDiv.style.left = (ajax_getLeftPos(inputObj) + ajaxBox_offsetX) + 'px';
    		if(ajax_optionDiv_iframe){
    			ajax_optionDiv_iframe.style.left = ajax_optionDiv.style.left;
    			ajax_optionDiv_iframe.style.top = ajax_optionDiv.style.top;			
    		}		
     
    	}
     
    	function ajax_showOptions(inputObj,paramToExternalFile,e,donnees)
    	{
     
    		if(e.keyCode==13 || e.keyCode==9)return;
    		if(ajax_list_currentLetters[inputObj.name]==inputObj.value)return;
    		if(!ajax_list_cachedLists[paramToExternalFile])ajax_list_cachedLists[paramToExternalFile] = new Array();
    		ajax_list_currentLetters[inputObj.name] = inputObj.value;
    		if(!ajax_optionDiv){
    			ajax_optionDiv = document.createElement('DIV');
    			ajax_optionDiv.id = 'ajax_listOfOptions';	
    			document.body.appendChild(ajax_optionDiv);
     
    			if(ajax_list_MSIE){
    				ajax_optionDiv_iframe = document.createElement('IFRAME');
    				ajax_optionDiv_iframe.border='0';
    				ajax_optionDiv_iframe.style.width = ajax_optionDiv.clientWidth + 'px';
    				ajax_optionDiv_iframe.style.height = ajax_optionDiv.clientHeight + 'px';
    				ajax_optionDiv_iframe.id = 'ajax_listOfOptions_iframe';
     
    				document.body.appendChild(ajax_optionDiv_iframe);
    			}
     
    			var allInputs = document.getElementsByTagName('INPUT');
    			for(var no=0;no<allInputs.length;no++){
    				if(!allInputs[no].onkeyup)allInputs[no].onfocus = ajax_options_hide;
    			}			
    			var allSelects = document.getElementsByTagName('SELECT');
    			for(var no=0;no<allSelects.length;no++){
    				allSelects[no].onfocus = ajax_options_hide;
    			}
     
    			var oldonkeydown=document.body.onkeydown;
    			if(typeof oldonkeydown!='function'){
    				document.body.onkeydown=ajax_option_keyNavigation;
    			}else{
    				document.body.onkeydown=function(){
    					oldonkeydown();
    				ajax_option_keyNavigation() ;}
    			}
    			var oldonresize=document.body.onresize;
    			if(typeof oldonresize!='function'){
    				document.body.onresize=function() {ajax_option_resize(inputObj); };
    			}else{
    				document.body.onresize=function(){oldonresize();
    				ajax_option_resize(inputObj) ;}
    			}
     
    		}
     
    		if(inputObj.value.length<minimumLettersBeforeLookup){
    			ajax_options_hide();
    			return;
    		}
     
     
    		ajax_optionDiv.style.top = (ajax_getTopPos(inputObj) + inputObj.offsetHeight + ajaxBox_offsetY) + 'px';
    		ajax_optionDiv.style.left = (ajax_getLeftPos(inputObj) + ajaxBox_offsetX) + 'px';
    		if(ajax_optionDiv_iframe){
    			ajax_optionDiv_iframe.style.left = ajax_optionDiv.style.left;
    			ajax_optionDiv_iframe.style.top = ajax_optionDiv.style.top;			
    		}
     
    		ajax_list_activeInput = inputObj;
    		ajax_optionDiv.onselectstart =  ajax_list_cancelEvent;
    		currentListIndex++;
    		/* if(ajax_list_cachedLists[paramToExternalFile][inputObj.value.toLowerCase()]){
    			ajax_option_list_buildList(inputObj.value,paramToExternalFile,currentListIndex);			
    		}else{ */
    			var tmpIndex=currentListIndex/1;
    			ajax_optionDiv.innerHTML = '';
    			var ajaxIndex = ajax_list_objects.length;
    			ajax_list_objects[ajaxIndex] = new sack();
    			var url = ajax_list_externalFile + '?' + paramToExternalFile + '=1&letters=' + inputObj.value.replace(" ","+") + '&donnees=' + donnees;
     
    			ajax_list_objects[ajaxIndex].requestFile = url;	// Specifying which file to get
    			ajax_list_objects[ajaxIndex].onCompletion = function(){ ajax_option_list_showContent(ajaxIndex,inputObj,paramToExternalFile,tmpIndex); };	// Specify function that will be executed after file has been found
    			ajax_list_objects[ajaxIndex].runAJAX();		// Execute AJAX function
     
    			//}
     
    			 ajaxBox_offsetX = 0;
    			 ajaxBox_offsetY = 0;
    			 ajax_list_externalFile = '../ajax/ajax-list.php';	// Path to external file
    			 minimumLettersBeforeLookup = 1;	// Number of letters entered before a lookup is performed.
     
    	}
     
    	function ajax_option_keyNavigation(e)
    	{
    		if(document.all)e = event;
     
    		if(!ajax_optionDiv)return;
    		if(ajax_optionDiv.style.display=='none')return;
     
    		if(e.keyCode==38){	// Up arrow
    			if(!ajax_list_activeItem)return;
    			if(ajax_list_activeItem && !ajax_list_activeItem.previousSibling)return;
    			ajax_options_rollOverActiveItem(ajax_list_activeItem.previousSibling,true);
    		}
     
    		if(e.keyCode==40){	// Down arrow
    			if(!ajax_list_activeItem){
    				ajax_options_rollOverActiveItem(ajax_list_optionDivFirstItem,true);
    			}else{
    				if(!ajax_list_activeItem.nextSibling)return;
    				ajax_options_rollOverActiveItem(ajax_list_activeItem.nextSibling,true);
    			}
    		}
     
    		if(e.keyCode==13 || e.keyCode==9){	// Enter key or tab key
    			if(ajax_list_activeItem && ajax_list_activeItem.className=='optionDivSelected')ajax_option_setValue(false,ajax_list_activeItem);
    			if(e.keyCode==13)return false; else return true;
    		}
    		if(e.keyCode==27){	// Escape key
    			ajax_options_hide();			
    		}
    	}
     
     
    	document.documentElement.onclick = autoHideList;
     
    	function autoHideList(e)
    	{
    		if(document.all)e = event;
     
    		if (e.target) source = e.target;
    			else if (e.srcElement) source = e.srcElement;
    			if (source.nodeType == 3) // defeat Safari bug
    				source = source.parentNode;		
    		if(source.tagName.toLowerCase()!='input' && source.tagName.toLowerCase()!='textarea')ajax_options_hide();
     
    	}
    Et le code php pour getProduit
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    if(isset($_GET['getProduit']) && isset($_GET['letters'])){
    	$letters = $_GET['letters'];
    	$letters = preg_replace("/[^a-z0-9 ]/si","",$letters);
     
    	$res3 = mysql_query("SELECT * " .
    			"FROM produits " .
    			"WHERE (designation like '$letters%') ORDER BY designation") or die(mysql_error());
    	while($inf3 = mysql_fetch_array($res3)){
    		echo $inf3["id_prod"]."###".htmlentities($inf3["designation"])."|";
    	}
    }

  4. #4
    Membre à l'essai
    Homme Profil pro
    Inscrit en
    Janvier 2013
    Messages
    4
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Corrèze (Limousin)

    Informations forums :
    Inscription : Janvier 2013
    Messages : 4
    Par défaut
    L'auto-complétion fonctionne nickel quand je l'utilise dans mon code php.
    Le problème viens de ma fonction AddRow()

  5. #5
    Membre à l'essai
    Homme Profil pro
    Inscrit en
    Janvier 2013
    Messages
    4
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Corrèze (Limousin)

    Informations forums :
    Inscription : Janvier 2013
    Messages : 4
    Par défaut
    J'ai résolu le problème
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    var get = "ajax_showOptions(this, 'getProduit', event)";
    newCell.innerHTML = '<input type="text" id="produit" name="produit" onkeyup="'+get+'" size="45" autocomplete="off"><input type="hidden" id="produit_hidden'+i+'" name="produit>">';
    En faisant comme ça

  6. #6
    Modérateur

    Avatar de NoSmoking
    Homme Profil pro
    Inscrit en
    Janvier 2011
    Messages
    17 207
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Isère (Rhône Alpes)

    Informations forums :
    Inscription : Janvier 2011
    Messages : 17 207
    Par défaut
    On en revient donc bien à ma question et maintenant on à la réponse, getProduit est une String, donc il manquait les quotes dans cette ligne autour de getProduit
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    newCell.innerHTML = '<input type="text" id="produit" name="produit" onkeyup="ajax_showOptions(this, getProduit, event);...
    les quotes sont à échapper dans ce cas
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    newCell.innerHTML = '<input type="text" id="produit" name="produit" onkeyup="ajax_showOptions(this, \'getProduit\', event);...

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [AJAX] Ajax, innerHTML et fonction javascript - solution ?
    Par gouroulubrik dans le forum Général JavaScript
    Réponses: 8
    Dernier message: 25/03/2008, 21h35
  2. [AJAX] Soap Ajax classe javascript
    Par sekaijin dans le forum Général JavaScript
    Réponses: 1
    Dernier message: 13/06/2006, 08h50
  3. [AJAX] Appel de fonction javascript
    Par slaborde dans le forum Général JavaScript
    Réponses: 2
    Dernier message: 29/05/2006, 19h43
  4. [AJAX] Réponse XML - Functions Javascript
    Par ..:: Atchoum ::.. dans le forum Général JavaScript
    Réponses: 24
    Dernier message: 24/01/2006, 03h02

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo