doushan2311 2017-11-17 08:25
浏览 84

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

I have a quiz management system and need to fetch the data from the database and displaying the data one by one on clicking next button.

I have used ajax to insert data into database after clicking next button by using file read and write function.

I am hoping for a better and efficient solution for my problem.

 <?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>

Insert page:

    <?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条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 微信会员卡接入微信支付商户号收款
    • ¥15 如何获取烟草零售终端数据
    • ¥15 数学建模招标中位数问题
    • ¥15 phython路径名过长报错 不知道什么问题
    • ¥15 深度学习中模型转换该怎么实现
    • ¥15 HLs设计手写数字识别程序编译通不过
    • ¥15 Stata外部命令安装问题求帮助!
    • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
    • ¥15 TYPCE母转母,插入认方向
    • ¥15 如何用python向钉钉机器人发送可以放大的图片?