php HTML 表单 POST 为空

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21450748/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:09:41  来源:igfitidea点击:

HTML Form POST is null

phphtmlformspost

提问by Kyle

so I'm trying to simply send one field of data from a form to a php file. Below is my form in a table. I also posted my php code. It keeps returning that $username is null. Ive tried post/get and it doesn't seem to matter.

所以我试图简单地将一个数据字段从表单发送到一个 php 文件。下面是我在表格中的表格。我还发布了我的php代码。它不断返回 $username 为空。我试过 post/get ,它似乎无关紧要。

HTML:

HTML:

<form action='http://k9minecraft.tk/scripts/adduser.php' method='POST'>
    <table>
        <tr>
            <td>First Name:</td>
            <td><input type='text' id='first'></td>
        </tr>
        <tr>
            <td>Last Name:</td>
            <td><input type='text' id='last'></td>
        </tr>
        <tr>
            <td>Email:</td>
            <td><input type='text' id='email'></td>
        </tr>
        <tr>
            <td>Minecraft Name:</td>
            <td><input type='text' name='user'></td>
        </tr>
        <tr>
            <td><input type='submit' value='Send'></td>
            <td><input type='reset' value='Reset'></td>
        </tr>
    </table>
</form>

PHP:

PHP:

<?php
print_r($_POST);
if (isset($_POST['user'])) {
    $username = $_POST['user'];
    echo $username;
    echo 'username is not null';
}
?>

采纳答案by Jason W

This code is working. You need to add some condition, that checks, if $usernameis posted or not.

此代码正在工作。您需要添加一些条件,以检查是否$username已发布。

Something like that:

类似的东西:

if(count($_POST)){
    $username ='';
    if(isset($_POST['user'])){
        $username = $_POST['user'];
    if ($username==null || !$username)
         echo 'username is null';
     echo strlen($username);
     echo $username;
   }

 }

回答by virmaior

The issue is that all of your inputs have idbut not name. The idare used by JavaScript. The nameare used for sending form data.

问题是您的所有输入都有id但没有name. 在id由JavaScript使用。所述name用于发送的表单数据。

Change it to be like this:

改成这样:

<form action='http://k9minecraft.tk/scripts/adduser.php' method='POST'>
  <table>
    <tr>
      <td>First Name:</td>
      <td><input type='text' name='first' id='first'></td>
    </tr>
    <tr>
      <td>Last Name:</td>
      <td><input type='text' name='last' id='last'></td>
    </tr>
    <tr>
      <td>Email:</td>
      <td><input type='text' name='email' id='email'></td>
    </tr>
    <tr>
      <td>Minecraft Name:</td>
      <td><input type='text' name='user'></td>
    </tr>
    <tr>
      <td><input type='submit' name='Send' value='Send'></td>
      <td><input type='reset' name='Rest' value='Reset'></td>
    </tr>
  </table>
</form>

回答by Chris

Try this to find out if the field is posted by the formular:

试试这个以找出该字段是否由公式发布:

isset($_POST['user'])

I think $username==nullwill be true even if $usernamereally is equal to an empty string.

我认为$username==null即使$username真的等于空字符串也会为真。

回答by Prasanth

This is how people usually do it:

人们通常是这样做的:

if(isset($_POST['user']) && !empty($_POST['user'])) {
    $user = $_POST['user'];
}

Note: == nullwill not work with empty string. see here.

注意:== null不适用于空字符串。看到这里

You also need to add a name attribute for other input fields of yours.

您还需要为您的其他输入字段添加名称属性。

回答by Akihiko Arihyoshi

try using this

尝试使用这个

<?php
    if(isset($_POST['submit'])){
     $msg = "";
     /* Validate post */
     if(isset($_POST['user'])==""){
      $msg .= "username is null";
     }
    /*End Validate*/
     if($msg==""){
      $user = $_POST['user'];
     }else{
       echo $msg;
     }
    }

?>