Page 1 sur 1

Recupération variable json dans php

Posté : 05 avr. 2015, 17:09
par debo225
Bonjour,
J'essai depuis 2 jours de récupérer la valeur d'une variable javascript dans mon iframe.
Pouvez vous m'aider svp?
Voici mon code:
<script type="text/javascript">


$(function() {
	 var flag ;
	dataType: 'json';
            $("#state").autocomplete({
				source: "states1.php",
				type: "GET",
				data: "param=param",
        		async: false,
                minLength: 2,
 success: function(json)
		 {flag = json.state;
		 $('#Code1').html(json.Code1);
     	
     	},
                select: function(event, ui) {
					$('#state').val(ui.item.Designation);
                    $('#state_id').val(ui.item.CodeProduit);
					$('#sid').val(ui.item.CodeProduit);
                    $('#abbrev').val(ui.item.Code1);
					$('#abbrev2').val(ui.item.Code2);
					$("#state_abbrev").focus();
                }
            });
        
            $("#abbrev2").autocomplete({
                source: "states_abbrev1.php",
                minLength: 2,
		
		 select: function(event, ui) {
					$('#state').val(ui.item.Designation);
                    $('#state_id').val(ui.item.CodeProduit);
					$('#sid').val(ui.item.CodeProduit);
                    $('#abbrev').val(ui.item.Code1;
					$('#abbrev2').val(ui.item.value);
					$("#state_abbrev").focus();
                }
            });
			
     $("#abbrev").autocomplete({
                source: "states_abbrev2.php",
                minLength: 2,
		
		 select: function(event, ui) {
					$('#state').val(ui.item.Designation);
                    $('#state_id').val(ui.item.CodeProduit);
					$('#sid').val(ui.item.CodeProduit);
                    $('#abbrev').val(ui.item.value);
					$('#abbrev2').val(ui.item.Code2);
					$("#state_abbrev").focus();
                }
            });
	 
	 
        });
</script>
echo"<iframe src=dispo.php?Code=json_encode($json['Code1'])></iframe>
Mon code php
<?php  
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$dbname = 'db';
 
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
mysql_select_db($dbname);
 
$term=$_GET["term"];  
$query=mysql_query("SELECT * FROM produit where Designation like '%".$term."%' order by Designation "); 
if ( ! $query )
	die ('mysql_query error SELECT produit' . mysql_error());
	
$json=array(); 
$return_json=array();
    
while($row=mysql_fetch_array($query))
{         
  
	$row = array_map('utf8_encode', $row);

	$critere = $row['Designation'].' '.$row['Code1'].' '.$row['Code2'];
	$json['value'] = $row['Designation'];
	$json['Code1'] = $row['Code1'];
	$json['Code2'] = $row['Code2'];
	$json['sid'] = $row['CodeProduit'];
	$json['label'] = $critere;
 
        array_push($return_json,$json);
					
}  


echo json_encode($return_json); 
flush();
exit;
?>

Re: Recupération variable json dans php

Posté : 05 avr. 2015, 20:12
par @rthur
Bonjour,

Regarde ton code HTML généré pour l'iframe (clic-droit, afficher la source), il manque des guillemets pour encadrer ton src et par ailleurs dans ton code PHP tu ne récupère jamais la variable Code que tu passes en paramètre

Re: Recupération variable json dans php

Posté : 05 avr. 2015, 21:07
par debo225
Bonjour @rthur,
Merci de regarder mon code.
En fait, j'essai de faire passer dans ma variable code, la valeur json code1.
Quand je test, il me renvoie null.

Re: Recupération variable json dans php

Posté : 06 avr. 2015, 01:37
par @rthur
Tu ne la récupères pas dans le code PHP que tu nous as donné... tu attends une variable term visiblement

Re: Recupération variable json dans php

Posté : 06 avr. 2015, 02:27
par debo225
la variable term me permet de faire une recherche autocompletion et les resultats sont affichés dans les champs code1 et code2.
Je voudrais juste recuperer le code1 et l'envoyer en paramètres à mon iframe.

Voici le code de mon formulaire
<form action=#  method="POST" name="form1">
                      
                        <table width="100%" border="0" cellpadding="4" cellspacing="4">
                  
                          <tr>

                            <td width="35%" rowspan="6" valign="bottom">
                              </td>
                            </tr>
                          <tr>
                            <td width="23%"><span class="ok">Designation</span><span class="ui-widget"><span class="jul">:</span> </span></td>
                            <td>
                              <input  name="state" type="text" id="state" size="50" />
                              </td>
                            </tr>
                          <tr>
                            <td class="jul">Code1</td>
                            <td width="42%">
                              <input type="text" id="abbrev" name="abbrev" size="25"/>
                              </td>
                            </tr>
                          <tr>
                            <td class="jul">Code2</td>
                            <td>
                              <input type="text" id="abbrev2" name="abbrev2" size="25"/>
                              </td>
                            </tr>
                          <tr>
                                  <td><span class="ui-widget">
                                <input name="state_abbrev" type="text" id="state_abbrev" size="5"   />
                                </span></td>
                            </tr>
                          <tr>
                            <td colspan="2" class="jul"><input type="submit" name="submit" value="Enregistrer" /></td>
                            </tr>
                          </table>

                      </form>