使用javascript验证下拉列表

时间:2013-08-07 17:34:31

标签: php javascript jquery

<?PHP
session_start();
if (isset($_SESSION['login']) && $_SESSION['login'] = '1') {
        //=========================================================
        //The following page is used to create a dynamic survey.
        //=========================================================
        $qNum = 'q1';
        $question = 'Question not set';
        $answerA = 'unchecked';
        $answerB = 'unchecked';
        $answerC = 'unchecked';
        $qID = array();
        $question = array();
        $A = array();
        $B = array();
        $C = array();
        $D = array();
        $E = array();
        $quesType = array();
        $survey_Answers1 = '';
        $nominatefriend = '';
        //============================================
        //  OPEN A CONNECTION TO THE DATABASE
        //============================================
        $user_name = "root";
        $password = "";
        $database = "surveyTest";
        $server = "127.0.0.1";
        $mysqli = new mysqli($server, $user_name, $password, $database);
        $SID = $_SESSION['user'];
        if (!mysqli_connect_errno()) {
            $result = $mysqli->query("SELECT * FROM tblquestions ORDER BY SUBSTRING(QID,2)+0");
            $numRows = mysqli_num_rows($result); //return number of rows in the table
            //var_dump($numRows);
            echo '<FORM NAME ="form1" METHOD ="POST" ACTION ="survey.php">';
            for ($i = 1; $i <= $numRows; $i++)
            {
                //var_dump ($mysqli->error);
                //var_dump($i);
                $db_field = mysqli_fetch_assoc($result);
                //var_dump($db_field);
                $qID[$i] = $db_field['QID'];
                $question[$i] = $db_field['Question'];
                $quesType = $db_field['qType'];
                //var_dump($quesType);
                $A[$i] = $db_field['qA'];
                $B[$i] = $db_field['qB'];
                $C[$i] = $db_field['qC'];
                $D[$i] = $db_field['qD'];
                $E[$i] = $db_field['qE'];
                if ($quesType == 'rating')
                {
                    echo '<P>';
                    print $qNum.': '.$question[$i]; 
                    echo '<P>';
                    if ($A[$i] != ""){
                        echo "<INPUT TYPE = 'Radio' Name = '".$qNum."'  value= '".$A[$i]."' id = '".$qNum."'>"; 
                        print $A[$i];
                    }
                    echo '<P>';
                    if ($B[$i] != ""){
                        echo "<INPUT TYPE = 'Radio' Name = '".$qNum."'  value= '".$B[$i]."' id = '".$qNum."'>"; 
                        print $B[$i];
                    }
                    echo '<P>';
                    if ($C[$i] != ""){
                        echo "<INPUT TYPE = 'Radio' Name = '".$qNum."'  value= '".$C[$i]."' id = '".$qNum."'>"; 
                        print $C[$i];
                    }
                    echo '<P>';
                    if ($D[$i] != ""){
                        echo "<INPUT TYPE = 'Radio' Name = '".$qNum."'  value= '".$D[$i]."' id = '".$qNum."'>"; 
                        print $D[$i];
                    }
                    echo '<P>';
                    if ($E[$i] != ""){
                        echo "<INPUT TYPE = 'Radio' Name = '".$qNum."'  value= '".$E[$i]."' id = '".$qNum."'>"; 
                        print $E[$i];
                    }
                    echo "<span style='color:red' id='radio_error'></span>";
                    if (isset($_POST[$qNum])){
                        $survey_Answers1 = $survey_Answers1.', '.$_POST["$qNum"];
                        var_dump($survey_Answers1);
                    }
                    $question_Number = ltrim($qNum,'q');
                    $question_Number++;
                    $qNum ='q'.$question_Number;
                }
                else if ($quesType == 'nominate')
                {
                    //echo "<INPUT TYPE = 'hidden' Name = '".$qNum."'  id = '".$qNum."'>";
                    $number_of_Combos = $A[$i];
                    //var_dump($number_of_Combos);
                    echo '<p>';
                    print $qNum.': '.$question[$i];
                    echo '</BR>';
                    for ($j = 1; $j <=  $number_of_Combos; $j++)
                    {
                        $nominatefriend = $qNum.$j;
                        //var_dump($nominatefriend);
                        $result_Combo = $mysqli->query("SELECT SID, fName, lName FROM students WHERE SID!='$SID'");
                        echo "<select name  = '".$nominatefriend."' id = '".$qNum."'>";
                        echo "<option value = '0'>".'Select a Name'."</option>";
                        while ($select_query_array = mysqli_fetch_array($result_Combo))
                        {
                            echo "<option value='".$select_query_array['SID']."'>".$select_query_array['fName'].' '.$select_query_array['lName']."</option>";
                        }
                        echo "</select>";
                    }

                    $question_Number = ltrim($qNum,'q');
                    $question_Number++;
                    $qNum ='q'.$question_Number;
                    //var_dump ($qNum);
                }
            }
            echo '<p>';
            echo "<INPUT TYPE = 'hidden' Name = 'h2'  VALUE = '".$survey_Answers1."'>";
                        echo '<p>';
            echo '<INPUT TYPE = "Submit" Name = "Submit1"  VALUE = "Click here to vote">';
            echo '</form>';



                        //$value1 = $_POST['nominateFriend1'];
                        //var_dump($value1);
                        //$value2 = $_POST['nominateFriend2'];
                        //var_dump($value2);
                        //$value3 = $_POST['nominateFriend3'];
                        //var_dump($value3);

            mysqli_close($mysqli);
        }
        else {
            print "Error getting Survey";
            mysqli_close($mysqli);
        }
    }
    else
    {
        header("location:login.php");
    }
    $_SESSION['user'] = $SID;  // refers to student ID from the login page
