0

I'm learning MySQL and PHP and got a problem with the input of the form. So I wrote a small test code, but it still cannot work. My PHP version is 5.6.

The code:

<html>
<body>
<form action ="2.php" method ="post">
    Name: <input type="text" name="username" />
    <input type ="submit" value="ok" />
</form>
</body>
</html>

and

<html>
<?php
if(isset($_POST['username'])){
    $user=$_POST['username'];
    echo $user;
    echo " is your name";
}
else{
    $user=null;
    echo "error";
}
?>
</html>

The output of the project is always error, can't output the input before.

I tried single quote and double quote for username, both can't work.

I also tried to set always_populate_raw_post_data in php.ini to 0, -1, 1, all can't work.

I don't know where the problem is, though it might be very silly.

7
  • 1
    $user=isset($_POST['username']); what you get here is a boolean, not the value from the form, btw. Commented Nov 1, 2016 at 14:33
  • "The output of the project is always error" So what's the error? Commented Nov 1, 2016 at 14:34
  • $user = $_POST['username']; echo $user.' is your name'; Commented Nov 1, 2016 at 14:35
  • Change $user=isset($_POST['username']); to $user=$_POST_['username'];` Commented Nov 1, 2016 at 14:35
  • 1
    basic debugging: var_dump($_POST, $_GET, $_SERVER['REQUEST_METHOD']); see what you got, see how it was received. Commented Nov 1, 2016 at 14:36

3 Answers 3

1

As what it look it is correct and should run without any problem. Make sure the above code is what you actually have. From my experience most of the form submission can be

  • you don't have correct name (username)
  • you might send incorrect http verb (post)
  • you submit to wrong endpoint (2.php)

From you code above everything look fine. if you still don't have the right result, you better debug it with var_dump, or print_r function with these built-in $_POST, $_GET, $_REQUEST and check whether they contains you form variable name username

Sign up to request clarification or add additional context in comments.

Comments

0

You are using isset as a variable, but it is a function that returns a boolean.

Change $user=isset($_POST['username']); to $user=$_POST['username'];

2 Comments

$_POST_['username'];, take a look at that one again.
Had an an extra _ in there. Try again. @Qirel Thanks
0

Another thing is that in both case you will end up in the IF condition even if there is no value added to the field so you can do something like this too:

<html>
<?php
if(isset($_POST['username']) && !empty($_POST['username'])){
    $user=$_POST['username'];
    echo $user;
    echo " is your name";
}
else{
    $user=null;
    echo "error";
}
?>
</html>

1 Comment

isset($_POST['username']) && !empty($_POST['username']) is a bit redundant, you can do !empty() alone, which does the same thing.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.