where is the problem? This simple html/PHP form works fine when tested locally but fails on the live server
P粉060528326
P粉060528326 2023-09-05 17:03:39
0
1
442
<p>I want to add a "Send a Message" form to my existing website. I found this tutorial: </p> <p>PHP Contact Form</p> <p>I reviewed the code from the tutorial, learned how it worked, and then programmed it. I got it running and tested it all on my PC and it works perfectly. </p> <p>I then put it on the server as part of an existing website. but failed. Clicking the submit button clears the screen and the form disappears. No error message. Nothing at all. </p> <p>Both XAMPP and the actual server are running PHP version 8.1.12. </p> <p>It fails on this line of PHP code: header('Location: contact.php', true, 303);</p> <p>I spent a lot of time debugging this problem, learning more about PHP, following advice found on Stack Overflow, and reading the PHP documentation on the function header(). I also took two LinkedIn Learning PHP courses. But I can't figure out where the problem is. </p> <p>I then reduced the problem to this simple code, making it as simple as possible with just a form containing a submit button. It has the same problem as the full form. </p> <p>This is my test code:</p> <pre class="brush:php;toolbar:false;"><!doctype html> <html lang="en"> <head> <!-- Required meta tags --> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> <title>Contact</title> </head> <?php session_start(); $request_method = strtoupper($_SERVER['REQUEST_METHOD']); if ($request_method === 'GET'){ ?><form action="for_stack_overflow.php" method="post"> <button type="submit">Send Message</button> </form> <?php } elseif ($request_method === 'POST'){ header('Location: for_stack_overflow.php', true, 303); exit; } ?></html></pre> <p>This simple html/php code has the same problem as my full form: after clicking the submit button, the screen is cleared, leaving only a form button. The form doesn't work. </p> <p>I intentionally removed all indentation because I learned that a common problem is that invisible whitespace characters cause the PHP function header() to fail. </p> <p>This program (and my complete form) each: </p> <ol> <li>Works perfectly when tested locally on my PC;</li> <li>It fails when running on my website hosting account on a real server. </li> </ol> <p>I have tried my best to resolve this issue. </p> <p>What went wrong? What could possibly go wrong? Is there something I don't understand? </p>
P粉060528326
P粉060528326

reply all(1)
P粉055726146

Move header() and session_start() calls before HTML output.

<?php session_start();
...
?>
<!doctype html>
...

Reference:https://www.php.net/manual/en/function.header.php

Reference:https://www.php.net/manual/en/function.session-start.php

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!