为什么 php Ajax 不将值传递给 php 文件


why doesn't php Ajax pass value to php file?

我有两个文件:第一个名为semester_1.php,第二个名为pagination.php。我正在尝试将主题的值从semester_1.php传递到pagination.php,然后查询并输出它。我使用 javascript 和 Ajax 来获取 href 的值,然后将其传递给另一个文件,即 pagination.php 。问题是我没有显示也没有错误,这是代码,任何人都可以帮忙吗?值得一提的是,在访问的情况下,这两个文件都在同一个目录中。

semester_1.php

<!doctype html>
<html lang="en">
<head>
<style>
.ui-tabs .ui-tabs-nav {
    margin: 0;
    padding: .2em .2em 0;
    height: 30px;
        font-size: 12px;
}
.ui-corner-all,
.ui-corner-bottom,
.ui-corner-right,
.ui-corner-br {
    border-bottom-right-radius: 4px;
    margin-left: 15px;
    height: 520px;
}
</style>
<meta charset="utf-8" />
<title>jQuery UI Tabs - Default functionality</title>
<link rel="stylesheet" href="pagenation.css" type="text/css" />
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css" />
<script>
$(function() {
$( "#tabs" ).tabs();
});
</script>

<script>
$(function(){
    $('div#tabs ul li a').click(function() {
        var n = $(this).attr('href');
        var p = n.slice(5, 6);
        $.ajax({
            type: 'post',
            url: 'pagenation.php',
            data: {value : p},
            success: function(data) {
                // Do something with response 'data'
            }
        });
    });
});
</script>

</head>
<body>
<div id="tabs">
<ul>
<li><a href="#sub=1">Introduction to Programming</a></li>
<li><a href="#sub=2">Fundamental of Programming</a></li>
<li><a href="#sub=3">Afghan History</a></li>
<li><a href="#sub=4">Mathematics</a></li>
<li><a href="#sub=5">Islamic Study</a></li>
<li><a href="#sub=6">English</a></li>
</ul>
<div id="sub=1">
<br><br>
<?php include('pagenation.php');?>
</div>
<div id="sub=2">
<br><br>
<?php include('pagenation.php');?>
</div>
<div id="sub=3">
<br><br>
<?php include('pagenation.php');?>
</div>
<div id="sub=4">
<br><br>
<?php include('pagenation.php');?>
</div>
<div id="sub=5">
<br><br>
<?php include('pagenation.php');?>
</div>
<div id="sub=6">
<br><br>
<?php include('pagenation.php');?>
</div>

</div>
</body>
</html>

分页.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Papermashup.com | PHP Pagination</title>

 <link rel="stylesheet" href="css/pagenation.css" type="text/css" />
</head>
<body>

<?php
$dbhost                         = "localhost";
$dbuser                         = "root";
$dbpass                         = "";
$dbname                         = "LMS";

$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ("Error connecting to database");
mysql_select_db($dbname);
    $tableName="presentation";      
    $targetpage = "Lecture.php";    
    $limit = 10; 
    $query = "SELECT COUNT(*) as num FROM $tableName";
    $total_pages = mysql_fetch_array(mysql_query($query));
    $total_pages = $total_pages[num];
    $stages = 3;
    $page = mysql_escape_string($_GET['page']);
    if($page){
        $start = ($page - 1) * $limit; 
    }else{
        $start = 0; 
        }   
    // Get page data
        $data = $_POST['value'];
    $query1 = "SELECT * FROM $tableName WHERE Sub_ID='$data' LIMIT $start, $limit";
    $result = mysql_query($query1) or die(mysql_error());
    // Initial page num setup
    if ($page == 0){$page = 1;}
    $prev = $page - 1;  
    $next = $page + 1;                          
    $lastpage = ceil($total_pages/$limit);      
    $LastPagem1 = $lastpage - 1;                    

    $paginate = '';
    if($lastpage > 1)
    {   


        $paginate .= "<div class='paginate'>";
        // Previous
        if ($page > 1){
            $paginate.= "<a href='$targetpage?page=$prev'>previous</a>";
        }else{
            $paginate.= "<span class='disabled'>previous</span>";   }

        // Pages    
        if ($lastpage < 7 + ($stages * 2))  // Not enough pages to breaking it up
        {   
            for ($counter = 1; $counter <= $lastpage; $counter++)
            {
                if ($counter == $page){
                    $paginate.= "<span class='current'>$counter</span>";
                }else{
                    $paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}                    
            }
        }
        elseif($lastpage > 5 + ($stages * 2))   // Enough pages to hide a few?
        {
            // Beginning only hide later pages
            if($page < 1 + ($stages * 2))       
            {
                for ($counter = 1; $counter < 4 + ($stages * 2); $counter++)
                {
                    if ($counter == $page){
                        $paginate.= "<span class='current'>$counter</span>";
                    }else{
                        $paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}                    
                }
                $paginate.= "...";
                $paginate.= "<a href='$targetpage?page=$LastPagem1'>$LastPagem1</a>";
                $paginate.= "<a href='$targetpage?page=$lastpage'>$lastpage</a>";       
            }
            // Middle hide some front and some back
            elseif($lastpage - ($stages * 2) > $page && $page > ($stages * 2))
            {
                $paginate.= "<a href='$targetpage?page=1'>1</a>";
                $paginate.= "<a href='$targetpage?page=2'>2</a>";
                $paginate.= "...";
                for ($counter = $page - $stages; $counter <= $page + $stages; $counter++)
                {
                    if ($counter == $page){
                        $paginate.= "<span class='current'>$counter</span>";
                    }else{
                        $paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}                    
                }
                $paginate.= "...";
                $paginate.= "<a href='$targetpage?page=$LastPagem1'>$LastPagem1</a>";
                $paginate.= "<a href='$targetpage?page=$lastpage'>$lastpage</a>";       
            }
            // End only hide early pages
            else
            {
                $paginate.= "<a href='$targetpage?page=1'>1</a>";
                $paginate.= "<a href='$targetpage?page=2'>2</a>";
                $paginate.= "...";
                for ($counter = $lastpage - (2 + ($stages * 2)); $counter <= $lastpage; $counter++)
                {
                    if ($counter == $page){
                        $paginate.= "<span class='current'>$counter</span>";
                    }else{
                        $paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}                    
                }
            }
        }
                // Next
        if ($page < $counter - 1){ 
            $paginate.= "<a href='$targetpage?page=$next'>next</a>";
        }else{
            $paginate.= "<span class='disabled'>next</span>";
            }
        $paginate.= "</div>";       

}
 //echo $total_pages.' Results';
 // pagination
 echo $paginate;
?>

    <?php
$data = $_POST['value'];
    while($row=mysql_fetch_array($result)){
    ?>
    <tr>
    <td><?php echo $row['Pre_Name']; ?></td>
    <td><?php echo $row['pdesc']; ?></td>
    <td><?php echo $row['pdatein']; ?></td>
    <td><?php echo "<a href='{$row['Path']}' title='download file' target='_blank' >Download</a><br />"?></td>

    </tr>
    <?php }
 ?>




</body>
</html>
在你的

jQuery中你使用POST方法,在你的PHP代码中你使用$_GET['value'];

尝试$data = $_POST['值'];

您遇到使用 POST 方法

传递的数据类型的问题,并且您尝试使用 GET 方法获取,因此最好的解决方案是您可以使用 _REQUEST 美元,因此,您将获得 POST 或 GET。

// Get page data
$data = $_REQUEST['value'];

从 $.ajax 调用中删除此行

// type: 'post', 

如果您仍有问题,请告诉我。

问候