Probleme d'eciture dans un fichier

Probleme d'eciture dans un fichier - PHP - Programmation

Marsh Posté le 21-08-2005 à 02:40:52    

Bonjour,
 
J'ai fait un petit script sans prétention qui ne marche pas bien...Il est dans un fichier index.php
Le voici :
 

Code :
  1. <?php
  2. include ('cache/config.php');
  3. function changevaleur() {
  4. $filename = "./cache/config.php";
  5. if ($options['jeux'] == "1" )
  6. {
  7.  $content  = "<?php \$options['jeux'] = 0 ?>";
  8.  if ($file = fopen($filename, "w" ))
  9.  {
  10.  fwrite($file, $content);
  11.  fclose($file);
  12.  exit();
  13.  $options['jeux'] = 0;
  14.  }
  15. }
  16. else
  17. {
  18.  $content  = "<?php \$options[\'jeux\'] = 1 ?>";
  19.  if ($file = fopen($filename, "w" ))
  20.  {
  21.  fwrite($file, $content);
  22.  fclose($file);
  23.  exit();
  24.  $options['jeux'] = 1;
  25.  }
  26. }
  27. redirect_header("index.php",2);
  28. }
  29. echo "<center>";
  30. if ($options['jeux'] == "1" )
  31. {
  32.  echo "<img src='./images/image2.gif'>";
  33. }
  34. else
  35. {
  36.  echo "<img src='./images/image1.gif'>";
  37. }
  38. echo "<br>";
  39. echo "<form method='post' action='index.php'>";
  40. echo "<input type='submit' name='submit' value='Changer' onclick='<?php changevaleur() ?>'>";
  41. echo "</form>";
  42. echo "</center>";
  43. ?>


 
et voici le contenu du fichier config.php :
 

Code :
  1. <?php $options['jeux'] = 0; ?>


 
Quand je change la valeur 0 par 1 à la main, ca change bien l'image, mais quand je clique sur le bouton changer, ca ne change pas la valeur...Et je ne comprend pas pourquoi...
 
Pourriez-vous m'aider ?
Merci

Reply

Marsh Posté le 21-08-2005 à 02:40:52   

Reply

Marsh Posté le 21-08-2005 à 12:23:06    

un petit coup de main ?

Reply

Marsh Posté le 21-08-2005 à 12:29:00    

Je crois qu'il faudrait que tu te renseignes un peu sur les rôles et les localisation d'exécution du Javascript et du PHP [:petrus75]
 
Parce que là t'essaies d'exécuter un script PHP depuis un évènement Javascript [:petrus75]
 
Ce qui peut grosso merdo être apparenté au fait de tenter de piloter un A340 volant au dessus des Apalache depuis ton pieu au moyen de ta brosse à chiottes [:petrus75]


---------------
I mean, true, a cancer will probably destroy its host organism. But what about the cells whose mutations allow them to think outside the box, and replicate and expand beyond their wildest dreams by throwing away the limits imposed by overbearing genetic r
Reply

Marsh Posté le 21-08-2005 à 12:38:30    

ouaip peut etre, mais je ne sais pas comment faire pour qu'un bete bouton puisse déclencher l'execution de ma fonction...
 
C'est si compliqué que ca ? :(
Non, parce que j'ai déja vu plein de script qui soumettent des champ du genre de celui-ci :
 

