这个PHP / JavaScript表单验证有什么问题?

时间:2011-04-13 05:40:06

标签: php javascript validation forms

我不确定我遇到的问题是使用JavaScript还是使用PHP。

我的目标:使用JavaScript验证一个简单的yes no form然后通过PHP处理并显示一条消息。

我的问题:当启用JavaScript并单击单选按钮并提交它时,PHP不会输出“YES status checked”。相反,它刷新页面(即。我认为它只是将表单发布到user_agreement4.php并且不执行任何其他操作)当禁用JavaScript并单击“是”单选按钮并提交它时,将显示“是状态已检查”消息。请注意,以下代码适用于user_agreement4.php。表格将提交给自己。

我做错了什么?

请注意,这是未完成的代码 - 我还没有添加cookie,重定向等内容。

我也有一个关于选择答案的问题。我可以选择不止一个回复作为答案吗?

<?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>

2 个答案:

答案 0 :(得分: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')

答案 1 :(得分: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
}

并删除“return false;”来自按钮的单击事件。验证函数在验证失败时返回false就足以阻止验证。

这应该使你的php能够按原样工作。