我的独立php文件如何从ajax帖子中接收数据?

时间:2015-08-16 05:59:41

标签: javascript php jquery html ajax

所以我有3个文件。 Header.php,script.js和load_more.php。 Header.php是网站上的普通页眉,script.js是一个被调用并发出ajax请求的文件。 load_more.php是script.js中脚本调用的php文件。

header.php:我得到页面ID($ schoolofficialId)并将其放入javascript变量中以便在script.js中访问ajax数据:

<?php
// There is more code to this but I do get the page ID and put it into $schoolOfficialId so this does work
    $shcoolOfficialId = $specificSchool[0]["id"];
?>

<!--Down in the <head> of the DOM where scripts get included-->
<script type="text/javascript">
    var schoolId = <?php Print($shcoolOfficialId); ?>
</script>
<script type="text/javascript" src="js/script.js"></script>

script.js:进行ajax调用。第二个ajax请求确实有效,但我对第一个ajax感到困惑,我想将js变量schoolId推送到load_more.php

            // execute an ajax query to push id of page to load_more.php
            $.ajax({
                url: 'load_more.php',
                type: 'POST',
                data: {schoolId:schoolId},
                success:function(data){

                }
            });
            // execute an ajax query to load more statements
            $.ajax({
                url: 'load_more.php',
                type: 'POST',
                data: {last_id:last_id, limit:limit},
                success:function(data){
                    // now we have the response, so hide the loader
                    $('#loader').hide();
                    // append: add the new statments to the existing data
                    $('#items').append(data);
                    // set is_loading to false to accept new loading
                    is_loading = false;
                }
            });

load_more.php:因为我的应用程序越来越大,我需要执行switch语句,这就是为什么我需要js变量schoolId来为我带来当前页面id或者在header.php中的php代码中的$ schoolOfficialId:注意:我不想将LOAD_MORE.PHP包含在我的DOM中,以及它的独立PHP文件:

// including the config file
require('config.php');
//pdo connct for config.php file
$pdo = connect();

var_dump($schoolId);
//this var dump returns NULL
switch ($schoolId) {
  case 1:
     echo "first school";
    break;

  case 2:
     echo "second school";
    break;
  default:
     //this default ends up happening because the $schoolId is NULL
     echo "no school";
    break;
}

0 个答案:

没有答案