如何通过单击下一步按钮逐个获取和显示数据

时间:2017-11-17 08:25:47

标签: php jquery html ajax

我有一个测验管理系统,需要从数据库中获取数据并在点击下一个按钮时逐个显示数据。

我使用ajax在使用文件读写功能单击下一个按钮后将数据插入数据库。

我希望为我的问题提供更好,更有效的解决方案。

 <?php
    $host="localhost";
    $user="root";
    $pass="";
    $con=mysqli_connect($host,$user,$pass);//connect to database;
    mysqli_select_db($con,'*****');
    // Create connection
    //$conn = new mysqli($host, $user, $pass);
    ?>

    <html>
    <head>
      <!--Import Google Icon Font-->
      <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
      <!--Import materialize.css-->
      <link type="text/css" rel="stylesheet" href="css/materialize.min.css"  media="screen,projection"/>
      <script type="text/javascript" src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.100.2/js/materialize.min.js"></script>
      <!--Let browser know website is optimized for mobile-->
      <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
      <style>
         nav {
                background-color: #2196F3 !important;
             }
             .second {

                background-color: #61BAE4 !important;
                height:45px;
             }

             .logo {
                    display: inline-block;
                    height: 100%;
                }

             .logo>img {
                    vertical-align: middle
                    padding-bottom: 10px;
                }

                @media only screen and (max-device-width: 480px) {


                @media only screen and (max-device-width: 480px) {

                 .card-panel{
                     width:280px !important;
                 }
                 nav{
                     width:300px !important;
                 }

      </style>
    </head>

    <body style="background-color:  #969696;">   
     <div class="row">


            <nav>
                    <div class="nav-wrapper">
                      <a href="#!" class="brand-logo logo"><img width="60" height="60" src="https://www.timefortest.com/images/logo1.png" alt=""><span>IBPS Portal</span></a>
                      <a href="#" data-activates="mobile-demo" class="button-collapse"><i class="material-icons">menu</i></a>
                      <ul class="right hide-on-med-and-down">
                        <li><a href="#">Dashboard</a></li>
                        <li><a href="#">Candidates</a></li>
                        <li><a href="#">Add Question</a></li>
                        <li><a id="logout" href="logout">Logout</a></li>
                        <li><a id="logout" href="logout"><i class="material-icons">lock_outline</i></a></li>
                      </ul>
                     <!--<ul class="side-nav" id="mobile-demo">
                        <li><a href="sass.html">Dashboard</a></li>
                        <li><a href="badges.html">Candidates</a></li>
                        <li><a href="collapsible.html">Add Question</a></li>
                        <li><a id="logout" href="logout">Logout</a></li>
                        <li><a href="sass.html"><i class="material-icons">lock_outline</i></a></li>

                      </ul>-->
                    </div>
            </nav>
            </div>
    <!--<nav class="second">
    <div class="nav-wrapper">
      <div class="col s12">
        <a href="#!" class="breadcrumb">Dashboard</a>
        <a href="#!" class="breadcrumb">Add Questions</a>
        <a href="#!" class="breadcrumb">Multiple Choice</a>
      </div>
    </div>
    </nav>-->
    <?php $var=0;?><?php


    //<div id="response">
    ?><form method="POST" action="submitanswer.php" id="answerdata">
    <?php
    $myfile = fopen("hitcount.txt", "r") or die("Unable to open file!");
    $fh=fread($myfile,filesize("hitcount.txt"));
    fclose($myfile);
            $sql="SELECT description,question,choiceA,choiceB,choiceC,choiceD,choiceE,msqid FROM master_question where msqid=$fh";
        if($r=mysqli_query($con,$sql))
        {
            while($result= $r->fetch_row())
            {



    ?> 
    <div class="col s12 m12"style="background-color:Orange;">
    <div class="row">
    <div class="col s6 m6">

                <h4 style="color:white;">DESCRIPTION</h4>
                <div class="card horizontal" style="height:500px;background-color:white;">  

                  <div class="card-stacked">
                    <div class="card-content">
                      <h5> <?php echo $result[0]?> </h4>
                    </div>
                    </div>

                  </div>
                </div>
    <div class="col s6 m6">
                <h4 style="color:white;">QUESTION</h4>
                <div class="card horizontal"style="height:500px;background-color:white;">

                  <div class="card-stacked">
                    <div class="card-content">
                      <h5><?php echo $result[1]?></h4>

                      </br />

                       <div class="card-stacked"style="padding-top:40px;">
                        <div class="card-content">

                        <input type="hidden" name="msqid" value="<?php echo $result[7]?>"/> 
                        <p>
                          <input type="radio" name="group" id="test1" name="test1" value="<?php echo $result[1]?>" />&nbsp;&nbsp;
                          <label for="test1"><Strong> <?php echo $result[2]?></strong></label>

                        </p>
                        <br />
                        <p>
                          <input type="radio" name="group" id="test2" name="test2" value="<?php echo $result[2]?>" />&nbsp;&nbsp;
                          <label for="test2"><Strong><?php echo $result[3]?></strong></label>

                        </p>
                        <br />
                        <p>
                          <input type="radio" name="group" id="test3" name="test3" value="<?php echo $result[3]?>" />&nbsp;&nbsp;
                          <label for="test3"><Strong><?php echo $result[4]?></strong></label>

                        </p>
                        <br />
                        <p>
                          <input type="radio" name="group" id="test4" name="test4" value="<?php echo $result[4]?>" />&nbsp;&nbsp;
                          <label for="test4"><Strong><?php echo $result[5]?></strong></label>

                        </p>
                        <br />
                        <p>
                          <input type="radio" name="group" id="test5" name="test5" value="<?php echo $result[5]?>" />&nbsp;&nbsp;
                          <label for="test5"><Strong><?php echo $result[6]?></strong></label>

                        </p>

                      </div>
                      </div>
                    </div>

                  </div>
                </div>

    </div>
    </div>

            <?php } }  ?>
               <div class="col s12 m12">
                <div class="row">
                <div class="col s6 m6">
                <center>
                <input type='button'value='back' class="btn" onclick="document.location.href='decrement.php';"/>
               </center>

                </div>
                <div class="col s6 m6 offset-s3">
                <center>
                <button type="submit" id="submcq" value="Submit" name="Snext" class="btn btn-success">Next</button></center>    
                </center>

                </div>
                </div>
                </div>


    </div>
    </form>
    </div>

    <script type="text/javascript" src="js/materialize.min.js"></script>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
      <script> 
      $(".button-collapse").sideNav();<!--nav jquery starts-->
        $(document).ready(function() {
                                    Materialize.updateTextFields();
                                    $(".button-collapse").sideNav();
                                    $('select').material_select();
                                    });
            </script>
    <script>    
    function copy() {
    document.getElementById("label").innerHTML = document.getElementById("mySelect").value
    }<!--to label the selected value-->     
     $(document).ready(function() {
    $('select').material_select();
    });   </script>

    <script><!--ajax function to insert answer in answer table-->
    $(document).ready(function(){
    $("answerdata").on('submit',(function (e){
      e.preventDefault();

       $.ajax({
           type:'POST',
           url:'submitanswer.php',
           data:$('#answerdata').serialize(),
           contentType:true,
           cache:true,
           processData:true,
          success:function(){
            alert("worked");
        }
       });

    }));


    return false;
    });


    </script>
    </body>
    </html>

插入页面:

    <?php
$host="localhost";
$user="root";
$pass="";
$con=mysqli_connect($host,$user,$pass);//connect to database;
mysqli_select_db($con,'******');
?>
<?php

   if($_SERVER['REQUEST_METHOD']=='POST'){

       $msqid=$_POST['msqid'];
       $answer=$_POST['group'];

     $sql="INSERT INTO review(question_attend,Question_id,answerA,answerB,answerC,answerD,answerE)VALUES('','$msqid','$answer','','','','')";
     $query= mysqli_query($con,$sql);
          if($query)
          {
              echo "success";


             $fp = fopen('hitcount.txt', 'c+');
                flock($fp, LOCK_EX);

                $count = (int)fread($fp, filesize('hitcount.txt'));
                ftruncate($fp, 0);
                fseek($fp, 0);
                fwrite($fp, $count + 1);

                flock($fp, LOCK_UN);
                fclose($fp);
              header("Location: newfetch.php"); 


          }
          else
          {
              echo mysqli_error;
          }

   }
?>

0 个答案:

没有答案