Code :
  1. <?php
  2. include_once('./admin_header.php');
  3. if (!$HTTP_POST_VARS['submit'])
  4. {
  5. // Navigation Links
  6. $arcade_home = "<a href='./index.php'><font size=+1>Arcade Admin</font></a>";
  7. echo $arcade_home.'&nbsp;<font size=+1> >> </font>&nbsp;<font size=+1>'._MI_ARCADE_ADMENU1.'</font><br><br>';
  8.     echo "<br>";
  9. ?>
  10. <form method="post" action="config.php">
  11. <table width="100%" border="0" cellspacing="0" cellpadding="5">
  12. <tr>
  13. <td valign="top" width="25%"><?php echo _MDA_LATEST;?>:</td>
  14. <td valign="top">
  15. <?php
  16. $chk1 = ''; $chk0 = '';
  17. ($options['latest'] == 1) ? $chk1 = " checked='checked'" : $chk0 = " checked='checked'";
  18. ?>
  19. <input type="radio" class="radio" name="latest" value="1"<?php echo $chk1;?> /> <?php echo _YES;?>
  20. <input type="radio" class="radio" name="latest" value="0"<?php echo $chk0;?> /> <?php echo _NO;?>
  21. </tr>
  22. <tr>
  23. <td valign="top" width="25%"><?php echo _MDA_GAMES_LATEST;?>:</td>
  24. <td valign="top"><input type='text' class='text' name='games_latest' value='<?php echo intval($options['games_latest']); ?>'></td>
  25. </tr>
  26. <tr>
  27. <td valign="top" width="25%"><?php echo _MDA_GAMES_LATEST_BLOCK;?>:</td>
  28. <td valign="top"><input type='text' class='text' name='games_latest_block' value='<?php echo intval($options['games_latest_block']); ?>'></td>
  29. </tr>
  30. <tr>
  31. <td valign="top" width="25%"><?php echo _MDA_GAMES_PER_PAGE;?>:</td>
  32. <td valign="top"><input type='text' class='text' name='games_per_page' value='<?php echo intval($options['games_per_page']); ?>'></td>
  33. </tr>
  34. <tr>
  35. <td valign="top" width="25%"><?php echo _MDA_CAT;?>:</td>
  36. <td valign="top">
  37. <?php
  38. $chk1 = ''; $chk0 = '';
  39. ($options['cat'] == 1) ? $chk1 = " checked='checked'" : $chk0 = " checked='checked'";
  40. ?>
  41. <input type="radio" class="radio" name="cat" value="1"<?php echo $chk1;?> /> <?php echo _YES;?>
  42. <input type="radio" class="radio" name="cat" value="0"<?php echo $chk0;?> /> <?php echo _NO;?>
  43. </tr>
  44. <tr>
  45. <td valign="top" width="25%"><?php echo NUMBER_CATS_ROW;?>:</td>
  46. <td valign="top"><input type='text' class='text' name='number_cats_row' value='<?php echo intval($options['number_cats_row']); ?>'></td>
  47. </tr>
  48. <tr>
  49. <td valign="top" width="25%"><?php echo _MDA_CAT_IMAGES;?>:</td>
  50. <td valign="top">
  51. <?php
  52. $chk1 = ''; $chk0 = '';
  53. ($options['cat_images'] == 1) ? $chk1 = " checked='checked'" : $chk0 = " checked='checked'";
  54. ?>
  55. <input type="radio" class="radio" name="cat_images" value="1"<?php echo $chk1;?> /> <?php echo _YES;?>
  56. <input type="radio" class="radio" name="cat_images" value="0"<?php echo $chk0;?> /> <?php echo _NO;?>
  57. </tr>
  58. <tr>
  59. <td valign="top" width="25%"><?php echo NUMBER_BEPOPULAR;?>:</td>
  60. <td valign="top"><input type='text' class='text' name='game_bepopular' value='<?php echo intval($options['game_bepopular']); ?>'></td>
  61. </tr>
  62. <tr>
  63. <td valign="top" width="25%"><?php echo _MDA_SCORES_LIMIT;?>:</td>
  64. <td valign="top"><input type='text' class='text' name='scores_limit' value='<?php echo intval($options['scores_limit']); ?>'></td>
  65. </tr>
  66. <tr>
  67. <td valign="top" width="25%"><?php echo _MDA_POSITION;?>:</td>
  68. <td valign="top">
  69. <?php
  70. if($options['position'] == '0')
  71. {
  72. $chk0 = " checked='checked'";
  73. }
  74. if($options['position'] == '1')
  75. {
  76. $chk1 = " checked='checked'";
  77. }
  78. if($options['position'] == '2')
  79. {
  80. $chk2 = " checked='checked'";
  81. }
  82. if($options['position'] == '3')
  83. {
  84. $chk3 = " checked='checked'";
  85. }
  86. // 0 = Under
  87. // 2 = Up
  88. ?>
  89. <input type="radio" class="radio" name="position" value="1"<?php echo $chk1;?> /> <?php echo "Right";?>
  90. <input type="radio" class="radio" name="position" value="3"<?php echo $chk3;?> /> <?php echo "Left";?>
  91. <tr>
  92. <td colspan="2" align="center">
  93. <br>
  94. <input type="hidden" name="mode" value="config">
  95. <input type="submit" class="button" name="submit" value="<?php echo _SAVE;?>">
  96. </td>
  97. </tr>
  98. </form></table>
  99. <?php
  100. } else {
  101. $content  = "<?php\n";
  102. $content .= "\$options['games_latest'] = ".intval($HTTP_POST_VARS['games_latest']).";\r\n";
  103. $content .= "\$options['games_latest_block'] = ".intval($HTTP_POST_VARS['games_latest_block']).";\r\n";
  104. $content .= "\$options['number_cats_row'] = ".intval($HTTP_POST_VARS['number_cats_row']).";\r\n";
  105. $content .= "\$options['games_per_page'] = ".intval($HTTP_POST_VARS['games_per_page']).";\r\n";
  106. $content .= "\$options['cat'] = ".intval($HTTP_POST_VARS['cat']).";\r\n";
  107. $content .= "\$options['cat_images'] = ".intval($HTTP_POST_VARS['cat_images']).";\r\n";
  108. $content .= "\$options['game_bepopular'] = ".intval($HTTP_POST_VARS['game_bepopular']).";\r\n";
  109. $content .= "\$options['latest'] = ".intval($HTTP_POST_VARS['latest']).";\r\n";
  110. $content .= "\$options['scores_limit'] = ".intval($HTTP_POST_VARS['scores_limit']).";\r\n";
  111. $content .= "\$options['position'] = ".intval($HTTP_POST_VARS['position']).";\r\n";
  112. $content .= "?>";
  113. $filename = "../cache/config.php";
  114. if ( $file = fopen($filename, "w" ) ) {
  115. fwrite($file, $content);
  116. fclose($file);
  117. } else {
  118.  redirect_header("index.php", 1, _NOTUPDATED);
  119.  exit();
  120. }
  121. redirect_header("config.php", 1, _UPDATED);
  122. exit();
  123. }
  124. CloseTable();
  125. xoops_cp_footer();
  126. exit();
  127. ?>


 
