2017-08-16 51 views
0

我需要一個JavaScript代碼來實現超時然後顯示一個警報,然後重定向到另一個頁面..之後再超時演出消息重定向

代碼:

<?php 
    session_start(); 
    error_reporting(1); 
    include("database.php"); 
    extract($_POST); 
    extract($_GET); 
    extract($_SESSION); 

    /*$rs=mysql_query("select * from mst_question where test_id=$tid",$cn) or    die(mysql_error()); 
    if($_SESSION[qn]>mysql_num_rows($rs)) 
    { 
    unset($_SESSION[qn]); 
    exit; 
    }*/ 
    if (isset($subid) && isset($testid)) { 
     $_SESSION[sid] = $subid; 
     $_SESSION[tid] = $testid; 
     $sqlq   = "SELECT * FROM mst_result WHERE login='$login' and test_id='$testid'"; 
     $er   = mysqli_query($cn, $sqlq); 
     $rc   = mysqli_num_rows($er); 
     if ($er > 0) { 
      echo " You have already given the exam"; 
      echo " Click <font color='lightgreen'<a href='sublist.php'>Here</a></font> to    return to exam menu"; 
     } else { 
      header("location:quiz.php"); 
     } 

    } 
    if (!isset($_SESSION[sid]) || !isset($_SESSION[tid])) { 
     header("location: index.php"); 
    } 
    ?> 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"  "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
    <head> 
     <script type="text/javascript"> 
      function myFunction() { 
       document.getElementById("myForm").submit(); 
      } 
      function mf() { 
       myVar = setTimeout(myFunction, 3000); 
      } 
     </script> 
     <title>Online Quiz</title> 
     <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> 
     <link href="quiz.css" rel="stylesheet" type="text/css"> 
    </head> 
    <body background="css/bvc.jpg" onload="mf()"> 
     <?php 
      include("header.php"); 
      echo $time; 


      $query="select * from mst_question"; 

      $rs=mysql_query("select * from mst_question where test_id=$tid",$cn) or  die(mysql_error()); 
      if(!isset($_SESSION[qn])) 
      { 
      $_SESSION[qn]=0; 
      mysql_query("delete from mst_useranswer where sess_id='" . session_id()  ."'") or die(mysql_error()); 
       $_SESSION[trueans]=0; 

      } 
      else 
      { 
       if($submit=='Next Question' && isset($ans)) 
       { 
         mysql_data_seek($rs,$_SESSION[qn]); 
         $row= mysql_fetch_row($rs); 
         mysql_query("insert into mst_useranswer(sess_id, test_id,  que_des, ans1,ans2,ans3,ans4,true_ans,your_ans) values ('".session_id()."',  $tid,'$row[2]','$row[3]','$row[4]','$row[5]', '$row[6]','$row[7]','$ans')") or  die(mysql_error()); 
         if($ans==$row[7]) 
         { 
            $_SESSION[trueans]=$_SESSION[trueans]+1; 
         } 
         $_SESSION[qn]=$_SESSION[qn]+1; 
       } 
       else if($submit=='Get Result' && isset($ans)) 
       { 
         mysql_data_seek($rs,$_SESSION[qn]); 
         $row= mysql_fetch_row($rs); 
         mysql_query("insert into mst_useranswer(sess_id, test_id, que_des, ans1,ans2,ans3,ans4,true_ans,your_ans) values ('".session_id()."', $tid,'$row[2]','$row[3]','$row[4]','$row[5]', '$row[6]','$row[7]','$ans')") or die(mysql_error()); 
         if($ans==$row[7]) 
         { 
            $_SESSION[trueans]=$_SESSION[trueans]+1; 
         } 
         echo "<h1 class=head1> Result</h1>"; 
         $_SESSION[qn]=$_SESSION[qn]+1; 
         echo "<Table align=center><tr class=tot><td>Total Question<td> $_SESSION[qn]"; 
         echo "<tr class=tans><td>True Answer<td>".$_SESSION[trueans]; 
         $w=$_SESSION[qn]-$_SESSION[trueans]; 
         echo "<tr class=fans><td>Wrong Answer<td> ". $w; 
         echo "</table>"; 
         mysql_query("insert into mst_result(login,test_id,test_date,score) values('$login',$tid,'".date("d/m/Y")."',$_SESSION[trueans])") or die(mysql_error()); 
         echo "<h1 align=center><a href=review.php> Review Question</a> 
      </h1>"; 
         unset($_SESSION[qn]); 
         unset($_SESSION[sid]); 
         unset($_SESSION[tid]); 
         unset($_SESSION[trueans]); 
         exit; 
       } 
      } 
      $rs=mysql_query("select * from mst_question where test_id=$tid",$cn) or   die(mysql_error()); 
      if($_SESSION[qn]>mysql_num_rows($rs)-1) 
      { 
      unset($_SESSION[qn]); 
      echo "<h1 class=head1>Some Error Occured</h1>"; 
      session_destroy(); 
      echo "Please <a href=index.php> Start Again</a>"; 

      exit; 
      } 
      mysql_data_seek($rs,$_SESSION[qn]); 
      $row= mysql_fetch_row($rs); 
      echo "<form id=myForm method=post action=quiz.php>"; 
      echo "<table width=100%> <tr> <td width=30>&nbsp;<td> <table border=0>"; 
      $n=$_SESSION[qn]+1; 
      echo "<tR><td><span class=style2>Que ". $n .": $row[2]</style>"; 
      echo "<tr><td class=style8><input type=radio name=ans value=1>$row[3]"; 
      echo "<tr><td class=style8> <input type=radio name=ans value=2>$row[4]"; 
      echo "<tr><td class=style8><input type=radio name=ans value=3>$row[5]"; 
      echo "<tr><td class=style8><input type=radio name=ans value=4>$row[6]"; 

      if($_SESSION[qn]<mysql_num_rows($rs)-1) 
      echo "<tr><td><input type=submit name=submit value='Next Question'></form>"; 
      else 
      echo "<tr><td><input type=submit id='t1' name=submit value='Get Result'>  </form>"; 
      echo "</table></table>"; 
      ?> 
    </body> 
