2011-06-07 49 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?另外,請使用作業標籤來處理與作業有關的問題。 – 2011-06-07 22:15:34

+0

'while'應該是'if'。這是偶然發生的,除非你單獨探測它們,它也會一次重置所有的領域。 – mario 2011-06-07 22:17:38

+0

我已經改變了如果,並從變量中刪除了「isset」。它最終返回輸入的變量,除了myFav(單選按鈕,我一直遇到麻煩)。 – Anna 2011-06-07 22:38:05

回答

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

你,如果他們設置或0,如果不是所有的變量設置爲1。如果該值存在

//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']); 對此:
$ myName = $ _POST ['txtName']; $ myAge = $ _POST ['mnuAge']; $ myFav = $ _POST ['rdFav']; $ myQuestion = $ _POST ['txaQuestions']; 但是我仍然遇到$ myFav的麻煩。 – Anna 2011-06-07 22:29:38

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

這返回1因爲isset()返回一個布爾值; 1如果該值被設置,並且0TRUE或以字符串形式FALSE)如果該值沒有被設置,作爲手冊說:如果變種存在並且具有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四個變量(以及默認值也被應用)。

0

而不是

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

這是非常錯誤的,因爲沒有循環回事?爲什麼要檢查,如果現在isset,然後再做一遍以後?

你可以使用:

$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。

相關問題