为什么我无法检索单选按钮变量?

时间:2011-06-07 22:13:04

标签: php forms variables

不确定问题是什么..它返回值为“1”的表单变量,而不是用户放入表单的值。从哪里获得“1”以及如何修复它?

<html>

<head>
<title>Feedback Form</title>
<!-- Modified by: Student Name -->
<!-- The page should accept user input in form values, then, after the form
 is submitted, hide the form and reveal a confirmation message using
 the data entered into the form elements. -->


</head>

<body>
<h1 align="center">We Need You!</h1>
<h2 align="center">Please provide us with your valuable feedback!</h2>
<hr>

<?php
if (!(isset($myName) || isset($myAge) || isset($myFav) || isset($myComments)))
    {
$myName = "anonymous";
$myAge = "unspecified";
$myFav = "unspecified";
$myQuestion = "unspecified";
}

$mySubmit = isset($_POST['btnSubmit']);
?>

<form name="frmFeedback" id="frmFeedback" action="sendFeedback.php" method="post" 
<?php if ($mySubmit == "Send Feedback!") { echo ' style="display: none"'; } ?>>
Name: <input type="text" name="txtName">
<br>
<br>
Age: <select name="mnuAge">
    <option value="youth">Youth</option>
    <option value="teen">Teen</option>
    <option value="adult">Adult</option>
    <option value="senior">Senior</option>
 </select>
<br>
<br>
What was your favorite page?
<br>
<input type="radio" name="radFav" value="ASP tutorial">ASP Tutorial
<br>
<input type="radio" name="radFav" value="JavaScript tutorial">JavaScript Tutorial
<br>
<input type="radio" name="radFav" value="PHP tutorial"> PHP Tutorial
<br>
<br>
Which pages did you visit?
<br>
<input type="checkbox" name="chkView[]" value="ASP tutorial">ASP Tutorial
<br>
<input type="checkbox" name="chkView[]" value="JavaScript tutorial">JavaScript Tutorial
<br>
<input type="checkbox" name="chkView[]" value="PHP tutorial"> PHP Tutorial
<br>
<br>
Do you have any additional scripting questions?
<br>
<textarea name="txaQuestions" wrap="soft" cols="50" rows="10">
</textarea>
<br>
<br>
<input type="submit" name="btnSubmit" value="Send Feedback!">
</form>
<?php

//Once the form elements have been filled in, extract data from form and store in 
//variables
$myName = $_POST['txtName'];
$myAge = $_POST['mnuAge'];
$myFav = $_POST['rdFav'];
$myQuestion = $_POST['txaQuestions'];     

if ($mySubmit == "Send Feedback!")
{
//hide form
//$myFormDisp = "none";

//display message
print("<h3 align='center'>Thank you!!</h3>");
print("Hello, ".$myName."!");
print("Thank you very much for your feedback on our tutorial site.");
print("The ".$myAge." age group is one of our most critical market segments,")
    print("so we really appreciate the time you took to fill out our form. ");
print("Active web visitors like yourself are what make these pages possible. ");
print("We are very glad you enjoyed the ".$myFav." page.");

if (isset($_POST['chkView'])) 
  {
    print(", and hope that you found the other pages you viewed (");
    foreach($_POST['chkView'] as $myView)
     {
        print("".$myView.", ");
     }
    print("etc.) to be just as helpful.");
  }
else
  {
print(". The next time you visit we hope you have a chance to view");
print("our other tutorials also.</p>");
  }

print("<p>We will respond to your question: \"".$myQuestion."\" ");
    print("just as soon as we can</p>");

print("<h3 align='center' Thanks for stopping by!</h3>");
  }
else
{
//set form to display
//$myFormDisp = "block";
}

?>

</body>
</html>

6 个答案:

答案 0 :(得分:2)

isset返回一个布尔值(在字符串中表示为1或0),如果设置了变量则返回1(真),如果不设置则返回0(假)。

因此,当你这样做时:

//Once the form elements have been filled in, extract data from form and store in 
//variables
$myName = isset($_POST['txtName']);
$myAge = isset($_POST['mnuAge']);
$myFav = isset($_POST['rdFav']);
$myQuestion = isset($_POST['txaQuestions']);

如果设置了所有变量,则将其设置为1,否则设置为0.

您可以按如下方式修改代码:

//Once the form elements have been filled in, extract data from form and store in 
//variables
if(isset($_POST['txtName']) {
   $myName = $_POST['txtName'];
};
// etc

答案 1 :(得分:1)

不应该是这些变量:

$myName = isset($_POST['txtName']);
$myAge = isset($_POST['mnuAge']);
$myFav = isset($_POST['rdFav']);
$myQuestion = isset($_POST['txaQuestions'])

是这些:

$myName = $_POST['txtName'];
$myAge = $_POST['mnuAge'];
$myFav = ['rdFav'];
$myQuestion = $_POST['txaQuestions'];

否则,您只是存储它们是否已设置,而不是它们的值。

答案 2 :(得分:0)

isset()测试值是否存在。要获得实际值,您只需要执行$var = $_GET['var'];等语句。

答案 3 :(得分:0)

$myName = isset($_POST['txtName']);

这将返回1,因为isset()返回一个布尔值;如果值已设置,则1,如果未设置该值,则0TRUEFALSE为字符串形式),如手册所示:

  

如果var存在并且其值不是NULL,则返回TRUE,否则返回FALSE。

尝试使用:

if (isset($_POST['txtName'])) {
    $myName = $_POST['txtName'];
}

答案 4 :(得分:0)

你做错了两件事(whileisset),这可能是通过这种方法修复和简化的:

// Prepare defaults for unset fields:
$defaults = array(
  "myName" => "anonymous",
  "myAge" => "unspecified",
  "myFav" => "unspecified",
  "myQuestion" => "unspecified"
);

// make local variables
extract(array_merge($defaults, array_intersect_key(array_filter($_POST), $defaults)));
# $myName, $myAge, ...

这避免了你偶然发现的isset测试,只从你真正感兴趣的POST中提取了四个变量(并且默认值也被应用)。

答案 5 :(得分:0)

而不是

while (!(isset($myName) || isset($myAge) || isset($myFav) || isset($myComments)))
    {
$myName = "anonymous";
$myAge = "unspecified";
$myFav = "unspecified";
$myQuestion = "unspecified";
}

这是非常错误的,因为没有循环,为什么现在检查是否存在,然后再次进行?

你可以使用:

$myName = isset($_POST['txtName']) ? $_POST['txtName'] : 'anonymous';
$myAge = isset($_POST['mnuAge']) ? $_POST['mnuAge'] : 'unspecified';
$myFav = isset($_POST['radFav']) ? $_POST['radFav'] : 'unspecified';
$myQuestion = isset($_POST['txaQuestions']) ? $_POST['txaQuestions'] : 'unspecified';

正如其他用户所说,isset返回一个布尔值(true / false但也是1/0),这是你获得的1而不是实际的$ _POST var。