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

Langage PHP Discussion :

[PHP-JS] Le javascript et le PHP - Compatible ? [Débutant(e)]


Sujet :

Langage PHP

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre éclairé Avatar de bilou95
    Inscrit en
    Septembre 2006
    Messages
    433
    Détails du profil
    Informations forums :
    Inscription : Septembre 2006
    Messages : 433
    Par défaut [PHP-JS] Le javascript et le PHP - Compatible ?
    Une question ?

    Le javascript s'applique t'il a plusieurs reprises dans du code PHP ...

    Je m'explique !

    Je viens de faire ceci ... Un champ formulaire de mise a jour modifiable sur une seule page PHP ... qui va chercher 2 fonctions javascript.

    Voici l'exemple.

    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
    <style>
    .largeur{width:20%}
    .largeur2{width:80%}
    </style> 
    <script type="text/javascript">
    function fct1()
    {
        document.forms.maj_os.submit(); 
    }
     
    function refresh()
    {
        location.reload();
    }
    </script>
    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
    <?php
    require("cnx/login_sql.php");
     
    //Connexion à la base de données
    $db = mysql_connect('localhost', 'root', '') or die ("Erreur de connexion");
     
    //Selection de la base
    mysql_select_db('alliance',$db);
     
    //Recupération de la variable id
    $id=$_GET['id'];
     
    $req_pr="SELECT * FROM spare WHERE id_spare='$id'";
     
    $sql_pr=mysql_query($req_pr) or die ('Erreur SQL !<br>'.$req.'<br>'.mysql_error());
    echo '<IMG src="image/fiche_sp.png" align="left" border=0>';
    while($data=mysql_fetch_assoc($sql_pr))
    	{
     
    	echo '<DIV align="left"><strong><u><font face="verdana" size="3">'.$data['nom_spare'].'</font></u></strong></br></br>';
     
     	if($data['modele'] == "ACER")
    	{
    	echo '<table style="border-color:#000000;border-width:1;border-style:dotted">';
    	echo '<tr>';
    	echo '<td align="center">';
    	echo '<IMG src="image/logo-acer.png" border=0>';
    	echo '</td>';
    	echo '<td>';
    	echo '<a href="http://support.acer-euro.com/">Support [www]</a>';
    	echo '</td>';
    	echo '</tr>';
    	echo '</table>';
    	}
    	elseif($data['modele'] == "DELL")
    	{
    	echo '<table style="border-color:#000000;border-width:1;border-style:dotted">';
    	echo '<tr>';
    	echo '<td align="center">';
    	echo '<IMG src="image/logo-dell.png" border=0>';
    	echo '</td>';
    	echo '<td>';
    	echo '<a href="http://support.euro.dell.com/">Support [www]</a>';
    	echo '</td>';
    	echo '</tr>';
    	echo '</table>';
    	}
    	elseif($data['modele'] == "NEC")
    	{
    	echo '<table style="border-color:#000000;border-width:1;border-style:dotted">';
    	echo '<tr>';
    	echo '<td align="center">';
    	echo '<IMG src="image/logo-nec.png" border=0>';
    	echo '</td>';
    	echo '<td>';
    	echo '<a href=" http://www.nec-display-solutions.fr/">Support [www]</a>';
    	echo '</td>';
    	echo '</tr>';
    	echo '</table>';
     
    	}
     
    	echo'</br>';
    	echo'</br>';
    	echo '<table border=1 align=center width="80%">';
    	echo '<tr>';
    	echo '<td style="background-image:url(image/bg6box.gif)">Information Matériel</td>';
    	echo '</tr>';
    	echo '</table>';
    	echo '&nbsp';
    	echo '<table style="border-color:#000000;border-width:1;border-style:dashed" width="80%" align=center>';
    	echo '<tr>';
    	echo '<td class="largeur" style="border-color:#000000;border-width:1;border-style:dotted"><strong><font size="1">Modèle</td>';
    	echo '<td style="border-color:#000000;border-width:1;border-style:dotted">eeee</td>';
    	echo'</tr>';
    	echo '<tr>';
    	echo '<td class="largeur" style="border-color:#000000;border-width:1;border-style:dotted"><strong><font size="1">N° série Machine</td>';
    	echo '<td style="border-color:#000000;border-width:1;border-style:dotted">eeee</td>';
    	echo'</tr>';
    	echo '<tr>';
    	echo '<td style="border-color:#000000;border-width:1;border-style:dotted"><strong><font size="1">N° Inventaire [A.H]</td>';
    	echo '<td style="border-color:#000000;border-width:1;border-style:dotted">eeee</td>';
    	echo'</tr>';
     
    	if($data['os'] == '')
    	{
    	echo '<tr>';
    	echo '<td style="border-color:#000000;border-width:1;border-style:dotted"><strong><font size="1">Système Exploitation</td>';
    	echo '<td style="border-color:#000000;border-width:1;border-style:dotted">';
     
    	if (isset($_POST["os"])) {
    	$sql_os="UPDATE spare SET os='".$_POST["os"]."' WHERE id_spare='$id'";
    	$exe_sqlos = mysql_query($sql_os) or die('Erreur SQL !');
    							}
     
    	echo '<form name="maj_os" method="POST" Action="index.php?numlien=fich_spare&id='.$id.'">';
    	echo '<input type="text" name="os" value="'.$data['os'].'">&nbsp&nbsp <IMG src="image/stylo.png" style="border:none;" onclick="fct1();refresh();" />';
    	echo'</form></td>';
    	}
    	else
    	{
    	echo '<tr>';
    	echo '<td style="border-color:#000000;border-width:1;border-style:dotted"><strong><font size="1">Système Exploitation</td>';
    	echo '<td style="border-color:#000000;border-width:1;border-style:dotted">'.$data['os'].'</td>';
    	echo'</tr>';
    	}
     
     
    	echo '<tr>';
    	echo '<td style="border-color:#000000;border-width:1;border-style:dotted"><strong><font size="1">Mémoire vive [R.A.M]</td>';
    	echo '<td style="border-color:#000000;border-width:1;border-style:dotted">eeee</td>';
    	echo'</tr>';
    	echo '<tr>';
    	echo '<td style="border-color:#000000;border-width:1;border-style:dotted"><strong><font size="1">Disque Dur [H.D]</td>';
    	echo '<td style="border-color:#000000;border-width:1;border-style:dotted">eeee</td>';
    	echo'</tr>';
    	echo '<tr>';
    	echo '<td style="border-color:#000000;border-width:1;border-style:dotted"><strong><font size="1">Vidéo [Carte Graphique]</td>';
    	echo '<td style="border-color:#000000;border-width:1;border-style:dotted">eeee</td>';
    	echo'</tr>';
    	echo '<tr>';
    	echo '<td style="border-color:#000000;border-width:1;border-style:dotted"><strong><font size="1">Sans Fil [Wifi]</td>';
    	echo '<td style="border-color:#000000;border-width:1;border-style:dotted">eeee</td>';
    	echo'</tr>';
    	echo '<tr>';
    	echo '<td style="border-color:#000000;border-width:1;border-style:dotted"><strong><font size="1">Carte réseau [LAN]</td>';
    	echo '<td style="border-color:#000000;border-width:1;border-style:dotted">eeee</td>';
    	echo'</tr>';
    	echo'</table>';
     
    	}
     
     
    mysql_close();
    ?>

  2. #2
    Membre éprouvé Avatar de gd_dev
    Homme Profil pro
    Développeur Web
    Inscrit en
    Décembre 2003
    Messages
    79
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 53
    Localisation : Suisse

    Informations professionnelles :
    Activité : Développeur Web
    Secteur : Service public

    Informations forums :
    Inscription : Décembre 2003
    Messages : 79
    Par défaut
    Bonjour,

    Selon le code présenté le formulaire s'affiche plusieur foix, c'est juste?

    Une petite remarque au passage, ce code est sensible au injection sql
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
     
    //Recupération de la variable id
    $id=$_GET['id'];
    Je vous conseil
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
     
    //Recupération de la variable id
    $id = intval($_GET['id']);

  3. #3
    Membre expérimenté Avatar de Azazel.fr
    Profil pro
    Inscrit en
    Mars 2007
    Messages
    170
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2007
    Messages : 170
    Par défaut
    oui, javascript et php cohabite sans probleme.

  4. #4
    Membre émérite
    Avatar de viviboss
    Profil pro
    Inscrit en
    Août 2006
    Messages
    943
    Détails du profil
    Informations personnelles :
    Âge : 43
    Localisation : France, Gard (Languedoc Roussillon)

    Informations forums :
    Inscription : Août 2006
    Messages : 943
    Par défaut
    Tu peux mettre du Javascript dans du PHP, mais pas l'inverse.

    Le PHP est un langage seveur, alors que le javascript est basé coté client (navigateur).

    Les deux langages sont même très complémentaire....
    Veni Vidi Vici
    -------------------------
    Mes articles : developpez.com ou bien vbview.net
    -------------------------
    Et SURTOUT ne pas oublier la bible PHP : --> php_manual_fr.chm!!!
    Et aussi : --> pear_manual_fr.chm!!!

    Ou encore : --> Les tutoriaux & cours PHP de Développez.com
    -------------------------

  5. #5
    Membre éclairé Avatar de bilou95
    Inscrit en
    Septembre 2006
    Messages
    433
    Détails du profil
    Informations forums :
    Inscription : Septembre 2006
    Messages : 433
    Par défaut
    Pourquoi lorsque je crée plusieurs fonctions pour chaque formulaire et que je les integre a un "onclick" ... cela ne fonctionne pas ...

    Grgrgrgrgrgrgr

  6. #6
    Fabouney
    Invité(e)
    Par défaut
    il est tout à fait possible d'envoyé a des variable javascript, le contenu d'une variable en php...:

    exemple:
    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
    <?php
    $varphp='foobar';
    ?>
    <html>
    <head>
    	<title>TEST</title>
    	<script language="javascript">
    	function display()
    	{
    		var result='<?= $varphp ?>';	
    		document.getElementById('test').innerHTML=result;
    	}
    	</script>
    </head>
    <body onload='display()';>
    <div id="test"></div>
    </body>
    </html>
    </body>
    à condition que le code javascript soit dans le script php.
    cela peut toujours servir .
    cordialement

  7. #7
    Membre éprouvé Avatar de gd_dev
    Homme Profil pro
    Développeur Web
    Inscrit en
    Décembre 2003
    Messages
    79
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 53
    Localisation : Suisse

    Informations professionnelles :
    Activité : Développeur Web
    Secteur : Service public

    Informations forums :
    Inscription : Décembre 2003
    Messages : 79
    Par défaut
    Il ne faut pas faire de refresh:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    ...  onclick="fct1();refresh();"
    Juste
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    ...  onclick="fct1();"
    De plus
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    <img src='' .. onClick='balaba'>
    n'est pas compatible avec tous les navigateur, il faut faire
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    <a href='#' onClick='balaba' ><img src='' .. border='0'></a>

  8. #8
    Membre éclairé Avatar de bilou95
    Inscrit en
    Septembre 2006
    Messages
    433
    Détails du profil
    Informations forums :
    Inscription : Septembre 2006
    Messages : 433
    Par défaut
    Grace a tes indications, j'ai reussi a faire fonctionner mon code correctement

    Merci encore !

    Voici la copie de mon code :

    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
    <style>
    .largeur{width:20%}
    .largeur2{width:80%}
    </style> 
    <script type="text/javascript">	
     
    function fonction1()
    {
        document.forms.maj_os.submit(); 
    }
     
    function fonction2()
    {
        document.forms.maj_ram.submit(); 
    }
     
     
     
     
    function refresh()
    {
        location.reload();
    }
    </script>
     
     
    </head>
    <body>
    <?php
    require("cnx/login_sql.php");
     
    //Connexion à la base de données
    $db = mysql_connect('localhost', 'root', '') or die ("Erreur de connexion");
     
    //Selection de la base
    mysql_select_db('alliance',$db);
     
    //Recupération de la variable id
    $id=$_GET['id'];
     
    $req_pr="SELECT * FROM spare WHERE id_spare='$id'";
     
    $sql_pr=mysql_query($req_pr) or die ('Erreur SQL !<br>'.$req.'<br>'.mysql_error());
    echo '<IMG src="image/fiche_sp.png" align="left" border=0>';
    while($data=mysql_fetch_assoc($sql_pr))
    	{
     
    	echo '<DIV align="left"><strong><u><font face="verdana" size="3">'.$data['nom_spare'].'</font></u></strong></br></br>';
     
     	if($data['modele'] == "ACER")
    	{
    	echo '<table style="border-color:#000000;border-width:1;border-style:dotted">';
    	echo '<tr>';
    	echo '<td align="center">';
    	echo '<IMG src="image/logo-acer.png" border=0>';
    	echo '</td>';
    	echo '<td>';
    	echo '<a href="http://support.acer-euro.com/">Support [www]</a>';
    	echo '</td>';
    	echo '</tr>';
    	echo '</table>';
    	}
    	elseif($data['modele'] == "DELL")
    	{
    	echo '<table style="border-color:#000000;border-width:1;border-style:dotted">';
    	echo '<tr>';
    	echo '<td align="center">';
    	echo '<IMG src="image/logo-dell.png" border=0>';
    	echo '</td>';
    	echo '<td>';
    	echo '<a href="http://support.euro.dell.com/">Support [www]</a>';
    	echo '</td>';
    	echo '</tr>';
    	echo '</table>';
    	}
    	elseif($data['modele'] == "NEC")
    	{
    	echo '<table style="border-color:#000000;border-width:1;border-style:dotted">';
    	echo '<tr>';
    	echo '<td align="center">';
    	echo '<IMG src="image/logo-nec.png" border=0>';
    	echo '</td>';
    	echo '<td>';
    	echo '<a href=" http://www.nec-display-solutions.fr/">Support [www]</a>';
    	echo '</td>';
    	echo '</tr>';
    	echo '</table>';
     
    	}
     
    	echo'</br>';
    	echo'</br>';
    	echo '<table border=1 align=center width="80%">';
    	echo '<tr>';
    	echo '<td style="background-image:url(image/bg6box.gif)">Information Matériel</td>';
    	echo '</tr>';
    	echo '</table>';
    	echo '&nbsp';
    	echo '<table style="border-color:#000000;border-width:1;border-style:dashed" width="80%" align=center>';
    	echo '<tr>';
    	echo '<td class="largeur" style="border-color:#000000;border-width:1;border-style:dotted"><strong><font size="1">Modèle</td>';
    	echo '<td style="border-color:#000000;border-width:1;border-style:dotted">eeee</td>';
    	echo'</tr>';
    	echo '<tr>';
    	echo '<td class="largeur" style="border-color:#000000;border-width:1;border-style:dotted"><strong><font size="1">N° série Machine</td>';
    	echo '<td style="border-color:#000000;border-width:1;border-style:dotted">eeee</td>';
    	echo'</tr>';
    	echo '<tr>';
    	echo '<td style="border-color:#000000;border-width:1;border-style:dotted"><strong><font size="1">N° Inventaire [A.H]</td>';
    	echo '<td style="border-color:#000000;border-width:1;border-style:dotted">eeee</td>';
    	echo'</tr>';
     
    	if($data['os'] == '')
    	{
    	echo '<tr>';
    	echo '<td style="border-color:#000000;border-width:1;border-style:dotted"><strong><font size="1">Système Exploitation</td>';
    	echo '<td style="border-color:#000000;border-width:1;border-style:dotted">';
     
    	if (isset($_POST["os"])) {
    	$sql_os="UPDATE spare SET os='".$_POST["os"]."' WHERE id_spare='$id'";
    	$exe_sqlos = mysql_query($sql_os) or die('Erreur SQL !');
    							}
     
    	echo '<form name="maj_os" method="POST" Action="index.php?numlien=fich_spare&id='.$id.'">';
    	echo '<input type="text" name="os" value="">&nbsp&nbsp <a href="#" onclick="fonction1();refresh()"><IMG src="image/stylo.png" style="border:"0""></a>';
    	echo'</form></td>';
    	}
    	else
    	{
    	echo '<tr>';
    	echo '<td style="border-color:#000000;border-width:1;border-style:dotted"><strong><font size="1">Système Exploitation</td>';
    	echo '<td style="border-color:#000000;border-width:1;border-style:dotted">'.$data['os'].'</td>';
    	echo'</tr>';
    	}
     
    	if($data['ram'] == '') 
    	{
    	echo '<tr>';
    	echo '<td style="border-color:#000000;border-width:1;border-style:dotted"><strong><font size="1">Mémoire vive [R.A.M]</td>';
    	echo '<td style="border-color:#000000;border-width:1;border-style:dotted">'; 
     
    	if (isset($_POST["ram"])) {
    	$sql_ram="UPDATE spare SET ram='".$_POST["ram"]."' WHERE id_spare='$id'";
    	$exe_sqlram = mysql_query($sql_ram) or die('Erreur SQL !');
    						}
     
    	echo '<form name="maj_ram" method="POST" Action="index.php?numlien=fich_spare&id='.$id.'">';
    	echo '<input type="text" name="ram" value="">&nbsp&nbsp <a href="#" onclick="fonction2();refresh()"><IMG src="image/stylo.png" style="border:"0""></a>';
    	echo'</form></td>';
    	}					
    		else
    	{
    	echo '<tr>';
    	echo '<td style="border-color:#000000;border-width:1;border-style:dotted"><strong><font size="1">Mémoire vive [R.A.M]</td>';
    	echo '<td style="border-color:#000000;border-width:1;border-style:dotted">'.$data['ram'].'</td>';
    	echo'</tr>';
    	}				
    	echo '<tr>';
    	echo '<td style="border-color:#000000;border-width:1;border-style:dotted"><strong><font size="1">Disque Dur [H.D]</td>';
    	echo '<td style="border-color:#000000;border-width:1;border-style:dotted">eeee</td>';
    	echo'</tr>';
    	echo '<tr>';
    	echo '<td style="border-color:#000000;border-width:1;border-style:dotted"><strong><font size="1">Vidéo [Carte Graphique]</td>';
    	echo '<td style="border-color:#000000;border-width:1;border-style:dotted">eeee</td>';
    	echo'</tr>';
    	echo '<tr>';
    	echo '<td style="border-color:#000000;border-width:1;border-style:dotted"><strong><font size="1">Sans Fil [Wifi]</td>';
    	echo '<td style="border-color:#000000;border-width:1;border-style:dotted">eeee</td>';
    	echo'</tr>';
    	echo '<tr>';
    	echo '<td style="border-color:#000000;border-width:1;border-style:dotted"><strong><font size="1">Carte réseau [LAN]</td>';
    	echo '<td style="border-color:#000000;border-width:1;border-style:dotted">eeee</td>';
    	echo'</tr>';
    	echo'</table>';
     
    	}
     
     
    mysql_close();
    ?>

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

Discussions similaires

  1. [PHP-JS] passage d'une variable php à une fonction javascript
    Par pimpmyride dans le forum Général JavaScript
    Réponses: 5
    Dernier message: 07/04/2006, 16h17
  2. [PHP-JS] du javaScript dans du php ?
    Par Tempotpo dans le forum Langage
    Réponses: 7
    Dernier message: 03/04/2006, 17h34
  3. Réponses: 8
    Dernier message: 10/11/2005, 15h58
  4. [PHP-JS] Execution d'un script php dans du javascript
    Par Anduriel dans le forum Général JavaScript
    Réponses: 2
    Dernier message: 13/06/2005, 20h41
  5. [PHP-JS] var javaScript avec test php
    Par lepierre dans le forum Général JavaScript
    Réponses: 2
    Dernier message: 01/12/2004, 12h58

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