如何从HTML向PHP传递表单值并实现贷款利息、总额及月供计算?
Here's the completed PHP code that implements your loan calculation logic, along with key improvements to handle data processing and formatting:
<!doctype html> <html lang="en"> <head><title>Registration Form</title> <style> .Cn{ text-align:center; color:#1e90ff; font-family:Arial, Helvetica, sans-serif; } .add{ color:black; font-family:Arial, Helvetica, sans-serif; } .h1{ text-align:center; color:black; font-family:Arial, Helvetica, sans-serif; } table, th, td { border: 1px solid black; align:center; } table.center{ margin-left:auto; margin-right:auto; } table { border-collapse: collapse; } </style> </head> <body> <h2 class="Cn">Loan</h2> <center class="add">blah City<br> Mobile Number: 12345678900 <br> Telephone:(08)9999999 </center> <hr style="Width:100%;color:#dc143c"> <h1 class="h1">Loan Information</h1> <?php // Process form data only if form was submitted if ($_SERVER['REQUEST_METHOD'] === 'POST') { // Extract and clean loan amount from radio button value $amount_str = $_POST['r1'] ?? ''; $loan_amount = (float)str_replace(['Php ', ','], '', $amount_str); // Extract number of months from payment terms $terms_str = $_POST['r2'] ?? ''; $months = (int)trim(str_replace('months', '', $terms_str)); // Determine interest rate based on cooperative officer checkbox $is_cooperative = isset($_POST['chckbx']); $interest_rate = $is_cooperative ? 0.05 : 0.10; // Calculate loan values $interest = $loan_amount * $interest_rate; $total_amount = $loan_amount + $interest; $monthly_dues = $months > 0 ? $total_amount / $months : 0; } ?> <table class="center"> <tr><td>Date</td> <td><?php echo date('F d, Y'); ?></td> </tr> <tr> <td>Name</td> <td><?php echo $_POST["name"] ?? '';?></td> </tr> <tr> <td>Address</td> <td><?php echo $_POST["address"] ?? '';?></td> </tr> <tr> <td>Contact</td> <td><?php echo $_POST["contact"] ?? ''; ?></td> </tr> <tr> <td>Amount</td> <td><?php echo $_POST['r1'] ?? ''; ?></td> </tr> <tr> <td>Terms of Payment</td> <td><?php echo $_POST['r2'] ?? ''; ?></td> </tr> <tr> <td>Interest (<?php echo isset($is_cooperative) ? ($is_cooperative ? '5%' : '10%') : ''; ?>)</td> <td><?php echo isset($interest) ? 'Php ' . number_format($interest, 2) : ''; ?></td> </tr> <tr> <td>Total Amount</td> <td><?php echo isset($total_amount) ? 'Php ' . number_format($total_amount, 2) : ''; ?></td> </tr> <tr> <td>Monthly Dues</td> <td><?php echo isset($monthly_dues) ? 'Php ' . number_format($monthly_dues, 2) : ''; ?></td> </tr> </table> <center> <input type="button" value="Back" onclick="window.history.back();"> </center> </body> </html>
Key Changes Explained:
- Data Cleaning: Converted the loan amount string (like "Php 5,000") to a numeric value by removing non-digit characters, which is necessary for calculations.
- Interest Rate Logic: Checked if the "Cooperative Officer" checkbox is selected to apply either 5% or 10% interest rate.
- Calculations: Computed interest, total loan amount, and monthly dues using the cleaned numeric values.
- Formatting: Used
number_format()to display currency values with 2 decimal places, matching the input style. - Error Prevention: Added null coalescing operators (
??) to handle cases where form fields might not be submitted, and wrapped calculations in a POST request check. - Back Button: Replaced the submit button with a button that uses
window.history.back()to navigate to the previous form page without resubmitting data.
内容的提问来源于stack exchange,提问作者rikardo14
相关产品推荐
相关产品推荐

