将变量从 JS 传递到 PHP
Passing variables from JS to PHP
我正在尝试将变量从JS传递给PHP,但到目前为止没有运气。我一直在这里寻找解决方案,但似乎没有任何帮助。
好的,我有带有分页的 php 文件:
$pagination .= '<li><a href="#" class="paginate_click" id="'.$i.'-page">'.$i.'</a></li>';
Paginate_click启动 js 函数:
$(".paginate_click").click(function (e) {
$("#results").prepend('<div class="loading-indication"><img src="ajax-loader.gif" /> Loading...</div>');
var clicked_id = $(this).attr("id").split("-"); //ID of clicked element, split() to get page number.
var page_num = parseInt(clicked_id[0]); //clicked_id[0] holds the page number we need
$('.paginate_click').removeClass('active'); //remove any active class
//post page number and load returned data into result element
//notice (page_num-1), subtract 1 to get actual starting point
$("#results").load("views/fetch_articles.php", {'page':(page_num-1)}, function(){
$(window).scrollTop(0);
});
$.post('views/articles_list.php', {'page':(page_num)});
$(this).addClass('active'); //add active class to currently clicked element (style purpose)
return false; //prevent going to herf link
});
在 php 文件中,我需要信息我当前所在的分页页面,所以我想page_num值检索回我的 php。我试过这个:
$.post('views/articles_list.php', {'page':(page_num)});
在 php 中:
$page_number = $_POST["page"];
我也尝试了许多其他选择,但没有任何帮助。我以为会更容易:/
你可能注意到了,还有另一个php文件(fetch_articles.php),在本例中为$_POST["page"];有效。但是对于articles_list.php我不能使用加载功能。
编辑:我想要什么和整个代码。
我有简单而漂亮的分页。唯一的问题是它没有上一个/下一个选项,它显示了所有按钮。当您有很多页面时,这是一个问题。所以我的想法是缩小它,而不是增加 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,15,16,17,18,19 等等,我想要这个:上一页,1,2,3...67,68,下一页。为此,我需要将有关实际页面的信息传递给我的 php 文件。有了这个变量,我可以计算所有内容并使用 for/if/else 语句组织我的分页。
代码。
articles_list.php:
<?php
include("../config/connection.php");
include('../config/css.php');
$results = mysqli_query($dbc_connection,"SELECT COUNT(*) FROM articles");
$get_total_rows = mysqli_fetch_array($results); //total records
//break total records into pages
$pages = ceil($get_total_rows[0]/$item_per_page);
//create pagination
if($pages > 1)
{
$pagination = '';
$pagination .= '<ul class="paginate">';
for($i = 1; $i<=$pages; $i++)
{
$pagination .= '<li><a href="#" class="paginate_click" id="'.$i.'-page">'.$i.'</a></li>';
}
$pagination .= '<li><a href="#" class="paginate_click" id="'.$page_number.'-page">'.$page_number.'</a></li>'; // only to check if variable is passed
$pagination .= '</ul>';
}
?><!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="js/jquery-1.9.0.min.js"></script>
<script type="text/javascript" src="js/pagination.js"></script>
</head>
<body>
<?php $page_number = $_POST["page"];
echo $page_number; // only to check if variable is passed ?>
<div id="results"></div>
<?php echo $pagination; ?>
</body>
</html>
分页.js:
$(document).ready(function() {
$("#results").load("views/fetch_articles.php", {'page':0}, function() {$("#1-page").addClass('active');}); //initial page number to load
$(".paginate_click").click(function (e) {
$("#results").prepend('<div class="loading-indication"><img src="ajax-loader.gif" /> Loading...</div>');
var clicked_id = $(this).attr("id").split("-"); //ID of clicked element, split() to get page number.
var page_num = parseInt(clicked_id[0]); //clicked_id[0] holds the page number we need
$('.paginate_click').removeClass('active'); //remove any active class
//post page number and load returned data into result element
//notice (page_num-1), subtract 1 to get actual starting point
$("#results").load("views/fetch_articles.php", {'page':(page_num-1)}, function(){
$(window).scrollTop(0);
});
$.post('views/articles_list.php', {page:page_num}, function(data){});
$(this).addClass('active'); //add active class to currently clicked element (style purpose)
return false; //prevent going to herf link
});
});
fetch_articles.php:
<?php
include("../config/connection.php"); //include config file
//sanitize post value
$page_number = filter_var($_POST["page"], FILTER_SANITIZE_NUMBER_INT, FILTER_FLAG_STRIP_HIGH);
//validate page number is really numaric
if(!is_numeric($page_number)){die('Invalid page number!');}
//get current starting point of records
$position = ($page_number * $item_per_page);
//Limit our results within a specified range.
$result = mysqli_query($dbc_connection,"SELECT * FROM articles ORDER BY id DESC LIMIT $position, $item_per_page");
//output results from database
while($row = mysqli_fetch_array($result))
{
?>
<h2><?php echo $row['title']; ?></h2>
<p><i><?php echo 'By '.$row['author']; ?></i></p>
<p><?php echo $row['header']; ?></p>
<a href="http://localhost/site/articles/<?php echo $row['slug'] ?>">Read</a><br>
<hr>
<?php
}
?>
尝试删除page_num
周围的括号:
$.post('views/articles_list.php', {'page':page_num});
你没有说在哪个请求中你无法获得你尝试传递的值。所以,我猜它是在第一个:
$("#results").load(...);
这里的问题是你使用的是 .load() 方法,它等效于 .get()。所以,当你尝试在PHP文件中得到$_POST["page"]时,它不会在那里,因为它实际上在$_GET数组中。
我可能在这里遗漏了一些东西,但你不能手动将查询字符串附加到 url 吗?
$.post('views/articles_list.php?pn=' + page_num);
然后在你的fetch_articles.php你只是把它拉下来
$page_number = $_GET["pn"];
如果失败,您可以随时使用 cookie。
而不是
$.post('views/articles_list.php', {'page':(page_num)});
尝试
$.post('views/articles_list.php', {page:page_num}, function(data){
console.log(data);
},'json');
并仔细检查"视图/articles_list.php"是否是正确的路径。如果您使用的是 Chrome,请通过右键单击 -> 检查元素 -> 网络阅读解析。
添加(发布您的编辑代码后):-请删除文档类型和 HTML 并留下类似的东西。
<?php
include("../config/connection.php");
include('../config/css.php');
$results = mysqli_query($dbc_connection,"SELECT COUNT(*) FROM articles");
$get_total_rows = mysqli_fetch_array($results); //total records
//break total records into pages
$pages = ceil($get_total_rows[0]/$item_per_page);
//create pagination
if($pages > 1)
{
$pagination = '';
$pagination .= '<ul class="paginate">';
for($i = 1; $i<=$pages; $i++)
{
$pagination .= '<li><a href="#" class="paginate_click" id="'.$i.'-page">'.$i.'</a></li>';
}
$pagination .= '<li><a href="#" class="paginate_click" id="'.$page_number.'-page">'.$page_number.'</a></li>'; // only to check if variable is passed
$pagination .= '</ul>';
}
//Assuming you're doing ajax here. so either pagination or page number posting back? if both try below.
$page_number = $_POST["page"];
echo json_encode(array("pagination"=>$pagination,"pageNumber"=>$page_number));
?>
希望对您有所帮助。
- 在SVG地图上添加水的渐变,就像在谷歌地图(PHP/JS)中一样
- PHP/JS-EchoJs函数PHP curl发布后的通知
- FullCalendar backend PHP/JS to symfony 3
- 什么'是一个用于PHP+JS(extJS)开发的IDE,支持ctrl+click
- PHP/JS 文本差异
- 一个页面上有两个引导 php-js 表单
- Piwik:如何跨域(PHP/JS)跟踪访问者(用户ID)
- 使用php-js-msyql的实时更新消息系统
- 使用PHP/JS自动刷新Highcharts
- 如何为PHP/JS游戏制作倒计时/定时器
- node.js可以选择性地引入到用普通PHP/js/CSS/HTML构建的web应用程序中吗
- 用PHP/JS开发依赖表单字段的最佳方法
- 无法从数字中删除空白(PHP/JS)
- PHP/JS:动态Dropboxes和回显SELECTED值
- 更改数据库值AJAX PHP JS
- '套接字连接错误'在Aptana 3上调试PHP/JS时
- PHP/JS:用JS函数发布输入值,并通过PHP进行回显
- 在“onexit”或“onsubmit”之后,查询mysql并填充表单字段,而无需离开页面php js
- "Share-to-Facebook" html / php / js
- 从 PHP-JS 返回数组的计算的读取值