将变量从JS传递给PHP

时间:2014-11-13 00:45:51

标签: javascript php jquery

我正在尝试将变量从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,我无法使用加载功能。

编辑:我想要的和整个代码。

我有简单而优秀的分页。唯一的问题是它没有prev / next选项,它显示了所有按钮。当你有很多页面时,这是一个问题。所以我的想法是缩小它,而不是起伏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> 

pagination.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
}

?>

4 个答案:

答案 0 :(得分:0)

尝试删除page_num周围的括号:

$.post('views/articles_list.php', {'page':page_num});

答案 1 :(得分:0)

你没有说过哪一个请求你无法获得你想要传递的价值。所以,我猜它是在第一个:

$("#results").load(...);

这里的问题是您使用 .load()方法,该方法相当于 .get()。因此,当您尝试在PHP文件中获取 $ _ POST [“page”]时,它将不会存在,因为它实际上位于 $ _ GET 数组中。

答案 2 :(得分:0)

我可能在这里遗漏了一些内容,但你不能手动将查询字符串附加到网址上吗?

$.post('views/articles_list.php?pn=' + page_num);

然后在你的fetch_articles.php中,你只需用

将它拉出来
$page_number = $_GET["pn"];

如果失败,您可以随时使用cookie。

答案 3 :(得分:0)

而不是

$.post('views/articles_list.php', {'page':(page_num)});

$.post('views/articles_list.php', {page:page_num}, function(data){ 
   console.log(data);
},'json');

并仔细检查是否有/ views / articles_list.php&#39;是正确的道路。如果您使用的是Chrome,请通过右键单击阅读解析 - &gt;检查元素 - &gt;网络

添加(发布您的编辑代码后): - 请删除Doctype和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));

?>

希望它有所帮助。