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

PHP表单提交仅刷新无输出问题求助(附代码与表结构)

Fixing PHP Form Refresh Issue Without Displaying Content

Hey there, let's walk through fixing your issue—there are a few straightforward mistakes in your code that are causing the page to refresh without showing your $str content. Let's break them down and fix each one:

1. Form Method Typo (Critical!)

Look at your form code:

<form action="case_status.php" action="POST">

You've got two action attributes here—this is invalid HTML. The second one should be method="POST" instead. Forms default to GET requests, so your $_POST checks were never triggering, which is why the page just refreshes. Fix it to:

<form action="case_status.php" method="POST">

2. Submit Button Name Mismatch

In your PHP, you're checking for:

if(isset($_POST['submit button'])){

But your submit button's name attribute is submit_button (with an underscore, not a space). This condition will never be true, so none of your form processing code runs. Correct it to:

if(isset($_POST['submit_button'])){

3. Undefined Variable in Your Output String

In your $str HTML, you reference $next_hearing, but earlier in your code you assigned that value to $date:

$date=$row['next_hearing'];

Either rename the variable to $next_hearing when fetching the row, or use $date in your output—otherwise, that field will be empty or throw an undefined variable notice.

4. Add SQL Error Handling (For Debugging)

You mentioned you're unfamiliar with SQL error handling, but this is key to diagnosing why queries might fail. After running your query, add a check to catch and display errors:

$query=mysqli_query($con,"SELECT * FROM case_details WHERE cs_number='$case_no'");
if (!$query) {
    die("SQL Query Failed: " . mysqli_error($con));
}

This will tell you immediately if there's a problem with your query (like a typo in a column name or table name).

5. Fix SQL Injection Vulnerability

Directly inserting user input into your SQL query is a huge security risk—attackers can exploit this to manipulate your database. Use prepared statements instead, which are safer:

// Prepare the query with a placeholder
$stmt = mysqli_prepare($con, "SELECT * FROM case_details WHERE cs_number = ?");
// Bind the integer parameter (since cs_number is an int)
mysqli_stmt_bind_param($stmt, "i", $case_no);
// Execute the statement
mysqli_stmt_execute($stmt);
// Get the result set
$result = mysqli_stmt_get_result($stmt);
// Fetch the row
$row = mysqli_fetch_array($result);

6. Start the Session

You're using $_SESSION['cs_no'] but haven't started the session at the top of your script. Add session_start() before including your config file, otherwise session variables won't work:

<?php
session_start();
require 'config/config.php';
// Rest of your code...

Bonus: Improve Output Placement

Right now you only echo $str inside the form submission condition. If you move the echo $str; outside the condition (above or below the form), you'll see the results (or error messages) consistently after submission, which is better for user experience.

Corrected Full Example (Key Sections)

<?php
session_start();
require 'config/config.php'; 
$case_no = ""; 
$str = ""; // Initialize to avoid undefined variable notices

if(isset($_POST['submit_button'])){ 
    $case_no = strip_tags($_POST['cs_no']); 
    $_SESSION['cs_no'] = $case_no; 

    // Use prepared statement for safety
    $stmt = mysqli_prepare($con, "SELECT * FROM case_details WHERE cs_number = ?");
    mysqli_stmt_bind_param($stmt, "i", $case_no);
    mysqli_stmt_execute($stmt);
    $result = mysqli_stmt_get_result($stmt);

    if (!$result) {
        $str = "<p>Error: " . mysqli_error($con) . "</p>";
    } elseif(mysqli_num_rows($result) === 0) {
        $str = "<p>No case found with that number.</p>";
    } else {
        $row = mysqli_fetch_array($result); 
        $filed_by = $row['filed_by']; 
        $defender = $row['defender']; 
        $district = $row['district']; 
        $status = $row['status']; 
        $details = $row['details']; 
        $next_hearing = $row['next_hearing']; 
        $updates = $row['updates']; 

        $str = "<div class='case_body'> 
                <table> 
                    <tr> <td>case no: $case_no</td> </tr> 
                    <tr> <td>filed by: $filed_by</td> </tr> 
                    <tr> <td>defender: $defender</td> </tr> 
                    <tr> <td>district: $district</td> </tr> 
                    <tr> <td>status: $status</td> </tr> 
                    <tr> <td>case details: $details</td> </tr> 
                    <tr> <td>next hearing: $next_hearing</td> </tr> 
                    <tr> <td>updates: $updates</td> </tr> 
                </table> 
            </div>"; 
    }
} 
?> 
<!DOCTYPE html> 
<html> 
<head> <title>Case Status Lookup</title> </head> 
<body> 
    <h3>Enter your case number below to view details</h3> 
    <?php echo $str; ?>
    <form action="case_status.php" method="POST"> 
        <input type="text" name="cs_no" placeholder="Enter case number" required> 
        <input type="submit" name="submit_button" value="Check Status"> 
    </form> 
</body>
</html>

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.14 06:40:22