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

JavaScript Discussion :

Popup et récupération de variable php


Sujet :

JavaScript

  1. #1
    Nouveau Candidat au Club
    Homme Profil pro
    Responsable de service informatique
    Inscrit en
    Septembre 2011
    Messages
    2
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Madagascar

    Informations professionnelles :
    Activité : Responsable de service informatique
    Secteur : Enseignement

    Informations forums :
    Inscription : Septembre 2011
    Messages : 2
    Points : 1
    Points
    1
    Par défaut Popup et récupération de variable php
    Bonjour,
    Depuis qlq temps j'ai fait des codes pour afficher le résultat de recherche sous forme tableau via un popup et récupérer dans une page mère.
    code sql:
    Code sql : 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
    - Base de données: `cpu`
     
    -- Structure de la table `produit`
     
    CREATE TABLE IF NOT EXISTS `produit` (
      `Identifiant` int(10) unsigned NOT NULL AUTO_INCREMENT,
      `Nom` varchar(100) DEFAULT NULL,
      `Prix` float DEFAULT NULL,
      `Vignette` varchar(100) DEFAULT NULL,
      `Categorie` int(11) DEFAULT NULL,
      PRIMARY KEY (`Identifiant`)
    ) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;
     
    --
    -- Contenu de la table `produit`
    --
     
    INSERT INTO `produit` (`Identifiant`, `Nom`, `Prix`, `Vignette`, `Categorie`) VALUES
    (1, 'Intel Pentium 4 2GHz', 245, 'vignette/p4.gif', 2),
    (2, 'Intel Celeron 4 2GHz', 145, 'vignette/celeron.gif', 2),
    (3, 'Barette DDR 512 Mo PC2100', 56, 'vignette/ddr.gif', 3),
    (4, 'Intel(R) Core(TM) i3-4005U 1.7GHz', 310, 'vignette/core_i3.gif', 2);


    1. page mère

    Code html : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    <script type="text/javascript">
            function open_infos()
                            {
                            window.open('rch_cpu.php','nom_de_ma_popup','menubar=no, scrollbars=no, top=100, left=100, width=800, height=400');
                            }
            </script>
        	<p>Nom CPU: <input type="text" size="30" name="recherche" id="ch" value="" disabled />
    		<a href="#null" onclick="javascript:open_infos();"><img src="rech.gif" width="25" title="Rechercher CPU" class="pointer" /></a>
    .

    1. page rch_cpu.php

    Code php : 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
    <script type="text/javascript">
    	function recherche() 
    		{
    			window.opener.document.getElementById('ch').value = document.getElementById('rch').value; 			
    			window.close();
    		}
    </script>
     
    <form name="frm_add_eleve" id="frm_add_eleve" method="GET" action="rch_cpu.php">
     
    	<table>         
    		<tr>
                <td>
                    Nom CPU
                </td>
                <td>
                    <input type="text" size="50" name="recherche" />
                </td>
            </tr>
    		<tr>
    			<td>&nbsp;&nbsp;</td>
    			<td><input type="submit" value="Rechercher" /></td>
    		</tr>
    	</table>
    </form>
     
    <?php
    mysql_connect('localhost','root','');
    mysql_select_db('CPU');
    //On determine l'expression a rechercher
    if(isset($_GET['recherche']))
    {
    $mot=@$_GET['recherche'];
    echo $mot;
     
    $req = 'SELECT * FROM produit WHERE  Nom LIKE "%'.$mot.'%" ' ;
     
    $req .= ' order by Identifiant asc';
    $requete = mysql_query($req);
     
    $nb=mysql_num_rows($requete);
    if($nb>0)
    {
    //Le formulaire de recherche
    ?>
    <h2>R&eacute;sultats</h2>
    <table width="500" border="1" cellspacing="0" cellpadding="5">
            <tr>
                    <th>Choisir</th>
                    <th>Nom</th>
    				<th>Prix</th>
    				<th>Vignette</th>
                    <th>atégorie</th>
            </tr>
    <?php
     
    //On affiche les resultats
    while($dnn = mysql_fetch_array($requete))
    {	
    ?>
            <tr>			
    			<td><a href="javascript:recherche();" ><input type="hidden" value="<?php echo $dnn['Nom']; ?>" name="recherche" id="rch" />Choisir</a></td>
    			<td><?php echo $dnn['Nom']; ?></td>
    			<td><?php echo $dnn['Prix']; ?></td>                 
    			<td><?php echo $dnn['Vignette']; ?></td>
    			<td><?php echo $dnn['Catégorie']?>
    				</td>
            </tr>
    <?php
    }
    }
    else
    	{
    		echo "<script language = \"javascript\"> alert(\"Aucin CPU !\");	document.location.href='rch_cpu.php'; </script>";
    	}
    }
    ?>
    </table>
    .

    Si j'ai choisi intel comme mot clé on a trois résultats et si j'ai clique sur choisir c'est toujour la premier ligne du tableau qu'on a récupéré.
    Pourriez-vous m'aider.

  2. #2
    Membre confirmé Avatar de 01001111
    Homme Profil pro
    Développeur Web
    Inscrit en
    Janvier 2009
    Messages
    319
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Haute Loire (Auvergne)

    Informations professionnelles :
    Activité : Développeur Web
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Janvier 2009
    Messages : 319
    Points : 509
    Points
    509
    Par défaut
    c'est normal tu cibles un id qui est forcément unique, même si généré 3 fois, le navigateur du poste client va choisir le premier à tous les coups.
    essaye:
    Code php : 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
    <script type="text/javascript">
    	function recherche(className) 
    		{
    			window.opener.document.getElementById('ch').value = document.querySelector(className).value; 			
    			window.close();
    		}
    </script>
     
    <form name="frm_add_eleve" id="frm_add_eleve" method="GET" action="rch_cpu.php">
     
    	<table>         
    		<tr>
                <td>
                    Nom CPU
                </td>
                <td>
                    <input type="text" size="50" name="recherche" />
                </td>
            </tr>
    		<tr>
    			<td>&nbsp;&nbsp;</td>
    			<td><input type="submit" value="Rechercher" /></td>
    		</tr>
    	</table>
    </form>
     
    <?php
    mysql_connect('localhost','root','');
    mysql_select_db('CPU');
    //On determine l'expression a rechercher
    if(isset($_GET['recherche']))
    {
    $mot=@$_GET['recherche'];
    echo $mot;
     
    $req = 'SELECT * FROM produit WHERE  Nom LIKE "%'.$mot.'%" ' ;
     
    $req .= ' order by Identifiant asc';
    $requete = mysql_query($req);
     
    $nb=mysql_num_rows($requete);
    if($nb>0)
    {
    //Le formulaire de recherche
    ?>
    <h2>R&eacute;sultats</h2>
    <table width="500" border="1" cellspacing="0" cellpadding="5">
            <tr>
                    <th>Choisir</th>
                    <th>Nom</th>
    				<th>Prix</th>
    				<th>Vignette</th>
                    <th>atégorie</th>
            </tr>
    <?php
     
    //On affiche les resultats
    while($dnn = mysql_fetch_array($requete))
    {	
    ?>
            <tr>			
    			<td><a href="javascript:recherche('_<?php echo $dnn['Identifiant']; ?>');" ><input type="hidden" value="<?php echo $dnn['Nom']; ?>" name="recherche" class="_<?php echo $dnn['Identifiant']; ?>" />Choisir</a></td>
    			<td><?php echo $dnn['Nom']; ?></td>
    			<td><?php echo $dnn['Prix']; ?></td>                 
    			<td><?php echo $dnn['Vignette']; ?></td>
    			<td><?php echo $dnn['Catégorie']?>
    				</td>
            </tr>
    <?php
    }
    }
    else
    	{
    		echo "<script language = \"javascript\"> alert(\"Aucin CPU !\");	document.location.href='rch_cpu.php'; </script>";
    	}
    }
    ?>
    </table>

    D'autre part il vaut mieux pour raison de sécurité utiliser mysql_real_escape_string pour echapper les variables de recherche en get que tu envoies. Au mieux utiliser PDO qui est plus moderne que les fonctions préfixées mysql_ en php.
    0x4F

  3. #3
    Nouveau Candidat au Club
    Homme Profil pro
    Responsable de service informatique
    Inscrit en
    Septembre 2011
    Messages
    2
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Madagascar

    Informations professionnelles :
    Activité : Responsable de service informatique
    Secteur : Enseignement

    Informations forums :
    Inscription : Septembre 2011
    Messages : 2
    Points : 1
    Points
    1
    Par défaut
    Merci de votre réponse, mais qu'on on clique sur choisir, aucune réponse, c-a-d que la fenêtre popup ne ferme pas et on ne récupéré rien. j'ai déjà essayé le même méthode mais avec
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    <a href="javascript:recherche('<?php echo $dnn['Nom']; ?>');" ><input type="hidden" value="<?php echo $dnn['Nom']; ?>" name="recherche" ; ?>" />
    même résultat que le votre. est qu'il aura une autre solution, merci d'avance.

  4. #4
    Membre confirmé Avatar de 01001111
    Homme Profil pro
    Développeur Web
    Inscrit en
    Janvier 2009
    Messages
    319
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Haute Loire (Auvergne)

    Informations professionnelles :
    Activité : Développeur Web
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Janvier 2009
    Messages : 319
    Points : 509
    Points
    509
    Par défaut
    oui pardon, j'étais fatigué je crois quand j'ai écris ceci
    ça devrait marcher comme ça:
    Code php : 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
     
    <script type="text/javascript">
    	function recherche(id) 
    		{
    			window.opener.document.getElementById('ch').value = document.querySelector('#'+id).value; 			
    			window.close();
    		}
    </script>
     
    <form name="frm_add_eleve" id="frm_add_eleve" method="GET" action="rch_cpu.php">
     
    	<table>         
    		<tr>
                <td>
                    Nom CPU
                </td>
                <td>
                    <input type="text" size="50" name="recherche" />
                </td>
            </tr>
    		<tr>
    			<td>&nbsp;&nbsp;</td>
    			<td><input type="submit" value="Rechercher" /></td>
    		</tr>
    	</table>
    </form>
     
    <?php
    mysql_connect('localhost','root','');
    mysql_select_db('CPU');
    //On determine l'expression a rechercher
    if(isset($_GET['recherche']))
    {
    $mot=@$_GET['recherche'];
    echo $mot;
     
    $req = 'SELECT * FROM produit WHERE  Nom LIKE "%'.$mot.'%" ' ;
     
    $req .= ' order by Identifiant asc';
    $requete = mysql_query($req);
     
    $nb=mysql_num_rows($requete);
    if($nb>0)
    {
    //Le formulaire de recherche
    ?>
    <h2>R&eacute;sultats</h2>
    <table width="500" border="1" cellspacing="0" cellpadding="5">
            <tr>
                    <th>Choisir</th>
                    <th>Nom</th>
    				<th>Prix</th>
    				<th>Vignette</th>
                    <th>atégorie</th>
            </tr>
    <?php
     
    //On affiche les resultats
    while($dnn = mysql_fetch_array($requete))
    {	
    ?>
            <tr>			
    			<td><a href="javascript:recherche('_<?php echo $dnn['Identifiant']; ?>');" ><input type="hidden" value="<?php echo $dnn['Nom']; ?>" name="recherche" id="_<?php echo $dnn['Identifiant']; ?>" />Choisir</a></td>
    			<td><?php echo $dnn['Nom']; ?></td>
    			<td><?php echo $dnn['Prix']; ?></td>                 
    			<td><?php echo $dnn['Vignette']; ?></td>
    			<td><?php echo $dnn['Catégorie']?>
    				</td>
            </tr>
    <?php
    }
    }
    else
    	{
    		echo "<script language = \"javascript\"> alert(\"Aucin CPU !\");	document.location.href='rch_cpu.php'; </script>";
    	}
    }
    ?>
    </table>
    0x4F

Discussions similaires

  1. récupération de variables php dans un formulaire
    Par xanthos dans le forum Langage
    Réponses: 3
    Dernier message: 22/07/2011, 09h52
  2. Récupération de variables PHP en AS3
    Par SubZero2 dans le forum ActionScript 3
    Réponses: 5
    Dernier message: 26/09/2010, 23h48
  3. [PHP-JS] Récupération de variable php en javascript
    Par akara dans le forum Général JavaScript
    Réponses: 6
    Dernier message: 30/07/2007, 18h08
  4. [AJAX] Récupération de variable php
    Par akara dans le forum Général JavaScript
    Réponses: 6
    Dernier message: 30/07/2007, 15h52
  5. ouverture popup avec un paramètre => variable php
    Par gmonta31 dans le forum Général JavaScript
    Réponses: 2
    Dernier message: 30/03/2006, 08h59

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