Et celui-la fonctionne super bien...Donc je ne pige pas trop la...

Reply

Marsh Posté le 21-08-2005 à 13:18:19    

Un formulaire appelle une fonction javascript ou un fichier sur un serveur.
Un formulaire n'appelle jamais une fonction php.
 
D'ailleur :
php = ex&écution serveur zavant envoie au navigateur
javascript = exécution navigateur aprés réception de la page

Reply

Marsh Posté le 21-08-2005 à 13:40:00    

Merci omega2, j'avais bien compris...Essaye à ton tour de me comprendre...merci...
 
Bon, j'ai modifié comme suit :
 

Code :
  1. <?php
  2. include ('../../mainfile.php');
  3. include ('cache/config.php');
  4. include_once(XOOPS_ROOT_PATH."/header.php" );
  5. function changevaleur() {
  6. $filename = "./cache/config.php";
  7. if ($options['jeux'] == 1)
  8. {
  9.  $content  = "<?php \$options['jeux'] = 0 ?>";
  10.  if ($file = fopen($filename, "w" ))
  11.  {
  12.  fwrite($file, $content);
  13.  fclose($file);
  14.  exit();
  15.  }
  16. }
  17. else
  18. {
  19.  $content  = "<?php \$options['jeux'] = 1 ?>";
  20.  if ($file = fopen($filename, "w" ))
  21.  {
  22.  fwrite($file, $content);
  23.  fclose($file);
  24.  exit();
  25.  }
  26. }
  27. redirect_header("index.php",2);
  28. }
  29. echo "<center>";
  30. if ($options['jeux'] == 1)
  31. {
  32.  echo "<img src='./images/image2.gif'>";
  33. }
  34. else
  35. {
  36.  echo "<img src='./images/image1.gif'>";
  37. }
  38. echo "<br>";
  39. echo "<form method='post' action='index.php'>";
  40. echo "<input type='submit' name='submit' value='Changer'>";
  41. echo "</form>";
  42. echo "</center>";
  43. include_once(XOOPS_ROOT_PATH."/footer.php" );
  44. if ($HTTP_POST_VARS['submit'])
  45. {
  46. changevaleur();
  47. }
  48. ?>


 
