0

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>
6
  • 1
    FIX this $varA == userAgeA; Assignment in PHP is $varA = userAgeA; only ONE equal sign. Do this, edit your code, and ask again. Commented Dec 29, 2013 at 23:55
  • in the form handles, the firsts lines, to get values from "userAgeA" form control, use $_GET or $_POST. to assign values to $varA use only one equal sign. Commented Dec 29, 2013 at 23:55
  • I attempted the revisions as best I could based on my limited understanding. Commented Dec 30, 2013 at 0:07
  • Re-Revised it a second time based on Comments below and it works now. Very educational exercise. Thank you. Commented Dec 30, 2013 at 0:22
  • I've rolled back your edit; please don't modify your question in such a way that it becomes useless to others with the same problem. Commented Dec 30, 2013 at 4:33

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, your comments and directions were aces on! I learned a lot here and will be playing with this all night trying to engrain it into my touch memory, et al. Thank you very much. (I take my first college php class this winter and i'm trying to get the basic syntax down so i can concentrate on the hard stuff! Thank you.
1

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'];
}

Comments

1

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;

?>

2 Comments

Is your code saying 'VarA equals A post A (change 0 to age a); VarB equals post B (change zero to B); compare results post which is greater. Is that what it is saying? Its very different from what I'm imagined (i'm very much the newb).
Thanks for the clarification - super appreciated - happy new year

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.