Ajax不显示mysql数据库的结果

时间:2015-01-12 05:35:48

标签: php jquery mysql ajax json

我的ajax请求有问题而且我无法弄清楚出了什么问题,在控制台中没有错误......

这是我的javascript文件:



$(document).ready(function(){
	done();	
});

function done(){
	setTimeout(function(){
		updates();
		done();
		}, 50000);
}

function updates() {
	$.getJSON("sedinte.php", function(data){
		$("#results").empty();
		$(".pagination").empty();

		$.each(data.result, function() {$("#results").append("<tr class='"+this['aclass']+"'><td>"+this['object']+"</td><td>"+this['date']+"</td><td>"+this['amount']+"</td><td>"+this['paid']+"</td></tr>");
			});
		$.each(data.pagination, function() {
				$("ul.pagination").append(this['controls']);
			});
	});
}

$("#btnAdd").click(function(){		
	$.post(
		$("#addForm").attr("action"), $("#addForm :input").serializeArray(), function(info) {
			$("#feedback").html(info);		
	});
	clearInput();
});

$("#addForm").submit(function(){
	return false;
});

function clearInput() {
	$("#addForm :input").each( function() {
	   $(this).val(' ');
	});
}
&#13;
&#13;
&#13;

这是我要求的文件:

&#13;
&#13;
<?php
     
    #Get the total number of rows
    $rows = $db->getTotalRows("sedinte");
    
    #Set number of result to show on each page
    $page_rows = 10;
    
    #Get the last page
    $last = ceil($rows/$page_rows);
    
    #Make sure the last page is not below 1
    if($last <1)
    {
        $last = 1;
    }
    $pagenum = 1; #Default page
    
    #Get page number from URL vars if it is pressent, else it is = 1
    if(isset($_GET['page']))
    {
        $pagenum = preg_replace('#[^0-9]#', '', $_GET['page']);
    }
    
    #Make sure the page number isn't below 1, or more than our last page
    if($pagenum < 1)
    {
        $pagenum = 1;
    }
    else if($pagenum > $last)
    {
        $pagenum = $last;
    }
    
    #This sets the range of rows to query for the chosen page number
    $limit = 'LIMIT ' .($pagenum - 1) * $page_rows . ',' .$page_rows;
    
    #This is for grabbing just one page worth of rows by applying limit
    $sql = "SELECT * FROM sedinte ORDER BY id DESC $limit";
    $query = mysqli_query($db->Conn(), $sql);
       
    #Enstablish the pagination controls variable
    $paginationCtrls = null;
    #<li><a href="#">2 <span class="sr-only">(current)</span></a></li>
    #If there is more than 1 page worth of results
    if($last != 1)
    {
        if($pagenum > 1)
        {
            $previous = $pagenum - 1;
            $paginationCtrls .= '<li><a href="'. $_SERVER['PHP_SELF'].'?page=' .$previous.'">&laquo;</a></li>';
            
            #Render clickable number links that should appear on the left of the target page number
            for($i = $pagenum - 4; $i < $pagenum; $i++)
            {
                if($i > 0)
                {
                    $paginationCtrls .= '<li><a href="'.$_SERVER['PHP_SELF'].'?page=' .$i.'">'.$i.'</a></li>';
                }
            }
        }
        
        #Render the current page
        $paginationCtrls .= '<li class="active"><a href="">'.$pagenum.'<span class="sr-only">(current)</span></a></li>';
        
        #Render clickable number lnks that should appear on the right of the target page number
        for($i = $pagenum+1; $i <= $last; $i++)
        {
          $paginationCtrls .= '<li><a href="'.$_SERVER['PHP_SELF'].'?page=' .$i.'">'.$i.'</a></li>';
          if($i >= $pagenum+4)
          {
            break;
          }  
        }
        
        if($pagenum != $last)
        {
            $next = $pagenum + 1;
            $paginationCtrls .= '<li><a href="'. $_SERVER['PHP_SELF'].'?page=' .$next.'">&raquo;</a></li>';
        }
        
    }
$paginationArray = array();
array_push($paginationArray, array('controls' => $paginationCtrls));

$listTest = null;
$resultArray = array();

while($row = mysqli_fetch_array($query, MYSQLI_ASSOC))
{
    //$id = $row['id'];
    $obiect = $row['obiect'];
	$data = $row['data'];
    $suma = $row['suma'];    
	$achitat = $row['achitat'];
        if($achitat == 'Da')
    {
        $active_class= null;
    }
    else
    {        
        $active_class= "danger";
    }   
    array_push($resultArray, array('object' => $obiect,
                              'date' => $data,
                              'amount' => $suma,
                              'paid' => $achitat,
                              'aclass' => $active_class
                               ));
    

   /*
    $listTest .='   <tr class="'.$active_class.'">                                 
                        <td>'.$obiect.'</td>
                        <td>'.$data.'</td>
                        <td>'.$suma.'</td>
                        <td>'.$achitat.'</td> 
                    </tr>';*/
  
}
echo json_encode(array("result" => $resultArray, "pagination" => $paginationArray));
mysqli_close($db->Conn());
?>
&#13;
&#13;
&#13;

