2011-04-13 22 views
0

我不確定我遇到的問題是使用JavaScript還是使用PHP。這個PHP/JavaScript表單驗證有什麼問題?

我的目標是:使用JavaScript驗證一個簡單的yes no表單,然後通過PHP處理它並顯示一條消息。

我的問題:啓用JavaScript後,我點擊單選按鈕並提交它,PHP不會輸出「YES status checked」。相反,它刷新頁面(即我認爲它只是將表單發佈到user_agreement4.php,並且什麼也不做)當JavaScript被禁用時,點擊YES單選按鈕並提交它,消息「YES status checked」顯示正確。請注意,以下代碼適用於user_agreement4.php。該表格將提交給自己。

我在做什麼錯?

請注意,這是未完成的代碼 - 我還沒有添加餅乾,重定向等東西。

另外我有一個關於選擇答案的問題。我可以選擇多個答案作爲答案嗎?

<?php 
// Set variables 
$selected_radio = 'test'; 
session_start(); // start up your PHP session! 

// The below code ensures that $dest should always have a value. 
if(isset($_SESSION['dest'])){ 
    $dest = $_SESSION['dest']; 
} 

// Get the user's ultimate destination 
if(isset($_GET['dest'])){ 
    $_SESSION['dest'] = $_GET['dest']; // original code was $dest = $_GET['dest']; 
    $dest = $_SESSION['dest'];   // new code 
} 
else { 
echo "Nothing to see here Gringo."; //Notification that $dest was not set at this time (although it may retain it's previous set value) 
} 

// Show the terms and conditions page 
//check for cookie 

if(isset($_COOKIE['lastVisit'])){ 
     /*  
     Add redirect >>>> header("Location: http://www.mywebsite.com/".$dest);  <<This comment code will redirect page 
     */ 
     echo "aloha amigo the cookie is seto!"; 
     } 
else { 
    echo "No cookies for you"; 
    } 
//Checks to see if the form was sent 

if (isset($_POST['submitit'])) { 
//Checks that a radio button has been selected 
    if (isset($_POST['myradiobutton'])) { 
     $selected_radio = $_POST['myradiobutton']; 
    //If No has been selected the user is redirected to the front page. Add code later 
      if ($selected_radio == 'NO') { 
       echo "NO status checked"; 
      } 
    //If Yes has been selected a cookie is set and then the user is redirected to the downloads page. Add cookie code later 
      else if ($selected_radio == 'YES') { 
       echo "YES status checked"; 
       // header("Location: http://www.mywebsite.com/".$dest); 
      } 
    } 
} 

?> 

<HTML> 
    <HEAD> 
    <TITLE>User Agreement</TITLE> 
    <script language="javascript"> 
function valbutton(thisform) { 
// validate myradiobuttons 
myOption = -1; 
for (i=thisform.myradiobutton.length-1; i > -1; i--) { 
if (thisform.myradiobutton[i].checked) { 
myOption = i; 
} 
} 
if (myOption == -1) { 
alert("You must choose either YES or NO"); 
return false; 
} 
if (myOption == 0) { 
alert("You must agree to the agreement to download"); 
return false; 
} 
thisform.submit(); // this line submits the form after validation 
} 
</script> 

    </HEAD> 
    <BODY> 
    <H1> User Agreement </H1> 
    <P>Before downloading you must agree to be bound by the following terms and conditions;</P> 
<form name="myform" METHOD ="POST" ACTION ="user_agreement4.php"> 
<input type="radio" value="NO" name="myradiobutton" />NO<br /> 
<input type="radio" value="YES" name="myradiobutton" />YES<br /> 
<input type="submit" name="submitit" onclick="valbutton(myform);return false;" value="ANSWER" /> 
</form> 




    </BODY> 
</HTML> 
+0

'我可以選擇多個答案作爲答案嗎?'你只能選擇一個答案,最好等到很多人有機會看到你的問題,這樣你才能得到更好的答案。通常等待一天才能接受一個好的答案是充足的時間。 – 2011-04-13 05:43:11

+0

@Madmartigan OK非常感謝。這是非常有用的知道。 – TryHarder 2011-04-13 05:45:13

+0

嘗試添加更多的回聲,看看你在POST中得到了什麼.. 並看看你是否越過ifs或不。讓我知道你得到了什麼 – jcane86 2011-04-13 06:27:44

回答

1

你的JavaScript更改爲:

function valbutton(thisform) { 
// validate myradiobuttons 
myOption = -1; 
for (i=thisform.myradiobutton.length-1; i > -1; i--) { 
if (thisform.myradiobutton[i].checked) { 
myOption = i; 
} 
} 
if (myOption == -1) { 
alert("You must choose either YES or NO"); 
return false; 
} 
if (myOption == 0) { 
alert("You must agree to the agreement to download"); 
return false; 
} 
return true; // this line enables the form to submit as normal and is not actually required 
} 

,並刪除了「返回false;「從點擊事件按鈕。在驗證失敗時驗證函數返回false足以阻止驗證。

這應該使您的PHP的工作原樣。

+0

這工作得很好!非常感謝你! – TryHarder 2011-04-13 14:47:22

2

看到這一行:

if (isset($_POST['submitit'])) { 

如果用戶按下submitit按鈕和JavaScript被禁用,一切正常 - 該按鈕,之前插入其名稱/值對到發佈的數據表單被髮布,因此設置$_POST['submitit']

但是,如果啓用了javascript,該按鈕不會自動觸發回發,而是調用發佈該表單的javascript函數。不幸的是,當你打電話給form.submit()時,它不會去查找按鈕,並將其名稱/值對添加到發佈的數據(出於各種原因)。所以你需要找到一種不同的方式來告訴你是否正在處理一個後回傳;最簡單的方法就是隻放一個隱藏字段插入到表單中併爲您的是,如:

(HTML中的一部分,<form></form>內某處):

<input type="hidden" name="is_postback" value="1" /> 

...然後改變你PHP檢查:

if ($_POST['is_postback'] == '1') 
+0

我試過這個,但我得到了一個未定義的索引錯誤。我可能做錯了什麼。我會稍後再討論它。無論如何,我真的很感謝快速回復謝謝! – TryHarder 2011-04-13 14:50:09