onclick
Posté : 25 mai 2006, 22:32
Je cherche à lancer une fonction depuis un bouton. La function marche parfaitement si je la lance au chargement mais pas depuis le bouton.
Voir code ci dessous. (pas parfait!)
<html>
<head>
<title>exo1.php</title>
<?php
$val0=0;
$val1=0;
$val2=0;
$val3=0;
$val4=0;
$res1=0;
$res2=0;
$res3=0;
$res4=0;
echo "<table border=\"1\">\n";
echo "<tr>\n";
function aleatoire(){
$nombre = array();
global $res1;
global $res2;
global $res3;
global $res4;
for ($i = 0 ; $i < 5; $i++) {
$nombre[$i] = RAND(0,100);
echo "<td>" . $nombre[$i] ."</td>\n";
switch ($i) {
case 0:
$val0= $nombre[$i];
case 1:
$val1= $nombre[$i];
case 2:
$val2= $nombre[$i];
case 3:
$val3= $nombre[$i];
case 4:
$val4= $nombre[$i];
}
}
$res1= $val1-$val0;
$res2= $val2 - $val1;
$res3= $val3 - $val2;
$res4= $val4 - $val3;
echo "</tr>\n";
echo "</table>\n";
}
aleatoire();
//calcul();
?>
<?php
function calcul() {
global $res1;
global $res2;
global $res3;
global $res4;
echo "<table border=\"1\">\n";
echo "<tr>\n";
echo "<td>" . $res1 ."</td>\n";
echo "<td>" . $res2 ."</td>\n";
echo "<td>" . $res3 ."</td>\n";
echo "<td>" . $res4 ."</td>\n";
echo "</tr>\n";
echo "</table>\n";
}
?>
</head>
<body>
<table width="150" border="1">
<tr>
<td><form method="post" action="exo1.php">
<input type="submit" value="Retirer">
</form>
</td>
<td><form action="exo1.php">
<input type="button" value="calculer" onclick="calcul()">
</form>
</td>
</tr>
</table>
</body>
</html>
La fonction aleatoire se lance très bien. Si je supprime les // devant calcul() la fonction de lance juste après aleatoire() donc le code est correct. C'est depuis <td><form action="exo1.php">
<input type="button" value="calculer" onclick="calcul()">
</form>
que ça ne marche pas.
Merci
Voir code ci dessous. (pas parfait!)
<html>
<head>
<title>exo1.php</title>
<?php
$val0=0;
$val1=0;
$val2=0;
$val3=0;
$val4=0;
$res1=0;
$res2=0;
$res3=0;
$res4=0;
echo "<table border=\"1\">\n";
echo "<tr>\n";
function aleatoire(){
$nombre = array();
global $res1;
global $res2;
global $res3;
global $res4;
for ($i = 0 ; $i < 5; $i++) {
$nombre[$i] = RAND(0,100);
echo "<td>" . $nombre[$i] ."</td>\n";
switch ($i) {
case 0:
$val0= $nombre[$i];
case 1:
$val1= $nombre[$i];
case 2:
$val2= $nombre[$i];
case 3:
$val3= $nombre[$i];
case 4:
$val4= $nombre[$i];
}
}
$res1= $val1-$val0;
$res2= $val2 - $val1;
$res3= $val3 - $val2;
$res4= $val4 - $val3;
echo "</tr>\n";
echo "</table>\n";
}
aleatoire();
//calcul();
?>
<?php
function calcul() {
global $res1;
global $res2;
global $res3;
global $res4;
echo "<table border=\"1\">\n";
echo "<tr>\n";
echo "<td>" . $res1 ."</td>\n";
echo "<td>" . $res2 ."</td>\n";
echo "<td>" . $res3 ."</td>\n";
echo "<td>" . $res4 ."</td>\n";
echo "</tr>\n";
echo "</table>\n";
}
?>
</head>
<body>
<table width="150" border="1">
<tr>
<td><form method="post" action="exo1.php">
<input type="submit" value="Retirer">
</form>
</td>
<td><form action="exo1.php">
<input type="button" value="calculer" onclick="calcul()">
</form>
</td>
</tr>
</table>
</body>
</html>
La fonction aleatoire se lance très bien. Si je supprime les // devant calcul() la fonction de lance juste après aleatoire() donc le code est correct. C'est depuis <td><form action="exo1.php">
<input type="button" value="calculer" onclick="calcul()">
</form>
que ça ne marche pas.
Merci