</html> 

我在滯留js的功能:

<script type="text/javascript"> 
function myFunction() { 
    document.getElementById("myForm").submit(); 
} 

function mf() 
{ 
    myVar = setTimeout(myFunction, 3000); 
} 
</script> 
+2

請正確格式的代碼,並顯示你在哪裏卡住的代碼,而不是發佈一大堆不必要的代碼 –

+1

**不要**使用**過時和不安全的** _mysql _ * - 函數。從PHP 5.5(2013年)開始,它們已被棄用,並且在PHP 7中(2015年)完全刪除。改用MySQLi或PDO。 2.您對** [SQL Injections](http://php.net/manual/en/security.database.sql-injection.php)廣泛開放,並且確實應該使用[Prepared Statements](http:/ /php.net/manual/en/mysqli.quickstart.prepared-statements.php)而不是連接你的查詢,如果你使用上面提到的MySQLi或PDO,可以使用它們。 –

+0

[SetTimeOut on Alert Box]可能重複(https://stackoverflow.com/questions/25298824/settimeout-on-alert-box) –

回答

0

可能尋找到代碼中,我發現,在提交表格後你必須等待X時間然後重定向它。

我給你建議使用ajax代替。沒關係。這可以是您的需求的解決方案

function myFunction() { 
    document.getElementById("myForm").submit(); 
    //after submitting the form 
} 
function mf() { 
    myVar = setTimeout(myFunction, 3000); 
    alert('submitted'); 
    window.location = url; 
    //where url can be your any url you wanted to redirect. 
} 
0

此功能應該做你問我是否正確理解你的問題。該函數允許您傳入參數,使其在整個代碼中更具動態性和可重用性(如果您需要多個位置的此功能)。這非常簡單,並且有評論。

function alertAndRedirectAfterTimeout(timeoutDuration, alertMsgText, redirectUrl) { 
    setTimeout(function() { /*setTimeout is a "built-in" js function that will call a callback function ONE TIME after a specified duration (in milliseconds)*/ 
     alert(alertMsgText); /*another "built-in" js function that will show an alert on screen*/ 
     window.location.replace('https://www.w3schools.com/js/js_window_location.asp'); /*this is one way to re-direct the browser window to a url*/ 
    }, timeoutDuration); 
} 

/*call the function - you could use this as an event handling function for example when the user clicks on a submit button*/ 
alertAndRedirectAfterTimeout(2500, 'Redirecting to W3 Schools after 2.5 seconds!', 'https://www.w3schools.com/js/js_window_location.asp');