开发者

Why can't I retrieve radio button variables?

Not sure what the problem is.. it's coming back with a value of "1" for the form variables, instead of the value put into the form by the user. Where is it getting the "1" from and how do I fix it?

<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($m开发者_JAVA百科yFav) || 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>


isset returns a boolean (represented as 1 or 0 in a string), 1 (true) if the variable is set, 0 (false) if it isn't.

Thus, when you do this:

//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']);

You're setting all the variables to 1 if they are set or 0 if not.

You can fix the code as follows:

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


Shouldn't these variables:

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

Be these:

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

Otherwise, you're just storing whether they have been set, not their values.


isset() tests if the value exists. To get the actual value, you just need to do the statements like $var = $_GET['var'];.


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

This returns 1 because isset() returns a Boolean value; 1 if the value is set, and 0 (TRUE or FALSE in string form) if the value is not set, as the manual says:

Returns TRUE if var exists and has value other than NULL, FALSE otherwise.

Try using:

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


You are doing two things wrong (the while and the isset), which can probably fixed and simplified with this approach:

// 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, ...

This avoids the isset test which you stumbled over, and only extracts the four variables from POST which you are actually interested in (and the defaults get applied as well).


Instead of

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

Which is quite wrong, since there's no looping going on and why do you check if isset now, and then do it again later?

You can just use:

$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';

As stated by the other users, isset returns a boolean (true/false but also 1/0), and that's the 1 you're getting instead of the actual $_POST var.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