PHP比较表单变量

时间:2013-12-29 23:48:40

标签: php forms variables compare

我一直在研究一个简单的脚本,以便更多地学习PHP。为此,我正在尝试创建一个表单处理程序,它将回显用户年龄较大的名称并且我已经非常困惑,我希望你们中的某个人可以看看并给我一些关于我是什么的简单指示做错了。理想情况下,$result将回应年龄较大的人的姓名。

我的提交表单(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>

我第一次尝试使用表单处理程序(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>

我第二次尝试使用表单处理程序(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>

3 个答案:

答案 0 :(得分:1)

您正在以错误的方式分配值。您正在使用POST方法,以便访问您应该使用的提交数据:

$varA = $_POST['userAgeA'];
$varB = $_POST['userAgeB'];

然后

if ($varA >= $varB)
 echo $_POST['userNameA']
else
 echo $_POST['userNameB']

我还注意到你使用==而不是=来为变量赋值。正确的表示法是

$varname = somevalue;

==是一个比较运算符,它比较两个变量是否相等。

答案 1 :(得分:1)

您的表单处理程序并不真正从表单中获取数据。您可以通过$_POST variable访问“POST”ed数据。这意味着您需要稍微更改您的代码(为清楚起见,我删除了您的一些重复声明):

<?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 :(得分:1)

您混淆了comparison operator ==assignment operator =。您还必须使用$ _POST从表单中检索数据。

此代码:

$varA == userAgeA;
$varB == userAgeB;

应该阅读(注意==更改为=):

$varA = $_POST['userAgeA'];
$varB = $_POST['userAgeB'];

然后比较:

if ($varA >= $varB) {
    $result = $_POST['userNameA'];
}
else {
    $result = $_POST['userNameB'];
}