Partage
  • Partager sur Facebook
  • Partager sur Twitter

Actualiser un script php sans update la page html

Actualiser un script php

Sujet résolu
    3 mai 2016 à 9:52:42

    Bonjour je suis en Termianle STI2D et j'ai mon projet pour le bac a finir pour 1 semaine mais je bloque. Je trvaille sur une raspberry pi 2 . Mon problème est le suivant comment faire update un script php sans update ma page .php ? J'ai un script qui active une commande shell pour récupéré un valeur d'une sonde ds18b20 dans un fichier bref la n'est pas le problème. Et cette valeur je veux qu'elle supdate sachant que la valeur dans le fichier elle s'update toute seul mais le script php ne se lance qu'une seule fois et après il faut refresh sois la page sois le script. pour la page je connais le http-equiv="refresh" .... Mais je ne veux pas refresh toute ma page, seulement le script comment faire ?

    Merci de votre aide,

    Cordialement.

    • Partager sur Facebook
    • Partager sur Twitter
      3 mai 2016 à 10:15:51

      Bonjour pouvez vous m'indique vers ou chercher ? 

      Ou me donnez un exemple ? 

      Cordialement.

      • Partager sur Facebook
      • Partager sur Twitter
        3 mai 2016 à 11:17:55

        j'ai actuellement ce script :

        <?php
        $Commande = shell_exec ('cat /sys/bus/w1/devices/28-*/w1_slave | grep "t=" | cut -d "=" -f 2 | cut -b 1-2'); 
        echo "$Commande";
        ?>


        Je voudrais qu'il s'update toute les x secondes. si je doit passez par ajax comment faire ? 

        Merci d'avance.

        Cordialement

        • Partager sur Facebook
        • Partager sur Twitter
          4 mai 2016 à 10:27:58

          Salut,

          le script à placer dans le .php contenant le script à actualiser:

          <script type="text/javascript">
          	function notif() {
          	$.ajax({
          			     
          	url: "refresh.php",			         
          			 
          	ifModified:true,
          	success: function(content){
          	$('#notifications').html(content); //id de la <div> à refresh
          	}
             });
            setTimeout(notif, 1000); //refresh toutes secondes (1 minute = 60000)
            }
            notif();
          </script>

          Dans cette même page, à l'endroit où tu veux afficher et refresh, tu fais:

          <div id="notifications"></div>

          et tu créée un script .php nommé "refresh.php" qui contient ton code à exécuter:

          <?php
          $Commande = shell_exec ('cat /sys/bus/w1/devices/28-*/w1_slave | grep "t=" | cut -d "=" -f 2 | cut -b 1-2');
          echo "$Commande";
          ?>

          Voila, dis moi si c'est OK ;)



          -
          Edité par g3kO 4 mai 2016 à 10:30:58

          • Partager sur Facebook
          • Partager sur Twitter
            6 mai 2016 à 20:23:06

            Merci je te dit sa lundi quand je retourne en cour ;) 

            Cordialement.

            • Partager sur Facebook
            • Partager sur Twitter
              9 mai 2016 à 11:38:12

              Bonjour j'ai fait ce que tu m'a dit. J'ai dans ma page de base : 

              <!DOCTYPE html>
              <html>
                <head>
              <script type="text/javascript">
                  function notif() {
                  $.ajax({
                                
                  url: "refresh.php",                  
                            
                  ifModified:true,
                  success: function(content){
                  $('#notifications').html(content); //id de la <div> à refresh
                  }
                 });
                setTimeout(notif, 1000); //refresh toutes secondes (1 minute = 60000)
                }
                notif();
              </script>
              		<meta charset="utf-8">
              		<SCRIPT LANGUAGE="JavaScript">
              		var delay=0.3 // ici 10 secondes
              		var img="pic"; // ici nom de l'image a recharger
              		var src="image/pic.jpg"
              		// fonction ‡ appeler dans le onload de BODY
              		function RefreshIMG() {
              		setTimeout("RefreshIMG()",delay*1000);
              		document.images[img].src=src+"?a="+Math.random(1);
              		}
              		</SCRIPT>
              <?php
               function servo(){
              $Commande = shell_exec ('sudo python Servo.py ');
              echo "$Commande";
               }
              ?>
                  <title>Contrôle</title>
                </head>
              
                <body onLoad="RefreshIMG()"; style="background-image: url(/image/fond.jpg); background-repeat:repeat;">
                  <title>Controle</title>
                  <table style="width: 342px; height: 98px;" border="3">
                    <tbody>
                      <tr>
                        <td onmouseover="javascript:this.style.background='#efefef" onmouseout="javascript:this.style.background='#transparent'"
              
                          onclick="location='index.php'" style="cursor: pointer; text-align: center; width: 154.983px;"
              
                          bgcolor="transparent">
                          <h2 style="background-color: transparent;"><span style="color: rgb(255, 255, 255);">Accueil</span></h2>
                        </td>
                            </span></h2>
                        </th>
                      </tr>
                    </tbody>
                  </table>
              
                  <span style="font-weight: bold;"><br>
                  </span>
                  <div style="text-align: center;">
                    <div style="text-align: left;">
              
                      <table style="width: 988px; text-align: left; margin-left: auto; margin-right: auto;"border="0">
                        <tbody>
                          <tr>
                            <td style="text-align: center; width: 440px;">
                              <h1><span style="color: white;"><span style="font-weight: bold;">Webcam
                                    live</span></span></h1>
                            </td>
                              <td  style="width: 0px; height: 80.8667px; text-align: center;">
                              <h1><span style="color: white;">Controle des compossants</h1>
                            </td>
                          <td>
                              <h1  style="text-align: center;"><span style="color: white;">Température de l'aquarium</h1>
                            </td>
                          </tr>
                          <tr>
                            <td style="text-align: center;">
                    <img id="pic" name="pic"style="width: 300px; height: 217px;"src="image/pic.jpg">
              
                 
                          </td>
                            <td style="background-color: transparent; text-align: center; width: 988px;">
                              <div style="margin-left: 80px;">
                      </div>
                   <!-- On/Off button's picture -->
              	 <?php
              	 //this php script generate the first page in function of the gpio's status
              	 $status = array (0, 0, 0, 0,);
              	 for ($i = 1; $i < count($status); $i++) {
              		//set the pin's mode to output and read them
              		system("gpio mode ".$i." out");
              		exec ("gpio read ".$i, $status[$i], $return );
              		//if off
              		if ($status[$i][0] == 0 ) {
              		echo ("<img id='button_".$i."' src='data/img/red/red_".$i.".jpg' alt='off'/>");
              		}
              		//if on
              		if ($status[$i][0] == 1 ) {
              		echo ("<img id='button_".$i."' src='data/img/green/green_".$i.".jpg' alt='on'/>");
              		}	 
              	 }
              	 ?>
              	 <!-- javascript -->
              	 <script src="script.js"></script>
                            </td>
                          <td>
              <h1  style="text-align: center;"><span style="color: white;">
              <div id="notifications"></div>
              </h1>
                            </td>
                          </tr>
                        </tbody>
                      </table>
                      <span style="font-weight: bold;"><br>
                      </span></div>
                    <span style="font-weight: bold;"></span></div>
                </body>
              </html>

              J'ai donc le div a l'emplacement souhaiter et le script dans le head.

              J'ai aussi mon autres fichier refresh.php qui contient ceci. Faut t'il des balise ou autres dans ce fichier ? 

              <?php
              $Commande = shell_exec ('cat /sys/bus/w1/devices/28-*/w1_slave | grep "t=" | cut -d "=" -f 2 | cut -b 1-2'); 
              echo "$Commande";
              ?>

              Voilà merci de votre aide 

              Cordialement.

              -
              Edité par Lecaramel56 9 mai 2016 à 11:39:22

              • Partager sur Facebook
              • Partager sur Twitter
                9 mai 2016 à 22:32:57

                Bonjour,

                désolé du délais de réponse,

                je ne vois pas l'appel à jQuery dans ton code ??

                Ajoute ceci dans ton head et dis moi quoi ;)

                <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.min.js"></script>



                -
                Edité par g3kO 9 mai 2016 à 22:41:32

                • Partager sur Facebook
                • Partager sur Twitter
                  10 mai 2016 à 15:37:53

                  Bonjour, j'ai ajouter la source jquery : 

                  <!DOCTYPE html>
                  <html>
                  <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.min.js">
                      function notif() {
                      $.ajax({
                                    
                      url: "refresh.php",                  
                                
                      ifModified:true,
                      success: function(content){
                      $('#notifications').html(content); //id de la <div> à refresh
                      }
                     });
                    setTimeout(notif, 1000); //refresh toutes secondes (1 minute = 60000)
                    }
                    notif();
                  </script>
                  		<meta charset="utf-8">
                  		<SCRIPT LANGUAGE="JavaScript">
                  		var delay=0.3 // ici 10 secondes
                  		var img="pic"; // ici nom de l'image a recharger
                  		var src="image/pic.jpg"
                  		// fonction ‡ appeler dans le onload de BODY
                  		function RefreshIMG() {
                  		setTimeout("RefreshIMG()",delay*1000);
                  		document.images[img].src=src+"?a="+Math.random(1);
                  		}
                  		</SCRIPT>
                  <?php
                   function servo(){
                  $Commande = shell_exec ('sudo python Servo.py ');
                  echo "$Commande";
                   }
                  	$test = shell_exec('cat /home/pi/test.txt | grep "=" | cut -d "=" -f 2 | cut -b 1-2');
                  	echo "$test";
                  ?>
                  <head>
                      <title>Contrôle</title>
                    </head>
                  
                    <body onLoad="RefreshIMG()"; style="background-image: url(/image/fond.jpg); background-repeat:repeat;">
                      <title>Controle</title>
                      <table style="width: 342px; height: 98px;" border="3">
                        <tbody>
                          <tr>
                            <td onmouseover="javascript:this.style.background='#efefef" onmouseout="javascript:this.style.background='#transparent'"
                  
                              onclick="location='index.php'" style="cursor: pointer; text-align: center; width: 154.983px;"
                  
                              bgcolor="transparent">
                              <h2 style="background-color: transparent;"><span style="color: rgb(255, 255, 255);">Accueil</span></h2>
                            </td>
                                </span></h2>
                            </th>
                          </tr>
                        </tbody>
                      </table>
                  
                      <span style="font-weight: bold;"><br>
                      </span>
                      <div style="text-align: center;">
                        <div style="text-align: left;">
                  
                          <table style="width: 988px; text-align: left; margin-left: auto; margin-right: auto;"border="0">
                            <tbody>
                              <tr>
                                <td style="text-align: center; width: 440px;">
                                  <h1><span style="color: white;"><span style="font-weight: bold;">Webcam
                                        live</span></span></h1>
                                </td>
                                  <td  style="width: 0px; height: 80.8667px; text-align: center;">
                                  <h1><span style="color: white;">Controle des composants</h1>
                                </td>
                              <td>
                                  <h1  style="text-align: center;"><span style="color: white;">Température de l'aquarium</h1>
                                </td>
                              </tr>
                              <tr>
                                <td style="text-align: center;">
                        <img id="pic" name="pic"style="width: 300px; height: 217px;"src="image/pic.jpg">
                  
                     
                              </td>
                                <td style="background-color: transparent; text-align: center; width: 988px;">
                                  <div style="margin-left: 80px;">
                          </div>
                       <!-- On/Off button's picture -->
                  	 <?php
                  	 //this php script generate the first page in function of the gpio's status
                  	 $status = array (0, 0, 0, 0,);
                  	 for ($i = 1; $i < count($status); $i++) {
                  		//set the pin's mode to output and read them
                  		system("gpio mode ".$i." out");
                  		exec ("gpio read ".$i, $status[$i], $return );
                  		//if off
                  		if ($status[$i][0] == 0 ) {
                  		echo ("<img id='button_".$i."' src='data/img/red/red_".$i.".jpg' alt='off'/>");
                  		}
                  		//if on
                  		if ($status[$i][0] == 1 ) {
                  		echo ("<img id='button_".$i."' src='data/img/green/green_".$i.".jpg' alt='on'/>");
                  		}	 
                  	 }
                  	 ?>
                  	 <!-- javascript -->
                  	 <script src="script.js"></script>
                  <form method="post" action"servo.php">
                  <input type="submit" value="Nourir">
                                </td>
                              <td>
                  <h1  style="text-align: center;"><span style="color: white;">
                  <div id="notifications"></div>
                  </h1>
                                </td>
                              </tr>
                            </tbody>
                          </table>
                          <span style="font-weight: bold;"><br>
                          </span></div>
                        <span style="font-weight: bold;"></span></div>
                    </body>
                  </html>

                  Et sa ne marche toujours pas. Rien ne s'affiche dans ma div la commande est bonne je les tester. Il ne reste qu'une problème venant du script ou si il faut autre chose dans le refresh.php autres que le >?php .....  ?< .

                  Merci d'avance,

                  Cordialement.

                  -
                  Edité par Lecaramel56 10 mai 2016 à 16:44:09

                  • Partager sur Facebook
                  • Partager sur Twitter
                    11 mai 2016 à 23:54:06

                    Salut,

                    teste un coup en supprimant ton autre script qui se trouve dans ton head pour voir

                    • Partager sur Facebook
                    • Partager sur Twitter
                      12 mai 2016 à 16:40:26

                      Salut j'ai bosser dessu mercredi aprem-midi et j'ai fini par réusir je vous envoie le code que j'ai utiliser pour en aider un futur dans mon cas ;) .

                      dans le html :

                      <script type="text/javascript">
                      		var t = self.setInterval("temperature()",1000);
                          		function temperature() {
                      			$.ajax({
                      				url: 'refresh.php',
                      				complete: function (response) {
                      					$('#notifications').html(response.responseText);
                      				}
                      			}); 
                      		}		
                      	</script>	

                      dans le body :

                      <div id="notifications">

                      dans le refresh.php :

                      <?php
                      $Commande = shell_exec('cat /sys/bus/w1/devices/28-*/w1_slave | grep "t=" | cut -d "=" -f 2');
                      $Commande = $Commande / 1000;
                      echo "$Commande °C";
                      ?>

                      Voilà merci comme même pour votre aide :)

                      Cordialement.

                      -
                      Edité par Lecaramel56 19 mai 2016 à 16:32:31

                      • Partager sur Facebook
                      • Partager sur Twitter
                        12 mai 2016 à 20:20:57

                        Salut, super, passe le sujet en résolu :)

                        ps: on ne vois pas ton code ^^

                        • Partager sur Facebook
                        • Partager sur Twitter
                          16 mai 2016 à 11:05:26

                          Bonjour,  il est possible de voire le code ? car j'ai le même problème que toi ...
                          • Partager sur Facebook
                          • Partager sur Twitter
                            19 mai 2016 à 16:33:10

                            Voilà je vous est rajouter le code bonne chance a vous :)
                            • Partager sur Facebook
                            • Partager sur Twitter
                              19 mai 2016 à 16:37:17

                              TheLumMys a écrit:

                              Bonjour,  il est possible de voire le code ? car j'ai le même problème que toi ...

                              Voilà je vous est rajouter le code bonne chance a vous :)



                              • Partager sur Facebook
                              • Partager sur Twitter

                              Actualiser un script php sans update la page html

                              × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
                              × Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.
                              • Editeur
                              • Markdown