Reputation: 151
<HTML>
<body>
<?php
echo "<H4>Are you a Male</h4>"
?>
<form action ="ifstat.php" method ="post">
Enter Please: <input type="text" name="val1">
<input type ="submit" >
<input type ="reset">
</form>
<?php
$isMale=$_POST["val1"];
if($isMale =="y" || "yes"){
echo("You are a Male");
} elseif($isMale =="n" || "no") {
echo("You are a female");
} else{
echo("Invalid entry");
}
?>
</body>
</html>
So, this is my program code. I always keep on getting You are a Male as the output even though I type in "n" or some other value rather than y. What's wrong with the code here?
Here is the photo of the output https://i.sstatic.net/Fpk5U.jpg
Upvotes: 1
Views: 50
Reputation: 57131
In your if
statements....
if($isMale =="y" || "yes" ){
the || "yes"
isn't valid and will always (in this case) evaluate to true.
You also don't check if anything has been entered before processing this, so normally you would do a check of if something is set (i.e. has the forma been submitted) and then do the processing...
<?php
if (isset($_POST["val1"])) {
$isMale=$_POST["val1"];
if($isMale =="y" || $isMale =="yes"){
echo("You are a Male");
} elseif($isMale =="n" || $isMale =="no") {
echo("You are a female");
} else {
echo("Invalid entry");
}
}
?>
Upvotes: 1
Reputation: 1365
You need to change your condition formatting from if($isMale =="y" || "yes"){
to if($isMale =="y" || $isMale =="yes"){
<HTML>
<body>
<?php
echo "<H4>Are you a Male</h4>"
?>
<form action ="ifstat.php" method ="post">
Enter Please: <input type="text" name="val1">
<input type ="submit" >
<input type ="reset">
</form>
<?php
$isMale=$_POST["val1"];
if($isMale =="y" || $isMale =="yes"){
echo("You are a Male");
} elseif($isMale =="n" || $isMale =="no") {
echo("You are a female");
} else{
echo("Invalid entry");
}
?>
</body>
</html>
Upvotes: 1