<?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數據庫中的值動態創建的。不過,我發現了以下錯誤:類型錯誤:document.form1.nominatefriend未定義在以下if條件:使用javascript驗證下拉列表
if (document.form1[nominatefriend[i]].selected) {
chosen[j] = document.form1.nominatefriend[i].value;
alert (chosen[j]);
}
向我們展示您的HTML? – 2013-08-07 17:46:53
您仍未顯示其中包含'提名朋友'的HTML代碼。 – 2013-08-07 17:55:24
我們不知道你的數據庫查詢返回什麼值,它轉換成什麼值。請在[jsfiddle](http://jsfiddle.net)上提供一個示例html。 – Srihari