Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // mengecek pengiriman form
- if (isset($_POST['opr'])) {
- // echo "<pre>"; print_r($_POST); echo "</pre>";
- extract($_POST);
- if (is_numeric($bil1) && is_numeric($bil2)) {
- $hasil = $bil1." ".$opr." ".$bil2. " = ";
- switch($opr) {
- case "+": $hasil .= $bil1+$bil2; break;
- case "-": $hasil .= $bil1-$bil2; break;
- case "*": $hasil .= $bil1*$bil2; break;
- case "/": $hasil .= $bil1/$bil2; break;
- case "%": $hasil .= $bil1%$bil2; break;
- case "**": $hasil .= $bil1**$bil2; break;
- default: echo 'Operator salah!';
- }
- } else {
- $error = "Operand yang diinputkan bukan bilangan!";
- }
- }
- ?>
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
- <title>Document</title>
- </head>
- <body>
- <form action="<?= $_SERVER['PHP_SELF'] ?>" method="post">
- <!-- <label for="bil1">Bilangan 1</label> -->
- <input placeholder="bilangan 1" type="text" size="8" name="bil1">
- <select name="opr">
- <option value="+">+</option>
- <option value="-">-</option>
- <option value="*">*</option>
- <option value="/">/</option>
- <option value="%">%</option>
- <option value="**">**</option>
- </select>
- <!-- <label for="bil2">Bilangan 2</label> -->
- <input placeholder="bilangan 2" type="text" size="8" name="bil2">
- <input type="submit" value="Hitung">
- </form>
- <?php if (isset($hasil)) { ?>
- <div class="alert alert-success" role="alert">
- <?= $hasil; ?>
- </div>
- <?php } ?>
- <?php if (isset($error)) { ?>
- <div class="alert alert-danger" role="alert">
- <?= $error; ?>
- </div>
- <?php } ?>
- <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz" crossorigin="anonymous"></script>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement