php - validating a drop down list using javascript -
<?php session_start(); if (isset($_session['login']) && $_session['login'] = '1') { //========================================================= //the following page used create 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 connection 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 * tblquestions order substring(qid,2)+0"); $numrows = mysqli_num_rows($result); //return number of rows in table //var_dump($numrows); echo '<form name ="form1" method ="post" action ="survey.php">'; ($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>'; ($j = 1; $j <= $number_of_combos; $j++) { $nominatefriend = $qnum.$j; //var_dump($nominatefriend); $result_combo = $mysqli->query("select sid, fname, lname students sid!='$sid'"); echo "<select name = '".$nominatefriend."' id = '".$qnum."'>"; echo "<option value = '0'>".'select 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 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 student id 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); (var = 1; <= len; i++ ) { var qnum = 'q'+i; //document.write(qnum); if ($('#'+qnum+'').is(':radio')) { if (($('input[name='+qnum+']:checked').length == 0) ){ alert("no selection made "+ 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); (var = 0; < len; i++) { if (document.form1[nominatefriend[i]].selected) { chosen[j] = document.form1[nominatefriend[i]].value; alert (chosen[j]); } } j++; nominatefriend = qnum+j; alert (nominatefriend); } } } // loop }); // form submit function }// form.length })//function </script> </body> </html>
i'm trying validate drop down list created dynamically based on value in mysql database. however, i'm getting following error: typeerror: document.form1.nominatefriend undefined in following if condition:
if (document.form1[nominatefriend[i]].selected) { chosen[j] = document.form1.nominatefriend[i].value; alert (chosen[j]); }
$nominatefriend = $qnum.$j; echo "<select name = '".$nominatefriend."' id = '".$qnum."'>";
so it's undefined yet
Comments
Post a Comment