简体   繁体   English

if($_SERVER['REQUEST_METHOD'] == 'POST') 不起作用

[英]if($_SERVER['REQUEST_METHOD'] == 'POST') don't work

I can't explain it, because I'm studying the book learning PHP and I went to test this example and it doesn't work at all.我无法解释它,因为我正在研究学习PHP的书,我去测试这个例子,它根本不起作用。

<?php
if($_SERVER['REQUEST_METHOD'] == 'POST') {
    print "hello, ". $_POST['my_name'];
} 
else {
    print<<<_HTML_
         <form method="post" action="$_SERVER[PHP_SELF]">
         Your name: <input type="text" name="my name">
         <br>
         <input type="submit" value="Say Hello">
        </form>
        _HTML_;
}
?>

As said in the comments your HEREDOC (the <<<HTML and HTML; parts) is incorrectly formed.正如评论中所说,您的 HEREDOC( <<<HTMLHTML;部分)的格式不正确。 Below is the corrected one so you can see the difference.以下是更正后的,因此您可以看到差异。

<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    print "hello, " . $_POST['my_name'];
} else {
    print<<<HTML
         <form method="post" action="$_SERVER[PHP_SELF]">
         Your name: <input type="text" name="my name">
         <br>
         <input type="submit" value="Say Hello">
        </form>
HTML;
}

Below is a screen for a working example on replit.com as the OP is using.以下是 OP 正在使用的 replit.com 上的工作示例屏幕。

OP 用于测试的 Repl.it 上工作示例的图像

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 if($ _SERVER [&#39;REQUEST_METHOD&#39;] ===&#39;POST&#39;)似乎不起作用 - if ($_SERVER['REQUEST_METHOD'] === 'POST') doesn't seem to work isset($ _ POST [&#39;submit&#39;])不起作用,但是$ _SERVER [“ REQUEST_METHOD”] ==“ POST”起作用 - isset($_POST['submit']) doesn't work but $_SERVER[“REQUEST_METHOD”] == “POST” does $_SERVER['REQUEST_METHOD'] 在服务器上不起作用 - $_SERVER['REQUEST_METHOD'] does not work on server $ _SERVER [&#39;REQUEST_METHOD&#39;]以及同时POST和GET - $_SERVER['REQUEST_METHOD'] and simultaneous POST and GET 检查$ _SERVER [&#39;REQUEST_METHOD&#39;] ==&#39;POST&#39;的原因? - Reason for checking if $_SERVER['REQUEST_METHOD'] == 'POST'? $ _SERVER [“ REQUEST_METHOD”] ==“ POST”给出错误 - $_SERVER[“REQUEST_METHOD”] == “POST” giving error $_SERVER[&quot;REQUEST_METHOD&quot;] == &quot;POST&quot; 中未定义的索引 - Undefined index in $_SERVER["REQUEST_METHOD"] == "POST" if($ _ SERVER [&#39;REQUEST_METHOD&#39;] ==“ POST”)遇到麻烦 - trouble with if($_SERVER['REQUEST_METHOD'] == “POST”) WordPress和处理if(($ _SERVER [“ REQUEST_METHOD”] ==“ POST”) - Wordpress and handling if (($_SERVER[“REQUEST_METHOD”] == “POST”) if($ _ SERVER [&#39;REQUEST_METHOD&#39;] ==&#39;POST&#39;)和android post请求不起作用 - if($_SERVER['REQUEST_METHOD']=='POST') and android post request not working
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM