html - How to fetch next row from database on each button click in PHP -
whenever click button, page got refreshed , lost previous values in variables. when use mt_rand
function, fetched rows duplicated. next row on each button click?
fetch random row not necessary, @ least want next row in order.
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> <?php //database initialization $studname =""; $name=""; $task=""; $connection=mysqli_connect("localhost","apb","12345678","apb"); $number=1; if(isset( $_post["button1"])) { $number = mt_rand(1,10); $s="select * student_list num='$number'"; $r= mysqli_query($connection,$s); $row = mysqli_fetch_array($r); $studname = $row["name"]; $name=""; } if(isset($_post["submit5"])) { $numb = mt_rand(1,4); $s="select * computer rn='$numb'"; $r= mysqli_query($connection,$s); $row=mysqli_fetch_array($r); $task = $row["task"]; $studname =""; $name =""; } ?> <html xmlns="http://www.w3.org/1999/xhtml"> <body> <form id="form" action="" name="form1" method="post" > <h1>mptc fresher's day celebration - 2017</h1> <h5>this time lucky 1 ...</h5> <p> <input type="hidden" name="process" value="1";> <input onclick="<?php $number=$number+1;?>" name="button1" type="submit" id="button1" value="generate" /> <br/> <input id="t1" name="textfield" type="text" value="<?php echo $studname; ?>" /> </p> <p> <h5>be ready !! </h5> <input type="submit" value="task" name="submit5" value="computer" /> <br/> <input name="textfield3" type="text" value="<?php echo $task; ?>" /> </p> </form> </body> </html>
first of onclick="<?php $number=$number+1;?>"
nothing. cannot use js event handler execute php code.
what can do, , you're close solution, this:
<p> <input type="hidden" name="process" value="<?php echo $number;?>";> <input name="button1" type="submit" id="button1" value="generate" /> <br/> <input id="t1" name="textfield" type="text" value="<?php echo $studname; ?>" /> </p>
and change php part this:
$number= $_post['process']; if(isset( $_post["button1"])) { $number = $number + 1; } $s="select * student_list num='$number'"; $r= mysqli_query($connection,$s); $row = mysqli_fetch_array($r); $studname = $row["name"]; $name="";
Comments
Post a Comment