HTML Form POST is null

2019-06-26 07:05发布

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.

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
print_r($_POST);
    if(isset($_POST['user'])){
        $username = $_POST['user'];
        echo $username;
        echo 'username is not null';
        }
?>

5条回答
smile是对你的礼貌
2楼-- · 2019-06-26 07:30

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

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;
   }

 }
查看更多
劫难
3楼-- · 2019-06-26 07:34

This is how people usually do it:

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

Note: == null will not work with empty string. see here.

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

查看更多
混吃等死
4楼-- · 2019-06-26 07:44

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;
     }
    }

?>
查看更多
Juvenile、少年°
5楼-- · 2019-06-26 07:47

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

isset($_POST['user'])

I think $username==null will be true even if $username really is equal to an empty string.

查看更多
神经病院院长
6楼-- · 2019-06-26 07:48

The issue is that all of your inputs have ids but not names. The ids are used by JavaScript. The names are used for posting.

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>
查看更多
登录 后发表回答