?>
<html>
<head>
<title>Radio Buttons</title>
</head>


<body>
<!--
    <P>
    <FORM NAME ="form2" METHOD ="GET" ACTION ="viewResults.php">
        <INPUT TYPE = "Submit" Name = "Submit2"  VALUE = "View results">
        <INPUT TYPE = "Hidden" Name = "h1"  VALUE = <?PHP print $qID; ?>>
    </FORM>
-->

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<script>
    $(function(){
        if ($('form').length > 0) {
            $('form').submit(function(e){
                var answers = '';
        var len = <?php echo $numRows; ?>;
                $('input[type=Radio]:checked').each(function() {
                    if (answers !== '') {
                        answers += ',';
                    }
                    answers += $(this).val();
            //alert(answers);
                })
                $('input[name=h2]').val(answers);       
        for (var i = 1; i <= len; i++ ) {
            var qNum = 'q'+i;
            //document.write(qNum);
            if ($('#'+qNum+'').is(':radio')) {
                if (($('input[name='+qNum+']:checked').length == 0) ){
                    alert("No Selection is made for "+ qNum);
                    return false;
                }
            }
            else if ($('#'+qNum+'').is('select')) {
                var j = 1;
                var nominatefriend = qNum+j;
                alert (nominatefriend);
                var check = '';
                var chosen = new Array();
                while (check = document.getElementsByName("nominatefriend")) {
                    alert(check);
                    var len = document.form1[nominatefriend].length;
                    alert (len);
                    for (var i = 0; i < len; i++) {
                        if (document.form1[nominatefriend[i]].selected) {
                            chosen[j] = document.form1[nominatefriend[i]].value;
                            alert (chosen[j]);
                        }
                    }
                    j++;
                    nominatefriend = qNum+j;
                    alert (nominatefriend);
                }
            }
        }   // for loop        
            }); // form submit function

        }// form.length
    })//function
</script>
</body>
</html>

我正在尝试验证根据mysql数据库中的值动态创建的下拉列表。但是,我收到以下错误:TypeError:document.form1.nominatefriend在以下if条件中未定义:

if (document.form1[nominatefriend[i]].selected) {
  chosen[j] = document.form1.nominatefriend[i].value;
  alert (chosen[j]);
}

2 个答案:

答案 0 :(得分:0)

$nominatefriend = $qNum.$j;

echo "<select name  = '".$nominatefriend."' id = '".$qNum."'>";

所以它还未定义

答案 1 :(得分:0)

该错误表明提名人未定义。

如果我查看您的PHP文件代码,我会发现在行号99和102中您有

$nominatefriend = $qNum.$j;

echo "<select name  = '".$nominatefriend."' id = '".$qNum."'>";

如果我查看更多,我会发现$ qNum为

$qNum = 'q1';

因此它可能是document.form1.q1[i]而不是document.form1.nominatefriend[i]

错误在于命名,因此如果您可以查看源页面,您可能会找到应在js中使用的正确值。