2011-04-13 19 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