am learning PHP, i dont know why this code is not doing the right thing. please help?

Gv g

New member
here is the code.

<form action="welcome8.php" method="request">
<div><label for="firstname"><b>First name:</b>
<input type="text" name="firstname" id="firstname"/></label>
</div>
<div><label for="lastname"><b>Last name:</b>
<input type="text" name="lastname" id="lastname"/></label></div>
<div><input type="submit" value="GO"/></div>
</form>

<?php
$firstname=$_REQUEST['firstname'];
$lastname=$_REQUEST['lastname'];

if ($firstname=='kevin' and lastname=='Yank')

{
echo 'welcome, glorious leader!';
}

else
{
echo 'welcome to our website, '.
htmlspecialchars($firstname, ENT_QUOTES, 'UTF-8').''.
htmlspecialchars($lastname, ENT_QUOTES, 'UTF-8').'!';
}
?>

so the code is only executing welcome to our website, but not performing the if else conditions. pls help
i have changed 'and' with '&&' not working yet.

i also had;

<?php
$name = $_REQUEST['name'];
if ($name == 'Kevin')

{
echo 'Welcome, oh glorious leader!';
htmlspecialchars($firstname, ENT_QUOTES, 'UTF-8') . ' ' .
htmlspecialchars($lastname, ENT_QUOTES, 'UTF-8') . '!';
}
?>

which doesn't work as well, what could be the problem.
 
Stay away from $_REQUEST and use $_POST.

There is no "request" method for forms use "post" (in this case) or "get". Name your "submit" button too.

copy and paste this code to try it out:

<html>
<head>
<title>POST example</title>
</head>
<body>
<?php

if(isset($_POST['submit'])) { // form has been submitted

$firstname = $_POST['firstname'];
$lasname = $_POST['lastname'];

if($firstname == "Kevin") {
echo "Welcome, oh glorious leader!";
} else {
echo "You are not Kevin!";
}
}

?>

<form action="<?php $_SERVER['PHP_SELF']; ?>" method="post">
<div><label for="firstname"><b>First name:</b>
<input type="text" name="firstname" id="firstname"/></label>
</div>
<div><label for="lastname"><b>Last name:</b>
<input type="text" name="lastname" id="lastname"/></label></div>
<div><input name="submit" type="submit" value="GO"/></div>
</form>

</body>
</html>
 
Back
Top