html - change background color every 5 press with php only -
i need make button php. fifth time when pressed should change background color random 1 , keep of every 5 presses. after 5 press background color should change again , keep until next 5 press
here code
<?php session_start(); $_session['counter'] = isset($_session['counter']) ? $_session['counter'] : 0; if($_post['sub']) { $_session['counter']++; echo "<br/>"; echo $culoare; echo "<body bgcolor='<?phpecho $culoare;?>'></body>"; } ?> </head> <body > <form action='' method="post"> <input type="submit" name="sub" value="click" /> <input type="hidden" name="counter" value="<?php print $_post['counter']; ?>" /> </form> </body>
what problem? have code.
when form submitted, value of counter in hidden field. if value not equal 5, increment , pass again view store hidden field. when value equal 5, change background color , set counter 0.
you need keep color in hidden field if want have same color 5 times.
<?php // first call. if(!isset($_post)) { $color = "rgb(" . rand(0, 255) . "," . rand(0,255) . "," . rand(0,255) . ")"; $counter = 0; } else { $color = $_post['color']; $counter = $_post['counter']; } if($counter == 5) { $color = "rgb(" . rand(0, 255) . "," . rand(0,255) . "," . rand(0,255) . ")"; $counter = 1; // set 1 if want change bg color on fifth click. } else { $counter++; } ?> <div style="background-color: <?php echo $color; ?>"> <form method="post"> <input type="hidden" name="color" value="<?php echo $color; ?>" /> <input type="hidden" name="counter" value="<?php echo $counter; ?>" /> <input type="submit" value="submit" /> </form> </div>
i think may work.
Comments
Post a Comment