PHP表单提交后无法输出输入数据问题求助
Hey there! Let's work through why your form isn't showing the data you submit when you click the submit button. I noticed a few critical issues in your PHP code that are causing this problem:
1. Function Definition Order is Wrong
You’re calling the check_input() function at the very top of your PHP file, but you don’t define that function until the end of the file. PHP runs code from top to bottom—so when it hits $yourname = check_input($_POST['yourname']);, it has no idea what check_input is yet! This throws a fatal error that stops the rest of your code from running, which is why you don’t see any data.
Fix: Move the check_input function to the start of your PHP file, before you try to use it.
2. No Check for Existing POST Data
If someone visits abiform.php directly (without submitting the form), the $_POST array won’t have any of your form’s keys. This will trigger "Undefined index" warnings, which can break your page’s output. We need to check if those keys exist before trying to use them.
3. Enable Error Reporting (For Debugging)
Turning on PHP’s error reporting during development will help you spot issues like these quickly. Add a few lines at the top of your PHP file to enable this.
Updated Working PHP Code
<?php // Turn on error reporting (only for development—disable in production!) error_reporting(E_ALL); ini_set('display_errors', 1); // Define the sanitization function FIRST function check_input($data) { $data = trim($data); $data = stripslashes($data); $data = htmlspecialchars($data); return $data; } // Check if POST data exists before sanitizing $yourname = isset($_POST['yourname']) ? check_input($_POST['yourname']) : ''; $email = isset($_POST['email']) ? check_input($_POST['email']) : ''; $urgency = isset($_POST['urgency']) ? check_input($_POST['urgency']) : ''; $existingissue = isset($_POST['existingissue']) ? check_input($_POST['existingissue']) : ''; $hours = isset($_POST['hours']) ? check_input($_POST['hours']) : ''; $description = isset($_POST['description']) ? check_input($_POST['description']) : ''; $outcome = isset($_POST['outcome']) ? check_input($_POST['outcome']) : ''; ?> <html> <body> Company Name: <?php echo $yourname; ?><br /> <br /> Company e-mail: <?php echo $email; ?><br /> <br /> Urgency: <?php echo $urgency; ?><br /> <br /> Existing Feature or Issue? <?php echo $existingissue; ?><br /> <br /> Max Hours Willing to Spend on Issue: <?php echo $hours; ?><br /> <br /> Issue Description:<br /> <?php echo $description; ?> <br /> Expected Outcome:<br /> <?php echo $outcome; ?> </body> </html>
Extra Tips to Improve Your Form
- Your HTML form’s
nameattributes are all set correctly—great job there! - Add
requiredto mandatory fields to enforce user input on the front end, like:<input type="text" name="yourname" required /> - Use
type="email"for the email field to get basic front-end validation:<input type="email" name="email" required />
After making these changes, your form should display the submitted data correctly. If you still run into issues, check Apache’s error log (usually located in apache/logs/error.log) for any additional error messages that might point to other problems.
内容的提问来源于stack exchange,提问作者K. Man

