Jump to content

Count how many times the submit button has been hit


keepAway

Recommended Posts

Probably i`m the newbiest guy around here, so i have some questions for you guys.

I have the following code:

 

<?php

$urna=array(0,1,2,3,4,5,6,7,8,9,"a","b","c","d","e","f");

$culoare="#";

 

for ($i=1; $i<=6; $i++)

    $culoare.=$urna[rand(0,15)];

?>

 

<body bgcolor="<?php echo $culoare;?>">

<form method=post>

<input type="submit" name="submit" value="Schimba culoarea" />

</form>

 

 

Now, i need to "count" how many times the "submit" button has been hit, and for every 4 hits the color will be changed.

So, anyone have any clue how could i do that?

i have no idea why you would want to, but hey cool lol

i think youd be better using sessions as they transcend the refresh

<?php
session_start();
$urna=array(0,1,2,3,4,5,6,7,8,9,"a","b","c","d","e","f");
$cs="#";

if($_SESSION['hit']>=4)
{
  for ($i=1; $i<=6; $i++)
  {
    $b .= $urna[rand(0,15)];
  }
  $_SESSION['culoare'] = $cs.$b;
}
else
{
  $_SESSION['hit']++;
}
?>

<body bgcolor="<?php echo $_SESSION['culoare']; ?>">
<form method=post>
<input type="submit" name="submit" value="Schimba culoarea" />
</form>

This code works:

<?php
session_start();
$urna=array_merge(range(0,9),range('a','f'));
$_SESSION['hit'] = (!isset($_SESSION['hit']))?0:$_SESSION['hit']+1;

if(($_SESSION['hit'] % 4) == 0)
{
shuffle($urna);
$_SESSION['culoare'] = '#' . implode('',array_slice($urna,0,6));
}
?>
<html>
<head>
	<title>Random Background</title>
</head>
<body style="background-color:<?php echo $_SESSION['culoare']; ?>">
<?php echo '<pre>' . print_r($_SESSION,true) . '</pre>'; ?>
<form method=post>
<input type="submit" name="submit" value="Schimba culoarea" />
</form>
</html>

 

Ken

This code works:

<?php
session_start();
$urna=array_merge(range(0,9),range('a','f'));
$_SESSION['hit'] = (!isset($_SESSION['hit']))?0:$_SESSION['hit']+1;

if(($_SESSION['hit'] % 4) == 0)
{
shuffle($urna);
$_SESSION['culoare'] = '#' . implode('',array_slice($urna,0,6));
}
?>
<html>
<head>
	<title>Random Background</title>
</head>
<body style="background-color:<?php echo $_SESSION['culoare']; ?>">
<?php echo '<pre>' . print_r($_SESSION,true) . '</pre>'; ?>
<form method=post>
<input type="submit" name="submit" value="Schimba culoarea" />
</form>
</html>

 

Ken

 

Thanks Ken, thanks dawsba ;)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.