这是我的索引文件:

&#13;
&#13;
<?php
 require 'includes/settings.php';
?>
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Sedinte</title>

    <!-- Bootstrap -->
    <!-- Latest compiled and minified CSS -->
	<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css">	
	<!-- Optional theme -->
	<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap-theme.min.css">	
	<!-- Latest compiled and minified JavaScript -->
	

    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
      <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
  </head>
  <body>
    <div class="container-fluid">
	<div class="row">
		
	<div class="col-xs-12 col-md-12">
		<div role="tabpanel">
		
		  <!-- Nav tabs -->
		  <ul class="nav nav-tabs" role="tablist">
		    <li role="presentation" class="active"><a href="#sedinte" aria-controls="sedinte" role="tab" data-toggle="tab"><span class="glyphicon glyphicon-eye-open"></span> Vezi Sedinte</a></li>
		    <li role="presentation"><a href="#adauga" aria-controls="adauga" role="tab" data-toggle="tab"><span class="glyphicon glyphicon-plus-sign"></span> Adauga Sedinta</a></li>
		  </ul>
		
		  <!-- Tab panes -->
		  <div class="tab-content">
		    <div role="tabpanel" class="tab-pane active" id="sedinte">
		    	<div class="row">
		    		<div class="col-xs-6 col-md-4">
		    			<h4><span class="glyphicon glyphicon-usd"></span>  Buget: 460 RON</h4>
		    		</div>
		    		<div class="col-xs-6 col-md-4 col-md-offset-4">
		    			<h4><span class="glyphicon glyphicon-calendar"></span> Data: <?php echo date('D d M Y');?></h4>
		    		</div>
		    	</div>
		    	
		    	<table class="table table-striped table-hover">
 						<thead>
 							<tr>								
		 						<th><span class="glyphicon glyphicon-edit"></span> Obiect</th>
		 						<th><span class="glyphicon glyphicon-calendar"></span> Data</th>
		 						<th><span class="glyphicon glyphicon-usd"></span> Suma</th>
		 						<th><span class="glyphicon glyphicon-ok"></span> Achitat</th>		 						
	 						</tr>
 						</thead>
 						<tbody id="results">
 							<?php //echo $listTest;?>
 						</tbody>
				</table>
				<ul class="pagination">
					<?php //echo $paginationCtrls;?>
				</ul>
		    </div>
		    <div role="tabpanel" class="tab-pane" id="adauga">
		    	<div class="row">
		    		<div class="col-md-6 col-md-offset-1">
		    			<div class="panel panel-default">
		    				<div class="panel-body">
				    			<form action="add.php" method="post" id="addForm">
				    				<div class="form-group">
				    					<label for="obiect">Obiect</label>
										<input type="text" class="form-control" name="boxObiect" placeholder="M, L, R">
										<!-- 
										<select name="boxObiect" class="form-control">
										  <option>Matematica</option>
										  <option>Romana</option>
										  <option>Logica</option>								
										</select>
										-->
				    				</div>
				    				<div class="form-group">
				    					<label for="data">Data: </label>
				    					<input type="text" class="form-control" name="boxData" placeholder="YYYY-MM-DD">
				    				</div>
				    				<div class="form-group">
					    				<label for="radioAchitat">Achitat: </label>
					    				<label class="radio-inline">
										  <input type="radio" name="radioAchitat" id="radioAchitat" value="Da"> Da
										</label>
										<label class="radio-inline">
										  <input type="radio" name="radioAchitat" id="radioAchitat" value="Nu" checked="checked"> Nu
										</label>
				    				</div>
				    	
				    				<button type="submit" id="btnAdd" class="btn btn-primary">Adauga</button>
				    			</form>			    			
		    				</div>
		    			</div>
		    		<div id="feedback"></div>
		    		    		
		    	</div>		    	
		    </div>
	
		   </div>
		
		</div>
	</div>
</div>
    <script src="//code.jquery.com/jquery-1.11.2.min.js"></script>
	<script src="//code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
    <!-- Include all compiled plugins (below), or include individual files as needed -->
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script>
    <script src="ajax/main.js"></script>
  </body>
</html>
&#13;
&#13;
&#13;

我的数据库中的数据没有显示......只是主模板。 请帮帮我!

1 个答案:

答案 0 :(得分:0)

我不知道是什么问题,因为我没有阅读完整的代码,但这是我发现的一个错误,所以你可以纠正,并可以再试:),我希望它会工作,谢谢

header('Content-type: application/json');
echo json_encode(array("result" => $resultArray, "pagination" => $paginationArray));
mysqli_close($db->Conn());