简体   繁体   English

$ _SERVER [“ REQUEST_METHOD”] ==“ POST”给出错误

[英]$_SERVER[“REQUEST_METHOD”] == “POST” giving error

I am really new to php but I have previous knowledge in asp.net . 我对php真的很陌生,但是我在asp.net上已有知识。 I have been reading and trying a lot through https://www.w3schools.com The problem is at posting forms and sending them as emails . 我一直在通过https://www.w3schools.com进行阅读和尝试,但是问题在于张贴表单并将其作为电子邮件发送。 so as a first step , I tried the following code from the following link: https://www.w3schools.com/php/php_superglobals.asp 因此,作为第一步,我尝试了以下链接中的以下代码: https : //www.w3schools.com/php/php_superglobals.asp

   <html>
<body>

<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
  Name: <input type="text" name="fname">
  <input type="submit">
</form>

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // collect value of input field
    $name = $_POST['fname'];
    if (empty($name)) {
        echo "Name is empty";
    } else {
        echo $name;
    }
}
?>

I copied it into phpDesigner8 but I got the following error when I press on run : Notice : Undefined index: REQUEST_METHOD in c:\\Users\\User\\AppData\\Local\\Temp\\Untitled 1 on line 10 我将其复制到phpDesigner8中,但是在运行时按以下错误:注意:未定义索引:REQUEST_METHOD在c:\\ Users \\ User \\ AppData \\ Local \\ Temp \\ Untitled 1 on line 10

can anyone please help me and explain to me what is wrong? 谁能帮我解释一下哪里出了问题? Thank you much in advance ! 提前非常感谢您!

Updated version: 更新后的版本:

<html>
<body>

<form method="post" action="">
  Name: <input type="text" name="fname">

  <input type="submit">
</form>

<?php



if(isset($_POST['fname'])) {
    // collect value of input field
    $name = $_POST['fname'];
    if (empty($name)) {
        echo "Name is empty";
    } else {
        echo $name;
    }
}


?>

now there is no more error but nothing is being output to the screen with echo 现在不再有错误,但没有任何内容通过回显输出到屏幕

Try using this: 尝试使用此:

$var = $GLOBALS["_SERVER"];
print_r($var);

Gotten from an answer on stackoverflow , 关于stackoverflow的答案中得到

$request_method = strtoupper(getenv('REQUEST_METHOD'));
$http_methods = array('GET', 'POST', 'PUT', 'DELETE', 'HEAD', 'OPTIONS');

    if (in_array($request_method, $http_methods)) {
    //this would only allow the above methods.
    if ($request_method == 'POST') {
        //proceed
       $name = $_POST['fname'];
    if (empty($name)) {
        echo "Name is empty";
    } else {
        echo $name;
    }
    }
} else {
    die('invalid request');
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM