简体   繁体   中英

How to get value from mysql database row by row after each click on button?

I want to get questions one by one by clicking on button from mysql database table. Each question is one single row, want to get every next question by clicking on "next" button from every next row. I have made this code but this only shows first question in the table of first row in the database. My html code is:

<span ng-repeat="record in records" id="next">  
 <p id="hello">{{record.ques_no}}.
{{record.question}}</p>
<p><input type="text" ng-model="ans" id="ans" value=""></p>
<p align="center"><a href="#next" id="nex" class="ui-btn ui-corner-all ui-btn-inline" onclick="">Next</a></p>
 </span>

php code getting value from database is:

$result=mysqli_query($con,"SELECT * FROM quest limit 1");
$record=array();
$number = 0;
while($row =mysqli_fetch_array($result))
{
 $record[] = array(
'ques_no'=> $row['ques_no'],
'question'=> $row['question'],
'answer'=> $row['answer']
);
$number++;
}
<html>
    <head>
    <style>
    .invisible{
    display:none;
    }

    .visible{
    display:visible;
    }
    </style>
    <script src="js/jquery.min.js"></script>
    <script>$(function() 
    {
        $( "#button" ).click(function()
        {
            $( "div.container div.invisible" ).first().addClass( "visible" ).removeClass("invisible");
        });
    });
    </script>
    </head>
    <div class="container">
        <div class="invisible">1</div>
        <div class="invisible">2</div>
        <div class="invisible">3</div>
        <div class="invisible">4</div>
        <div class="invisible">5</div>
        <div class="invisible">6</div>
        <div class="invisible">7</div>
        <div class="invisible">8</div>
        <div class="invisible">9</div>
        <div class="invisible">10</div>
    </div>
    <input type="button" id="button"/>
</html>

Something I put together real quick.

$("#nex").click(function() { var formData1 = $("#ques").val();

         $.ajax({
         type:'POST',
        data:{'tota':formData1},
        url:'list1.php',
        success:function(data){
            alert(data);
             var json = $.parseJSON(data);
             // $("#hello").html(data);
             $("#hello").html(json[0]);
        }
        });
        });

list1.php

<?php
$total=$_POST['tota'];
$input=1;
$con=mysqli_connect("localhost","root","root","school");
$result=mysqli_query($con,"SELECT * FROM quest LIMIT $total OFFSET $input");
$record=array();
echo $input;
while($row =mysqli_fetch_array($result))
{
   $record[] = array(
    'ques_no'=> $row['ques_no'],
    'question'=> $row['question'],
    'answer'=> $row['answer']

   );

}

echo json_encode($record);

mysqli_close($con); ?>

Facing some problem in retreiving json data

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM