我不確定我遇到的問題是使用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>
'我可以選擇多個答案作爲答案嗎?'你只能選擇一個答案,最好等到很多人有機會看到你的問題,這樣你才能得到更好的答案。通常等待一天才能接受一個好的答案是充足的時間。 – 2011-04-13 05:43:11
@Madmartigan OK非常感謝。這是非常有用的知道。 – TryHarder 2011-04-13 05:45:13
嘗試添加更多的回聲,看看你在POST中得到了什麼.. 並看看你是否越過ifs或不。讓我知道你得到了什麼 – jcane86 2011-04-13 06:27:44