fork download
  1. <?php
  2. $discount = 0;
  3. $final_price = 0;
  4.  
  5. if ($_SERVER["REQUEST_METHOD"] == "POST") {
  6. $price = floatval($_POST['price']);
  7.  
  8. if ($price >= 5000 && $price <= 10000) {
  9. $discount = 3;
  10. } elseif ($price > 10000 && $price <= 20000) {
  11. $discount = 5;
  12. } elseif ($price > 20000) {
  13. $discount = 10;
  14. }
  15.  
  16. $discount_amount = ($price * $discount) / 100;
  17. $final_price = $price - $discount_amount;
  18. }
  19. ?>
  20.  
  21. <!DOCTYPE html>
  22. <html>
  23. <head>
  24. <title>คำนวณส่วนลดสินค้า</title>
  25. </head>
  26. <body>
  27. <h2>โปรแกรมคำนวณส่วนลดสินค้า</h2>
  28. <form method="post" action="">
  29. <label for="price">ราคาสินค้า (บาท):</label>
  30. <input type="number" name="price" id="price" required>
  31. <button type="submit">คำนวณ</button>
  32. </form>
  33.  
  34. <?php if ($_SERVER["REQUEST_METHOD"] == "POST") : ?>
  35. <h3>ผลลัพธ์:</h3>
  36. <p>ราคาสินค้า: <?php echo number_format($price, 2); ?> บาท</p>
  37. <p>ส่วนลด: <?php echo $discount; ?>% (<?php echo number_format($discount_amount, 2); ?> บาท)</p>
  38. <p>ราคาสุทธิหลังหักส่วนลด: <?php echo number_format($final_price, 2); ?> บาท</p>
  39. <?php endif; ?>
  40. </body>
  41. </html>
  42.  
Success #stdin #stdout #stderr 0.03s 25892KB
stdin
Standard input is empty
stdout
<!DOCTYPE html>
<html>
<head>
    <title>คำนวณส่วนลดสินค้า</title>
</head>
<body>
    <h2>โปรแกรมคำนวณส่วนลดสินค้า</h2>
    <form method="post" action="">
        <label for="price">ราคาสินค้า (บาท):</label>
        <input type="number" name="price" id="price" required>
        <button type="submit">คำนวณ</button>
    </form>
    
    </body>
</html>
stderr
PHP Notice:  Undefined index: REQUEST_METHOD in /home/oiTXN8/prog.php on line 5
PHP Notice:  Undefined index: REQUEST_METHOD in /home/oiTXN8/prog.php on line 34