mais ca marche pas bien...
 
si $options['jeux'] = 0, image1 s'affiche...Quand je clique sur le bouton, rien ne change, je reclique, image2 s'affiche à la place de image1...et appres, je peux cliquer comme je veux, plus rien ne change...argh !


Message édité par bagu le 21-08-2005 à 13:41:02
Reply

Marsh Posté le 21-08-2005 à 14:48:45    

bon...j'ai encore tout changé (merci ash)
 

Code :
  1. <?php
  2. include('./cache/config.php');
  3. function saveconf($conf) {
  4. if (!is_array($conf)) {
  5.  trigger_error('saveconf: invalid argument, $conf must be an array');
  6.  return false;
  7. }
  8. $fp = fopen('./cache/config.php', 'w') or die('error opening conf file');
  9. fwrite($fp, '<?php $options = ' . var_export($conf, true) . ' ?>');
  10. fclose($fp);
  11. }
  12. if ($_POST['submit']) {
  13. $options['jeux'] = !$options['jeux'];
  14. saveconf($options);
  15. redirect_header('./index.php', 0);
  16. die;
  17. }
  18. $images = array('./images/image1.gif', './images/image2.gif');
  19. ?>
  20. <p><img src="<?php echo $images[$options['jeux']]; ?>" /></p>
  21. <form method="post" action="index.php">
  22. <input type="submit" name="submit" value="changer" />
  23. </form>


 
et ca marche, par contre, je cherche le moyen de ne recharger que l'image au lieu de toute la page...on m'a dit qu'en javascript c'etait faisable...Comment faire ?
Merci

Reply

Marsh Posté le 21-08-2005 à 16:09:57    

Salut,
 
Tu peut utiliser les xmlhttprequest pour ton pb.
 
http://developpeur.journaldunet.co [...] st-2.shtml
 
Dans ce cas c'est des req. SQL mais bon tu modifie comme tu veut :).
 
+.


---------------
Feedback
Reply

Marsh Posté le 21-08-2005 à 16:14:13    

Merci...
 
Cependant, ca marche bien maintenant...
 
J'ai un autre petit pb :

Code :
  1. <?php
  2. function b_tetrinet_show() {
  3. require_once(XOOPS_URL.'/modules/tetrinet/cache/config.php');
  4. switch ($options['jeux']) {
  5. case true:
  6. $images = XOOPS_URL.'/modules/tetrinet/images/image1.gif';
  7. break;
  8. case false:
  9. $images = XOOPS_URL.'/modules/tetrinet/images/image2.gif';
  10. break;
  11. }
  12. $block             = array();
  13. $block['title']    = "Tetrinet";
  14. $block['content']  = "<p><img src=".$images." /></p>";
  15. $block['content'] .= var_dump($options['jeux']);
  16. return $block;
  17. }
  18. ?>


 
Et le var_dump me retourne la valeur NULL et je ne pige pas pourquoi...j'ai pourtant incul le fichier config.php afin qu'il récupere la valeur...
 
C'est space...

Reply

Marsh Posté le 21-08-2005 à 16:15:59    

Bah ton tableau est défini à l'extèrieur de ta fonction?
Si oui défini le en global dans ta fonction nan?


---------------
Feedback
Reply

Marsh Posté le 21-08-2005 à 16:15:59   

Reply

Marsh Posté le 21-08-2005 à 16:16:03    

un global $var (avec le bon nom de variable) bien placé régle généralement ce genre de probléme. ;)

Reply

Marsh Posté le 21-08-2005 à 16:36:53    

donc si j'ai bien pigé, il faut que je rajoute  
 

Code :
  1. global $options;


 
ligne 3 par ex ?

Reply

Marsh Posté le 21-08-2005 à 16:42:00    

exactly :D.


---------------
Feedback
Reply

Marsh Posté le 21-08-2005 à 16:47:45    

nickel
 
Merci beaucoup ;)

Reply

Sujets relatifs:

Leave a Replay

Make sure you enter the(*)required information where indicate.HTML code is not allowed