我正在PHP
创建在线测试系统,我在其中放置了一个问题类型,用户通过点击它来选择错误的问题。鼠标点击word后,单词background变为红色。我使用window.getSelection().toString()
,但我无法得到这个单词。它之前工作得很好,但现在警报没有显示任何值。它显示空白值。我的html和javascript代码如下所示。
<form method="post" name="test" action="student_result.php">
<?php
$query_get_audio=mysql_query("SELECT * FROM tps_questions where uid = $uid and question_type='audio'");
$row1 = mysql_fetch_assoc($query_get_audio);
?>
<div class="maindiv" >
<div >
<audio controls id='myaudio' name='myaudio' class='myaudio' >
<source src="admin/<?php echo $row1 ['audio_file_loc']?>" type='audio/mpeg'>
</audio>
<br/>
<br/>
</div>
<?php
$query_get_uid=mysql_query("SELECT * FROM `tps_questions` WHERE `question_type` != 'audio' AND `uid` = $uid");
$counter=1;
while($row = mysql_fetch_assoc($query_get_uid))
{
$strqid=$strqid . "," .$row ['qid'];
$strqtype=$strqtype . "," .$row ['question_type'];
$strtime = $strtime . "," .$row ['timeDuration'];
$qid = $row ['qid'];
$quetype = $row['question_type'];
$quetime = $row['timeDuration'];
$que = html_entity_decode($row['q_name']);
$que = nl2br($que);
if($counter==1)
{
$pro="block";
}
else
{
$pro="none";
}
if($quetype=="radio")
{
$query = mysql_query("SELECT * FROM tps_answers, tps_right_que_ans WHERE tps_answers.aid = tps_right_que_ans.aid AND qid ='$qid'");
$row_res = mysql_fetch_assoc($query);
$aid = $row_res['aid'];
$ans1 = htmlspecialchars($row_res['ans1']);
$ans2 = htmlspecialchars($row_res['ans2']);
$ans3 = htmlspecialchars($row_res['ans3']);
$ans4 = htmlspecialchars($row_res['ans4']);
echo "<div class='div" . $counter . "' style='display:" . $pro . ";' >
<input type='hidden' name='qtype$counter' value = '$quetype'/>
<input type='hidden' name='time$counter' value = '$quetime'/>
<div >
<div style='float:left;width:20%'>Que - $counter.) </div>
<div style='float:left;width:80%'><input type='hidden' name='q$counter' value = '$qid'/>
<p>$que</p>
</div>
</div>
<div>
<div style='float:left;width:20%'>Ans : </div>
<div style='float:left;width:80%;padding-bottom: 20px;'>
<label>a) <input id='radioans$counter" . "_1' type='radio' name='ans$counter' value='$ans1'/> $ans1</label><br>
<label>b) <input id='radioansq$counter" . "_2' type='radio' name='ans$counter' value='$ans2' /> $ans2</label><br>
<label>c) <input id='radioansq$counter" . "_3' type='radio' name='ans$counter' value='$ans3'/> $ans3</label><br>
<label>d) <input id='radioansq$counter" . "_4' type='radio' name='ans$counter' value='$ans4'/> $ans4</label><br>
<label>e) <input id='notattempt' type='radio' name='ans$counter' value='Not Attempted' checked='checked'/>Not Attempted</label><br>
</div>
</div>
</div>";
}
if($quetype=="fill")
{
$string="";
$query = mysql_query("SELECT * FROM tps_answers, tps_right_que_ans WHERE tps_answers.aid = tps_right_que_ans.aid AND qid ='$qid'");
$string= $string . "<div class='div" . $counter . "' style='display:" . $pro . ";' >
<input type='hidden' name='qtype$counter' value = '$quetype'/>
<input type='hidden' name='time$counter' value = '$quetime'/>
<div >
<div style='float:left;width:20%'>Que - $counter.) </div>
<div style='float:left;width:80%'><input type='hidden' name='q$counter' value = '$qid'/>
<p>$que</p>
</div>
</div>
<div style='float:left;width:20%'>Ans : </div>
<div style='float:left;width:80%;padding-bottom: 20px;'>" ;
$val=1;
while($row_res = mysql_fetch_assoc($query))
{
$aid = $row_res['aid'];
$ans1 = htmlspecialchars($row_res['ans1']);
$ans2 = htmlspecialchars($row_res['ans2']);
$ans3 = htmlspecialchars($row_res['ans3']);
$ans4 = htmlspecialchars($row_res['ans4']);
$string= $string . "
<label> <input id='radioans$counter" . "_1' type='radio' name='ans$val$counter' value='$ans1' checked='checked'/> $ans1</label>
<label> <input id='radioansq$counter" . "_2' type='radio' name='ans$val$counter' value='$ans2' /> $ans2</label>
<label> <input id='radioansq$counter" . "_3' type='radio' name='ans$val$counter' value='$ans3'/> $ans3</label>
<label> <input id='radioansq$counter" . "_4' type='radio' name='ans$val$counter' value='$ans4'/> $ans4</label><br>";
$val++;
}
$string= $string . "</div></div>";
echo $string;
}
if($quetype=="text")
{
echo "<div class='div" . $counter . "' style='display:" . $pro . ";' >
<input type='hidden' name='qtype$counter' value = '$quetype'/>
<input type='hidden' name='time$counter' value = '$quetime'/>
<div >
<div style='float:left;width:20%'>Que - $counter.) </div>
<div style='float:left;width:80%'><input type='hidden' name='q$counter' value = '$qid'/>
<p>$que</p>
</div>
</div>
<div>
<div style='float:left;width:20%'>Ans : </div>
<div style='float:left;width:80%;padding-bottom: 20px;'>
<textarea name='textans$counter' id='textans$counter' class='textans$counter' cols='85%' rows='15%' wrap='off'></textarea>
</div>
</div>
</div>";
}
if($quetype=="jumble")
{
$input = array($row["A"], $row["B"], $row["C"],$row["D"]);
shuffle($input); //Shuffle function to shuffle the jumble answer comming from Database
echo "<div class='div" . $counter . "' style='display:" . $pro . ";' >
<input type='hidden' name='qtype$counter' value = '$quetype'/>
<input type='hidden' name='time$counter' value = '$quetime'/>
<div >
<div style='float:left;width:20%'>Que - $counter.) </div>
<div style='float:left;width:80%'><input type='hidden' name='q$counter' value = '$qid'/>
<p>$que</p>
</div>
</div>
<div>
<div style='float:left;width:20%'>Ans : </div>
<div class='sortable' style='float:left;width:80%;padding-bottom: 20px;'>
<div id='jumbleans$counter" . "_1' class='jumblediv'>" . $input[0] . "</div>
<div id='jumbleans$counter" . "_2' class='jumblediv'>" . $input[1] . "</div>
<div id='jumbleans$counter" . "_3' class='jumblediv'>" . $input[2] . "</div>
<div id='jumbleans$counter" . "_4' class='jumblediv'>" . $input[3] . "</div>
</div>
</div>
</div>";
}
if($quetype=="drag_fill")
{
$query = mysql_query("SELECT * FROM tps_answers, tps_right_que_ans WHERE tps_answers.aid = tps_right_que_ans.aid AND qid ='$qid'");
$row_res = mysql_fetch_assoc($query);
$aid = $row_res['aid'];
$ans1 = htmlspecialchars($row_res['ans1']);
$ans2 = htmlspecialchars($row_res['ans2']);
$ans3 = htmlspecialchars($row_res['ans3']);
$ans4 = htmlspecialchars($row_res['ans4']);
$ans5 = htmlspecialchars($row_res['ans5']);
$ans6 = htmlspecialchars($row_res['ans6']);
$ans7 = htmlspecialchars($row_res['ans7']);
$input = array($ans1,$ans2,$ans3,$ans4,$ans5,$ans6,$ans7);
shuffle($input);
echo "<div class='div" . $counter . "' style='display:" . $pro . ";' >
<input type='hidden' name='qtype$counter' value = '$quetype'/>
<div >
<div style='float:left;width:20%'>Que - $counter.) </div>
<div id='question' style='float:left;width:80%'><input type='hidden' name='q$counter' value = '$qid'/>
<p>$que</p>
</div>
</div>
<div>
<div style='float:left;width:20%'>Ans : </div>
<div style='float:left;width:80%;padding:20px;background:lightblue;margin-bottom:10px;' id='answer'>
<label style='padding:20px;background:#ededed'>" . $input[0] . "</label>
<label style='padding:20px;background:#ededed'>" . $input[1] . "</label>
<label style='padding:20px;background:#ededed'>" . $input[2] . "</label>
<label style='padding:20px;background:#ededed'>" . $input[3] . "</label>
<label style='padding:20px;background:#ededed'>" . $input[4] . "</label>
<label style='padding:20px;background:#ededed'>" . $input[5] . "</label>
<label style='padding:20px;background:#ededed'>" . $input[6] . "</label>
</div>
</div>
</div>";
}
if($quetype=="checkbox")
{
$query = mysql_query("SELECT * FROM tps_answers, tps_right_que_ans WHERE tps_answers.aid = tps_right_que_ans.aid AND qid ='$qid'");
$row_res = mysql_fetch_assoc($query);
$aid = $row_res['aid'];
$chkans1 = htmlspecialchars($row_res['ans1']);
$chkans2 = htmlspecialchars($row_res['ans2']);
$chkans3 = htmlspecialchars($row_res['ans3']);
$chkans4 = htmlspecialchars($row_res['ans4']);
echo "<div class='div" . $counter . "' style='display:" . $pro . ";' >
<input type='hidden' name='qtype$counter' value = '$quetype'/>
<input type='hidden' name='time$counter' value = '$quetime'/>
<div >
<div style='float:left;width:20%'>Que - $counter.) </div>
<div style='float:left;width:80%'><input type='hidden' name='q$counter' value = '$qid'/>
<p>$que</p>
</div>
</div>
<div>
<div style='float:left;width:20%'>Ans : </div>
<div style='float:left;width:80%;padding-bottom: 20px;'>
<label>a) <input id='chkans$counter" . "_1' type='checkbox' name='chkans$counter" . "_1' value='$chkans1'/> $chkans1</label><br>
<label>b) <input id='chkans$counter" . "_2' type='checkbox' name='chkans$counter" . "_2' value='$chkans2' /> $chkans2</label><br>
<label>c) <input id='chkans$counter" . "_3' type='checkbox' name='chkans$counter" . "_3' value='$chkans3'/> $chkans3</label><br>
<label>d) <input id='chkans$counter" . "_4' type='checkbox' name='chkans$counter" . "_4' value='$chkans4'/> $chkans4</label><br>
</div>
</div>
</div>";
}
if($quetype=="write-answer")
{
echo "<div class='div" . $counter . "' style='display:" . $pro . ";' >
<input type='hidden' name='qtype$counter' value = '$quetype'/>
<input type='hidden' name='time$counter' value = '$quetime'/>
<div >
<div style='float:left;width:20%'>Que - $counter.) </div>
<div style='float:left;width:80%'><input type='hidden' name='q$counter' value = '$qid'/>
<p>$que</p>
</div>
</div>
</div>";
}
if($quetype=="pick-wrong")
{
echo "<div class='div" . $counter . "' style='display:" . $pro . ";' >
<input type='hidden' name='qtype$counter' value = '$quetype'/>
<input type='hidden' name='time$counter' value = '$quetime'/>
<div >
<div style='float:left;width:20%'>Que - $counter.) </div>
<div style='float:left;width:80%' ><input type='hidden' name='q$counter' value = '$qid'/>
<p id='pickWrongText'>$que</p>
</div>
</div>
</div>";
}
$counter++;
}
if($counter==1)
{
$msg = "No Test is Available or Test is OFF.";
session_destroy();
echo "<script>setTimeout(function() {window.location.href= 'index.php';},4000);</script>";
}
$chars = "1234567890";
$resid = "";
for($i=1;$i<=10;$i++)
{
$rand_user = rand() % strlen ($chars);
$temp_user = substr($chars,$rand_user,1);
$resid .= $temp_user;
}
echo "<input type='hidden' name='unit' value='$uid'/>";
echo "<input type='hidden' name='lid' value='1'/>";
echo "<input type='hidden' name='resid' value='$resid'/>";
$counter--;
echo "<input type='hidden' name='counter' value='$counter' />"
?>
<label class="col-lg-6" ><?php echo "<h2>" . $msg . "</h2>"; ?></label>
<div align="right"><button class="button" id="next" type="button" name="next" >Next</button></div>
<div ><input class="button" id="finish" type="submit" name="finish_test" value="Save & View Test" style="display:none;" /></div>
<div class="col-lg-12" id="data_fetch"></div>
</div> <!-- End of main div -->
Javascript代码: -
function get_selection()
{
var txt = '';
var flag=0;
var seltext='';
alert(window.getSelection().toString());
if(window.getSelection) {
txt = window.getSelection().toString();
} else if(document.selection) {
txt = document.selection.createRange().text;
}
var tr = window.getSelection().getRangeAt(0);
var span = document.createElement("span");
if(flag==0)
{
span.style.cssText = "background:#ff0000;color:#ffffff";
flag=1;
//alert("+ve" + flag);
}
else if(flag==1)
{
span.style.cssText = "color:#000000";
flag=0;
}
tr.surroundContents(span);
}
document.getElementById("pickWrongText").onclick = get_selection;