5

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';
}
?>
14
  • try print_r($_POST), and post it in your question Jan 30, 2014 at 8:04
  • If this is what you were looking for I will add it in my question. Parse error: syntax error, unexpected T_VARIABLE in /hermes/waloraweb097/b516/moo.bondsolutionsnjcom/k9minecraft/scripts/adduser.php on line 3
    – Kyle
    Jan 30, 2014 at 8:05
  • you have syntax error, can you do it without syntax error ??? Jan 30, 2014 at 8:06
  • Not sure why this line is a syntax error though. $username = $_POST['user'];
    – Kyle
    Jan 30, 2014 at 8:08
  • put it before the first line. print_r($_POST);die; Jan 30, 2014 at 8:09

5 Answers 5

10

The issue is that all of your inputs have id but not name. The id are used by JavaScript. The name are used for sending form data.

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>
2
  • Names have beem added and no different result sadly.
    – Kyle
    Jan 30, 2014 at 8:14
  • @Kyle did you ever figure this out? i'm having the same problem now. it worked the very first time, now it's posting a NULL value :/
    – oldboy
    May 20, 2017 at 8:44
2

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

 }
10
  • 1
    this just remove a notice error if post is empty, certainly not solving the problem Jan 30, 2014 at 8:07
  • Gave this a shot, but no it didn't fix it.
    – Kyle
    Jan 30, 2014 at 8:07
  • what are you getting now
    – M I
    Jan 30, 2014 at 8:09
  • Array ( ) using the code suggested in the comments of my question. Using just your code, the results were no different than the original problem.
    – Kyle
    Jan 30, 2014 at 8:10
  • 1
    first always try to initialize the variables like ($username) on the top. secondly always check for variable being empty like empty($username) or !$username.
    – M I
    Jan 30, 2014 at 8:20
2

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.

2
  • Ok so I tried this line and it didn't print anything out. echo isset($_POST['user']);
    – Kyle
    Jan 30, 2014 at 8:04
  • seems like you figured it out. At least it is working for me, when i post the form to the script on your server. I'm getting: Array ( [user] => chris ) chrisusername is not null
    – Chris
    Jan 30, 2014 at 8:13
0

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.

1
  • yeah but that's not what i meant. i meant you can not detect if it was empty or it was not set using == without a php notice.
    – Prasanth
    Jan 30, 2014 at 8:11
0

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

?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.