Hướng dẫn php calculator using radio buttons - máy tính php sử dụng các nút radio

0

Mới!Lưu câu hỏi hoặc câu trả lời và sắp xếp nội dung yêu thích của bạn.Tìm hiểu thêm.
Learn more.

Tôi đã thực hiện một máy tính đơn giản. Nhưng sự cố không phải là các nút radio không hoạt động đúng để chọn thao tác sẽ được thực hiện mã của tôi cho đầu vào như sau:

<form action="output.php" method="post" class="form"> <label for="">Value 1</label> <input type="text" name="value1" placeholder="Enter 1st value"> <br> <label for="">Value 2</label> <input type="text" name="value2" placeholder="Enter 2nd value"> <h2>Select Operator </h2> <input type="radio" name="addition" value="add">+ <br> <input type="radio" name="subtraction" value="sub">- <br> <input type="radio" name="multiplication" value="mul">* <br> <input type="radio" name="division" value="div">/ <br> <input type="submit" class="btn btn-success" value="Show Result"> </form>

và mã PHP đầu ra như sau:

<?php $value_1=$_POST['value1']; $value_2=$_POST['value2']; if(isset($_POST['submit'])) { if($_POST['operation'] == add) { echo "$value_1 + $value_2"; }else if($_POST['operation'] == sub){ echo "$value_1 - $value_2"; }else if($_POST['operation'] == mul){ echo "$value_1 * $value_2"; }else if($_POST['operation'] == div){ echo "$value_1 / $value_2"; } } ?>

Hỏi ngày 3 tháng 8 năm 2021 lúc 6:53Aug 3, 2021 at 6:53

3

Các nút radio cần chia sẻ cùng một tên, chỉ giá trị nên thay đổi.

label { display: block; }<label><input type="radio" name="operation" value="add">+</label> <label><input type="radio" name="operation" value="sub">-</label> <label><input type="radio" name="operation" value="mul">*</label> <label><input type="radio" name="operation" value="div">/</label>

Đã trả lời ngày 3 tháng 8 năm 2021 lúc 6:55Aug 3, 2021 at 6:55

Camcamcam

3.0291 Huy hiệu vàng10 Huy hiệu bạc12 Huy hiệu đồng1 gold badge10 silver badges12 bronze badges

1

Nút submit không có tên - vì vậy nó sẽ không xuất hiện trong mảng bài và logic sẽ không bao giờ được xử lý.Thay vì kiểm tra sự hiện diện (hoặc không) của một nút trong mảng bài đăng, bạn thực sự nên kiểm tra các yếu tố biểu mẫu thực tế mà bạn sẽ sử dụng trong các tính toán của mình.

<!DOCTYPE html> <html lang='en'> <head> <meta charset='utf-8' /> <title>simple calculator</title> </head> <body> <form method="post" class="form"> <label>Value 1 <input type="text" name="value1" placeholder="Enter 1st value"></label> <label>Value 2<input type="text" name="value2" placeholder="Enter 2nd value"></label> <h2>Select Operator </h2> <input type="radio" name="operator" value="add">+ <br> <input type="radio" name="operator" value="sub">- <br> <input type="radio" name="operator" value="mul">* <br> <input type="radio" name="operator" value="div">/ <br> <input type="submit" class="btn btn-success" value="Show Result"> </form> <?php /* Ensure that ALL post fields are set */ if( isset( $_POST['value1'], $_POST['value2'], $_POST['operator'] )) { /* Assign POST values as variables. */ $value_1=(int)$_POST['value1']; $value_2=(int)$_POST['value2']; $operator=$_POST['operator']; /* Perform the relevant calculation and assign the correct symbol for display */ switch( $operator ){ case 'add':$symbol='+';$res=$value_1 + $value_2;break; case 'sub':$symbol='-';$res=$value_1 - $value_2;break; case 'mul':$symbol='*';$res=$value_1 * $value_2;break; case 'div':$symbol='/';$res=$value_1 / $value_2;break; } /* print the sum and result */ printf( '%d %s %s=%d', $value_1, $symbol, $value_2, $res ); } ?> </body> </html>

Đã trả lời ngày 3 tháng 8 năm 2021 lúc 7:21Aug 3, 2021 at 7:21

3

Chủ đề