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

问题描述

| 我不确定我遇到的问题是JavaScript还是PHP。 我的目标:使用JavaScript验证简单的“是”表单,然后通过PHP处理它并显示一条消息。 我的问题:启用JavaScript并单击单选按钮并将其提交后,PHP不会输出“已检查状态”。取而代之的是刷新页面(即,我认为它只是将表单发布到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>
    

解决方法

将您的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照原样工作。     ,看到这一行:
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\')