当我搜索不同的查询时,数据库不会刷新。 -ajax

时间:2015-07-18 20:57:11

标签: javascript php jquery mysql ajax

好吧,让我尽力解释这个细节。

我有这些不同的输入。它有乘客姓名,票号和rloc。 我试图通过票号来搜索乘客。 我有这个搜索按钮,可以在数据库中搜索票号。

我收到了票号,我试图通过点击下一个按钮找到下一个票号。它起到了这一点的作用。 (这些是通过使用ajax完成的)

让我陷入困境的是什么。如果我没有刷新页面,数据库将保留到我单击下一步时使用的数据库。 示例如果我输入111搜索记录,将其拉出来然后我点击下一步它将转到记录112然后113再次点击但没有刷新页面,我输入111并搜索哪些仍然有效但如果我再次点击下一步它将显示114而不是112的记录。

我认为这可能是.on()事件的约束目的,但仍然没有做到正确。有人能给我一个额外的眼睛吗?谢谢。

这是我的html,这是非常基本的

<div class="container">
  <div class="title">
    <h1>Ticket search</h1>
  </div>
  <div class="update-info">
    <h3>{{$num}} files have been convert and updated.</h3>
  </div>
  <div class="sub-container">
    <div class="form-field">
      <label>Ticket Number : </label>
      <input class="ticket-field" type="text" name="ticketNumber" value="" placeholder="Please enter ticket number">
    </div>
    <div class="button-field">
      <input type="submit" class="btn-search"value="Search">
      <button class="btn-update">Update</button>
      <button class="btn-prev" name="previous">PREV</button>
      <button class="btn-next" name="next">NEXT</button>
    </div>
    <input type="hidden" name="ticketHolder" value="">
  </div>
  <div id="text-field">
  </div>
</div>

我的js搜索使用jquery和ajax

$("div.container").on("click", ".btn-search", function(event) {
  event.preventDefault();
  var noError = true;

  var ticketNumber = $.trim($("input[name='ticketNumber']").val());

  if($.isNumeric(ticketNumber) || ticketNumber==""){
    noError = true;
  }else{
    noError = false;
    $("input[name='ticketNumber']").val('');
    alert("please enter a number");
  }

  if(noError){
    $("#text-field").empty();
    $.ajax({
      method: "post",
      url: "/search",
      dataType: "json",
      data: {ticketNumber:ticketNumber},
      success: function(data){
        if(data.length>1){
          $.each(data,function(index,item){
            $("#text-field").append("<h3 class='block-hearder'><span>"+item['dateOfFile']+"</span><span>"+item['paxName']+"</span><span>"+item['airlineName']+"</span></h3><div class='text-block'>"+item['content']+"</div>");
          });

          $( "#text-field" ).accordion( "destroy" );
          $( "#text-field" ).accordion({
            collapsible: true
          });

        }else{
          $.each(data,function(index,item){
            $("#text-field").append("<div class='text-block-single'>"+item['content']+"</div>"+"<script>");
            searchNext(item['systemName'],item['ticketNumber']);
            if(item['content'].indexOf('S') != 0){
              $('.btn-prev').button( "enable" );
              $('.btn-next').button( "enable" );
            }
          });


        }
        $("input[name='ticketNumber']").val('');
      }
    });
  }
});  //end btn-search

js的searchNext()

function searchNext(systemName, ticketNumber){
  var systemName = systemName;
  var ticketNumber = Number(ticketNumber);

  // Making sure this function doesn't get called twice
  if( !this.wasRun ){
    $(".btn-next").click(function(event) {
      event.preventDefault();
      $.ajax({
        method: "post",
        url: "/next",
        dataType: "json",
        data: {systemName: systemName, ticketNumber: ticketNumber},
        success: function(data){
          $("#text-field").empty();
          $("#text-field").append("<div class='text-block-single'>"+data['content']+"</div>");
          if(data['content'].indexOf('>') > 0){
            if((ticketNumber + 1) == data['ticketNumber']){
              ticketNumber++;
              searchNext(ticketNumber, data['systemName']);
            }
          }else{
            $('.btn-next').button( "disable" );
          }

        }
      });
    });  //end btn-next
  }
  this.wasRun = true;
}  //end searchNext()

在我的php中,我的搜索()

public function search(){
    $data = array();
    /* Check if ticket number entered is not null and is number */
    if(($_POST['ticketNumber'] != null) && (is_numeric($_POST['ticketNumber']))){
        $ticketNumber = trim($_POST['ticketNumber']);
    }else{
        $ticketNumber = "";
    }

    if(strlen($ticketNumber) == 10 ){
        $model = Document::where('ticketNumber', '=', $ticketNumber)->get();
    }else{
        $data['content'] = 'entery valid ticket number (10 numbers)';
    }

    $index = 0;
    if(sizeof($model)>0){
        foreach ($model as $key => $value) {
            $document = $value->getAttributes();
                $data[$index]['content']=$document['fileContent'];
                $data[$index]['dateOfFile']=$document['dateOfFile'];
                $data[$index]['systemName']=$document['systemName'];
                $data[$index]['ticketNumber']=$document['ticketNumber'];
            $index++;
        }

    }else{
        $data[$index]['content'] = "Sorry the document does not exist, or hasn't been update yet, please click update and try again.";
    }
    echo json_encode($data);
}  //End search function

在我的php next()中搜索下一条记录

public function next(){
    $data = array();
    $systemName = $_POST['systemName'];
    $nextTicketNumber = $_POST['ticketNumber'] + 1;

    //used to get the nextTicketNumber if there is a sequential nextTicketNumber
    $model = Document::where('systemName', '=', $systemName)
                        ->where('ticketNumber', '=', ($nextTicketNumber))->get();

    if(sizeof($model) == 0){
        $data['content'] = 'Reached end of record';
        return json_encode($data);
    }
    //Getting all the same system number and stores the tickets in an array to find the max ticketNumber
    $getAllModel = Document::where('systemName', '=', $systemName)->get();

    $allTicketNumber = [];
    if(sizeof($getAllModel) > 0){
        foreach($getAllModel as $key => $value){
            $allTicketNumber[] = $value->ticketNumber;
        }
    }
    $maxTicketNumber = max($allTicketNumber);

    if($nextTicketNumber > $maxTicketNumber){
        $data['content'] = 'Reached end of record';
    }else{
        $data['content'] = $model[0]->fileContent;
        $data['ticketNumber'] = $model[0]->ticketNumber;
        $data['systemName'] = $model[0]->systemName;
    }

    echo json_encode($data);
}

0 个答案:

没有答案