duanhuang7591 2015-05-04 15:32
浏览 21

按钮在没有页面刷新的情况下一次浏览一个数组值

I have a function that generates 10 random and unique numbers between 1-20.

function contain($prevItems, $number) {
            for ($k=0; $k<sizeof($prevItems); $k++) {
              if ($prevItems[$k] == $number)
                return true;
              }
              return false;
            }
            $num[0] = rand(1,20);
            $prevItems[0] = $num[0];
            for ($i=1; $i<=10; $i++) {
              $num[$i] = rand(1,10);
              while (contain($prevItems, $num[$i])) {
                $num[$i] = rand (1,20);
              }
              $prevItems[$i] = $num[$i];
           }
           sort($num);

I then have a button that fetches the first number from the array and echoes a text from database based on the number.

<form action="koe.php" method="POST">
<input id="myform" type="submit"  class="btn btn-primary" name="submit" value="new question">
</form

if(isset($_POST['submit'])) {
  if($result = $my->query("SELECT * FROM questions ORDER BY OID DESC LIMIT 1")) {
    if($result = $my->query('SELECT * FROM questions WHERE OID="'.$prevItems[0].'"')) {
      while($t = $result->fetch_object()) {
         echo '<h2>'.$t->text.'</h2>';
      }
    }
  }
}

My problem is this: I want the button to echo the next value from the previously. Like I want to echo prevItems[1] and then prevItems[2] without the page refresh because at the moment every time I press the button, the page refreshes and the function makes new 10 numbers so they're not unique anymore.

I've tried to stop page refresh with javascript

    var frm = $('#myform');
frm.submit(function (ev) {
    $.ajax({
        type: frm.attr('method'),
        url: frm.attr('action'),
        data: frm.serialize(),
        success: function (data) {
            alert('ok');
        }
    });

    ev.preventDefault();
});

I figured it can't work like this though but I'm not sure how to fix it.

To clarify: My problem is to go through array on a button click without page refresh. Everytime a button is pressed, the next number from array would show up. array[0] -> array[1] -> array[2] -> array[3]

  • 写回答

1条回答 默认 最新

  • douwen4125 2015-05-04 15:48
    关注

    I would change your php page to expect a post variable "num"

    if(isset($_POST['num'])) {
      if($result = $my->query("SELECT * FROM questions ORDER BY OID DESC LIMIT     1")) {
        if($result = $my->query('SELECT * FROM questions WHERE     OID="'.$_POST['num'].'"')) {
          while($t = $result->fetch_object()) {
             echo '<h2>'.$t->text.'</h2>';
          }
        }
      }
    }
    

    //you probably want to google "mysql prevent injection" right after you get this working

    Then in your ajax call you can pass in the "num"

    $(function(){
      $("mybutton").on("click", function() {
        $.ajax({
         type: "POST",
         url: "myURL",
         data: { num:myNums[0] },
         success: function (data) {
                $("#output").innerHTML += data;
                myNums.splice(0,1); //take out the first num
            }
        });
      });
    });
    

    评论

报告相同问题?

悬赏问题

  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 MATLAB中streamslice问题
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 51单片机中C语言怎么做到下面类似的功能的函数(相关搜索:c语言)
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端