You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP表单代码无法执行,页面空白问题排查求助

Fixing Your Blank Fuel Calculator Page

Got it, let's figure out why your fuel calculator is showing a blank page and failing to calculate. The core issue is straightforward—you defined your calculation function in PHP but never actually ran it! Let's walk through the fixes step by step, plus add safeguards to prevent other common issues.

1. The Main Problem: You Didn't Call Your Function

In your get_info.php file, you created the spalanie() function, but there's no line to execute it. Without calling the function, the code does nothing, resulting in a blank page.

Corrected Basic PHP Code

<html>
<body>
<?php
function spalanie(){
    $sp = $_GET["sp"];
    $km = $_GET["km"];
    $wynik = $sp * 100 / $km;
    echo "Spalanie na 100 km: " . $wynik . " l";
}

// This line is critical—call the function to run the calculation!
spalanie();
?>
</body>
</html>

2. Add Error Handling to Avoid Blank Pages

Even with the function called, invalid inputs (like empty fields, non-numeric values, or a km value of 0) can cause errors that might result in a blank page (especially if your server hides errors). Let's add checks to handle these cases:

PHP Code with Error Checking

<html>
<body>
<?php
// Enable error reporting for debugging (remove this in production if needed)
error_reporting(E_ALL);
ini_set('display_errors', 1);

function spalanie(){
    // Check if both form fields were submitted
    if(!isset($_GET["sp"]) || !isset($_GET["km"])){
        echo "Proszę wypełnić wszystkie pola!";
        return;
    }

    $sp = $_GET["sp"];
    $km = $_GET["km"];

    // Validate inputs are numeric and km isn't zero
    if(!is_numeric($sp) || !is_numeric($km) || $km == 0){
        echo "Proszę podać prawidłowe wartości:<br>
        - Spalone paliwo i odległość muszą być liczbami<br>
        - Odległość nie może być zerem!";
        return;
    }

    $wynik = $sp * 100 / $km;
    // Format result to 2 decimal places for readability
    echo "Spalanie na 100 km: " . number_format($wynik, 2) . " l";
}

spalanie();
?>
</body>
</html>

3. Improve the HTML Form for Better User Input

Your HTML form uses text inputs, which can lead to non-numeric entries. Let's update it to use number inputs and add validation to guide users:

Updated HTML Form

<html>
<head>
<meta charset="utf-8">
<title>Kalkulator spalania paliwa</title>
</head>
<body align="center">
<h1>Kalkulator spalania paliwa.</h1><br>
<form action="get_info.php" method="get">
Podaj spalone paliwo: <input type="number" name="sp" step="0.1" required> l<br>
Podaj przejechaną odległość: <input type="number" name="km" step="0.1" min="0.1" required> km<br>
<input type="submit" value="Oblicz spalanie">
</form>
</body>
</html>

Key improvements here:

  • type="number": Ensures users can only enter numeric values
  • required: Prevents submitting empty fields
  • step="0.1": Allows decimal values (e.g., 5.5 liters)
  • min="0.1": Stops users from entering 0 for distance (which would cause division by zero)

Why This Works

  • Calling the function executes your calculation logic instead of leaving it unused
  • Error checking catches invalid inputs and shows user-friendly messages instead of blank pages
  • The improved form reduces invalid submissions before they even reach the PHP code

内容的提问来源于stack exchange,提问作者Deezor

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 07:30:39