2011-06-07 10 views
1

問題が何であるかわかりません。ユーザーがフォームに入力した値ではなく、フォーム変数の値が「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> 
+0

どのフィールドの値が1になっていますか?また、宿題に関する質問に宿題タグを使用してください。 –

+0

'while'はおそらく' if'でしょう。それはここで偶然働いています。また、あなたが単独で調べなければ、すべてのフィールドを一度にリセットします。 – mario

+0

whileをifに変更し、変数から "isset"を削除しました。最終的には、myFav(私はいつも問題を抱えていたラジオボタン)を除いて、入力された変数を返しています。 – Anna

答えて

2

issetそうでない場合、(文字列に1又は0として表される)ブール値を返す1(真)変数が設定されている場合、0(偽)。したがって

、あなたがこれを行う:彼らは設定または0でない場合はされている場合は、1にすべての変数を設定している

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

次のようにコードを修正することができます:値が存在する場合は

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

isset()テストを。実際の値を取得するには、$var = $_GET['var'];のようなステートメントを実行するだけです。

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

は、あなただけの彼らはない、その値を設定されているかどうかを保存しています。

+0

私はこれを変更しました:
$ myName = isset($ _ POST ['txtName']); $ myAge = isset($ _ POST ['mnuAge']); $ myFav = isset($ _ POST ['rdFav']); $ myQuestion = isset($ _ POST ['txaQuestions']); to this:
$ myName = $ _POST ['txtName']; $ myAge = $ _POST ['mnuAge']; $ myFav = $ _POST ['rdFav']; $ myQuestion = $ _POST ['txaQuestions']; しかし、私はまだ$ myFavに問題があります。 – Anna

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

これは1isset()ため返すブール値を返します。値が設定されている場合1、および0TRUEまたは文字列形式でFALSE)のマニュアルが言うように値が設定されていない場合:varが存在し、NULL以外の値、それ以外の場合にFALSEを持っている場合

trueを返します。

使用してみてください:

if (isset($_POST['txtName'])) { 
    $myName = $_POST['txtName']; 
} 
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から4つの変数のみを抽出します(デフォルトも同様に適用されます)。

0

の代わりに起こって何のループがありませんから、かなり間違っていると、今ISSET場合は、なぜあなたがチェックしますか、し、後で再びそれを行う

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は(真/偽も1/0)ブール値を返し、それはあなたが実際の代わりに取得している1です:

あなただけ使用することができます$ _POST var。

関連する問題