Warning: Cannot use a scalar value as an array in /home/admin/public_html/forum/include/fm.class.php on line 757

Warning: Invalid argument supplied for foreach() in /home/admin/public_html/forum/include/fm.class.php on line 770
Форумы портала PHP.SU :: Версия для печати :: Помогите!!!Калькулятор с помощью формы
Форумы портала PHP.SU » PHP » Напишите за меня, пожалуйста » Помогите!!!Калькулятор с помощью формы

Страниц (1): [1]
 

1. Aleksei_Lymar - 16 Ноября, 2016 - 13:49:11 - перейти к сообщению
Как можно привязать checkbox к функции if, что бы при выборе нужной ячейки обсчитывалась нужная функция???

echo '<input type="checkbox" name="operation" checked value="+"/> + </Br>';
if ($operation="on")
{
$result = $a + $b;
}
echo '<input type="checkbox" name="operation" checked value="-"/> - </Br>';
if ($operation="on")
{
$result = $a - $b;
}
echo '<input type="checkbox" name="operation" checked value="*"/> / </Br>';
if ($operation="on")
{
$result = $a / $b;
}
echo '<input type="checkbox" name="operation" checked value="/"/> * </Br>';
if ($operation="on")
{
$result = $a * $b;
}
2. andrewkard - 16 Ноября, 2016 - 22:35:02 - перейти к сообщению
Aleksei_Lymar
посмотрите тут: http://phpfaq[dot]ru/newbie/na_tanke
3. nooblamer - 30 Ноября, 2016 - 22:36:27 - перейти к сообщению
Aleksei_Lymar пишет:
Как можно привязать checkbox к функции if, что бы при выборе нужной ячейки обсчитывалась нужная функция???

echo '<input type="checkbox" name="operation" checked value="+"/> + </Br>';
if ($operation="on")
{
$result = $a + $b;
}
echo '<input type="checkbox" name="operation" checked value="-"/> - </Br>';
if ($operation="on")
{
$result = $a - $b;
}
echo '<input type="checkbox" name="operation" checked value="*"/> / </Br>';
if ($operation="on")
{
$result = $a / $b;
}
echo '<input type="checkbox" name="operation" checked value="/"/> * </Br>';
if ($operation="on")
{
$result = $a * $b;
}


Улыбка

PHP:
скопировать код в буфер обмена
  1. ini_set('display_errors',0);
  2.  
  3. $a = intval($_POST['a']);
  4. $b = intval($_POST['b']);
  5.  
  6. $operation = $_POST['operation'];
  7.  
  8.  
  9. echo '<form action="/t.php" method="post">';
  10.  
  11. echo '<input type="text" name="a" value="'.$a.'"/> <input type="text" name="b" value="'.$b.'"/><br>';
  12.  
  13. echo '<input type="radio" name="operation" value="+"/> + </Br>';
  14. if ($operation == '+')
  15. {
  16. $result = $a + $b;
  17. }
  18. echo '<input type="radio" name="operation" value="-"/> - </Br>';
  19. if ($operation == '-')
  20. {
  21. $result = $a - $b;
  22. }
  23. echo '<input type="radio" name="operation" value="/"/> / </Br>';
  24. if ($operation == '/')
  25. {
  26. $result = ($a/$b);
  27. }
  28. echo '<input type="radio" name="operation" value="*"/> * </Br>';
  29. if ($operation == '*')
  30. {
  31. $result = $a * $b;
  32. }
  33.  
  34.  
  35. echo '<input type="submit" value="считать">';
  36.  
  37. echo '</form>';
  38.  
  39.  
  40. echo $result;
4. Denkill - 30 Ноября, 2016 - 22:53:04 - перейти к сообщению
PHP:
скопировать код в буфер обмена
  1. <?
  2. ini_set('display_errors',0);
  3.  
  4. $a = intval($_POST['a']);
  5. $b = intval($_POST['b']);
  6.  
  7. $operation = $_POST['operation'];
  8.  
  9. switch($operation){
  10. case "+":
  11. $result = $a + $b;
  12. break;
  13. case '-':
  14. $result = $a - $b;
  15. break;
  16. case '/':
  17. $result = ($a/$b);
  18. break;
  19. case '*':
  20. $result = $a * $b;
  21. break;
  22. }
  23. ?>
  24.  
  25. <form action="/t.php" method="post">
  26. <input type="text" name="a" value="<?=$a?>"/> <input type="text" name="b" value="<?=$b?>"/>
  27. <br>
  28. <input type="radio" name="operation" value="+"/> + </Br>
  29. <input type="radio" name="operation" value="-"/> - </Br>
  30. <input type="radio" name="operation" value="/"/> / </Br>
  31. <input type="radio" name="operation" value="*"/> * </Br>
  32. <input type="submit" value="считать">
  33. </form>
  34. Результат: <?=$result?>
  35.  


Так меньше "извращений"
5. Строитель - 30 Ноября, 2016 - 23:33:00 - перейти к сообщению
Denkill пишет:
Так меньше "извращений"
Так ещё меньше ))
PHP:
скопировать код в буфер обмена
  1. <?PHP
  2.  
  3. $result = 0;
  4. $operations = ['+', '-', '*', '/'];
  5.  
  6. $args = [
  7.     'a' => FILTER_SANITIZE_NUMBER_INT,
  8.     'b' => FILTER_SANITIZE_NUMBER_INT,
  9.     'action' => FILTER_SANITIZE_SPECIAL_CHARS
  10. ];
  11.  
  12. $arrayInput = filter_input_array(INPUT_POST, $args);
  13.  
  14. if (!empty($arrayInput)) {
  15.     foreach ($arrayInput as $var => $val) {
  16.         $$var = $val;
  17.     }
  18.  
  19.     if (in_array($action, $operations) && $b > 0) {
  20.         $create = create_function('$a, $b', 'return $a '. $action .' $b;');
  21.         $result = $create($a, $b);
  22.     }
  23. }
  24.  
  25. ?>
  26.  
  27. <form method="post">
  28.     <input size="8" type="text" name="a" />
  29.     <?PHP foreach ($operations as $opt) { ?>
  30.         <input type="radio" name="action" value="<?PHP echo $opt; ?>" />
  31.         <?PHP echo $opt; ?>
  32.     <?PHP } ?>
  33.     <input size="8" type="text" name="b" />
  34.     <input type="submit" value="=" />
  35. </form>
  36. <?PHP echo $result; ?>

 

Powered by ExBB FM 1.0 RC1