My first code was as follows. Please tell me whats the problem test.php
<html>
<title>Speaker</title>
<body>
<form action="welcome.php" method="post" enctype="multipart/form-data">
Name: <input type="text" name="name">
Number: <input type="text" name="number">
<input type="submit">
</body>
</html>
Welcome.php
<?php
if(isset($_POST["number"]))
{$_POST["number"]=$number;}
if(isset($_POST["name"]))
{$_POST["name"]= $name;}
if($number<33)
{echo "hello" "$name" "you have been failed" ;
}
?>
This script is not working. Please tell me where the problem is.
You are setting your variables backwards. It should look like:
if( isset($_POST["number"]) ){
$number = (int) $_POST["number"];
}
if( isset($_POST["name"]) ){
$name = $_POST["name"];
}
if( isset($number) && $number<33 ){
echo "hello {$name} you have been failed";
}
I would also recommend using a more standard if/else
style like in my example, it is easier to read in larger statements. There was also a bug in your echo
, you need to concatenate the string with a .
or put the variable inside double quotes using curly braces. You would want to check for $number
being set (if it isn't in the $_POST
then it will generate a warning in your if statement).
Lastly you want to make sure you are converting your inputs to the proper type, and sanitizing them to protect against SQL injections, cross site scripting, and other attacks (this is not shown in my example).
You are not setting your variable that's why you aren't able to print. To set any variable you should first declare cariable itself and then give it a value. so it should be
if(isset($_POST["number"])){
$number = $_POST["number"];
}
if(isset($_POST["name"])){
$name = $_POST["name"];
}
if($number<33) {
echo "hello".$name." you have been failed" ;
}
Neither $number or $name are defined I think you meant to do this :
if(isset($_POST["number"]))
{$number=$_POST["number"];}
if(isset($_POST["name"]))
{$name=$_POST["name"];}
Try this... Also, check the $number
only if $_POST
is set and is not empty... An the echo was wrong too.
<?php
if( !empty($_POST) )
$number = $_POST["number"];
$name = $_POST["name"];
if($number<33) {
echo "hello $name you have been failed" ;
}
}
?>