如何使用ajax将GET值和php发送到另一个页面?

时间:2014-02-07 07:47:28

标签: php jquery ajax post get

首先我访问

www.mysite.com/index.php?order_by=id asc

我想将$name$_GET[order_by]发送给autoload_process.php,我该怎么办?

的index.php

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Auto Loading Records</title>
<script type="text/javascript" src="js/jquery-1.9.0.min.js"></script>

<?php
include("connect.php");
$items_per_group = 5;
$mysqli = new mysqli($DB_HOST, $DB_USER, $DB_PASSWORD, $DB_NAME);

$results = $mysqli->query("SELECT COUNT(*) as t_records FROM product_details");
$total_records = $results->fetch_object();
$total_groups = ceil($total_records->t_records/$items_per_group);
$results->close(); 
?>

<script type="text/javascript">
$(document).ready(function() {
    var track_load = 0; //total loaded record group(s)
    var loading  = false; //to prevents multipal ajax loads
    var total_groups = <?php echo $total_groups; ?>; //total record group(s)

    $('#results').load("autoload_process.php", {'group_no':track_load}, function() {track_load++;}); //load first group

    $(window).scroll(function() { //detect page scroll

        if($(window).scrollTop() + $(window).height() == $(document).height())  //user scrolled to bottom of the page?
        {

            if(track_load <= total_groups && loading==false) //there's more data to load
            {
                loading = true; //prevent further ajax loading
                $('.animation_image').show(); //show loading image

                //load data from the server using a HTTP POST request
                $.post('autoload_process.php',{'group_no': track_load}, function(data){

                    $("#results").append(data); //append received data into the element

                    //hide loading image
                    $('.animation_image').hide(); //hide loading image once data is received

                    track_load++; //loaded group increment
                    loading = false; 

                }).fail(function(xhr, ajaxOptions, thrownError) { //any errors?

                    alert(thrownError); //alert with HTTP error
                    $('.animation_image').hide(); //hide loading image
                    loading = false;

                });

            }
        }
    });
});
</script>
<style>
body,td,th {font-family: Georgia, Times New Roman, Times, serif;font-size: 15px;}
.animation_image {background: #F9FFFF;border: 1px solid #E1FFFF;padding: 10px;width: 500px;margin-right: auto;margin-left: auto;}
#results{width: 500px;margin-right: auto;margin-left: auto;}
#resultst ol{margin: 0px;padding: 0px;}
#results li{margin-top: 20px;border-top: 1px dotted #E1FFFF;padding-top: 20px;}
</style>
</head>

<body>

<ol id="results">
</ol>
<div class="animation_image" style="display:none" align="center"><img src="ajax-loader2.gif"></div>


</body>
</html>

autoload_process.php

<?php
include("connect.php");
$items_per_group = 5;
$mysqli = new mysqli($DB_HOST, $DB_USER, $DB_PASSWORD, $DB_NAME);

if($_POST)
{
    //sanitize post value
    $group_number = filter_var($_POST["group_no"], FILTER_SANITIZE_NUMBER_INT, FILTER_FLAG_STRIP_HIGH);

    //throw HTTP error if group number is not valid
    if(!is_numeric($group_number)){
        header('HTTP/1.1 500 Invalid number!');
        exit();
    }

    //get current starting point of records
    $position = ($group_number * $items_per_group);

    //Limit our results within a specified range. 
    $results = $mysqli->query("SELECT * FROM product_details WHERE seller = '$name' ORDER BY $_GET[order_by] LIMIT $position, $items_per_group");

    if ($results) { 
        //output results from database

        while($obj = $results->fetch_object())
        {
            echo '<li id="item_'.$obj->id.'">'.$obj->id.' - <strong>'.$obj->seller.'</strong></span> &mdash; <span class="page_message">'.$obj->description.'</span></li>';
        }

    }
    unset($obj);
    $mysqli->close();
}
?>

2 个答案:

答案 0 :(得分:0)

$ .post的第二个参数是“data”。您可以将变量作为对象发送。

例如:{ 'group_no': track_load, name: 'John Smith', order_by: 'id asc' }

答案 1 :(得分:0)

首先将url参数值保存到这样的隐藏输入框

<input type="hidden" id="orderby" value="<?php echo $_GET['orderby']; ?>"/>

和会话可变

<input type="hidden" id="name" value="<?php echo $_SESSION['Username']; ?>"/>

然后像这样传递$ .POST中的值

var orderby = $("#orderby").val();

var name = $("#name").val();

$.post('autoload_process.php',{'group_no': track_load, 'order_by':orderby, 'name':name} , function(data){