Reputation: 11
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.
Upvotes: 0
Views: 101
Reputation: 821
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>
Upvotes: 0
Reputation: 9937
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
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
Upvotes: 1
Reputation: 3599
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'];
Upvotes: 0