Reputation: 713
I've been working on a simple script in an effort to learn a little more about PHP. To that end I am trying to create a form handler that will echo the name of which ever user is older and I've gotten very confused and am hoping someone of you might take a look and give me some simple pointers as to what i am doing wrong. Ideally, $result
will echo the name of whichever person is older.
My Submit form (0001Form.php
)
<html lang="en">
<head>
<meta charset="utf-8">
<title>Test | Form</title>
<meta name="description" content="php tutorial stuff">
<meta name="author" content="php tutorial stuff">
<link rel="stylesheet" href="css/styles.css?v=1.0">
<!--[if lt IE 9]>
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
</head>
<body>
<script src="js/scripts.js"></script>
<form action="0001FormHandler.php" method="post">
<p> Name 1: <input type="text" name="userNameA" /> </p>
<p>Age 1: <input type="text" name="userAgeA"/></p>
<p>Your name 2: <input type="text" name="userNameB" /></p>
<p>Your age 2: <input type="text" name="userAgeB" /></p>
<p><input type="submit" /></p>
</form>
</body>
</html>
My first attempt at a form handler (0001FormHandler.php
):
<?php
//Assign ages to vars for comparison from from
$varA == userAgeA;
$varB == userAgeB;
//compare vars
if ($varA >= $varB)
{$result == UserAgeA;}
else ($result == userAgeB);
//If A is older then B, use A name, else B
if ($result == UserAgeA)
{$result = userNameA;}
else ($result = userNameB);
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Welcome <?php echo $result;?></title>
<meta name="description" content="php tutorial stuff">
<meta name="author" content="php tutorial stuff">
<link rel="stylesheet" href="css/styles.css?v=1.0">
<!--[if lt IE 9]>
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
</head>
<body>
<script src="js/scripts.js"></script>
<p>
Hello <?php echo htmlspecialchars($_POST['userNameA']); ?>.
You are <?php echo (int)$_POST['userAgeB']; ?> years old.
Hi <?php echo htmlspecialchars($_POST['userNameB']); ?>.
You are <?php echo (int)$_POST['userAgeB']; ?> years old.
</p>
</body>
</html>
My second attempt at a form handler (0001FormHandler.php
):
<?php
//asign values to evaluate
$varA = ($_POST[userAgeA]);
$varB = ($_POST[userAgeB]);
//If A is Greater then B, then a else b
if ($varA >= $varB)
{$result == UserAgeA;}
else ($result == userAgeB);
//if A then UserA else User B
if ($result == UserAgeA)
{$result = userNameA;}
else ($result = userNameB);
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
//Echo result name in title!
<title>Welcome <?php echo $result;?></title>
<meta name="description" content="php tutorial stuff">
<meta name="author" content="php tutorial stuff">
<link rel="stylesheet" href="css/styles.css?v=1.0">
<!--[if lt IE 9]>
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
</head>
<body>
<script src="js/scripts.js"></script>
<p>
Hello <?php echo htmlspecialchars($_POST['userNameA']); ?>.
You are <?php echo (int)$_POST['userAgeB']; ?> years old.
Hi <?php echo htmlspecialchars($_POST['userNameB']); ?>.
You are <?php echo (int)$_POST['userAgeB']; ?> years old.
</p>
</body>
</html>
Upvotes: 0
Views: 1126
Reputation: 4881
Your form handler doesn't really get the data from the form. You can access "POST"ed data via the $_POST
variable. This means that you need to change your code slightly (I removed some duplicate statements of yours for clarity):
<?php
//Assign ages to vars for comparison from from
$varA = isset($_POST["userAgeA"]) ? $_POST["userAgeA"] : 0;
$varB = isset($_POST["userAgeB"]) ? $_POST["userAgeB"] : 0;
//compare vars
$result = ($varA >= $varB) ? $varA : $varB;
?>
Upvotes: 1
Reputation: 4494
You have confused the comparison operator ==
and the assignment operator =
. You also must use $_POST to retrieve data from the form.
This code:
$varA == userAgeA;
$varB == userAgeB;
should read (note that == is changed to =):
$varA = $_POST['userAgeA'];
$varB = $_POST['userAgeB'];
and then compare:
if ($varA >= $varB) {
$result = $_POST['userNameA'];
}
else {
$result = $_POST['userNameB'];
}
Upvotes: 1
Reputation: 2943
You are assigning the values in a wrong way. You are using POST method so in order to access the submitted data you should use:
$varA = $_POST['userAgeA'];
$varB = $_POST['userAgeB'];
And then
if ($varA >= $varB)
echo $_POST['userNameA']
else
echo $_POST['userNameB']
I also noticed you are using == instead of = for assigning values to variables. Correct notation is
$varname = somevalue;
== is a compare operator, it compares if two variables are equal.
Upvotes: 1