我的AJAX Post使用文件的HTML进行响应

时间:2014-04-18 12:29:41

标签: php ajax

我的问题很复杂,我会在问题的最后分析。我创建了一个站点,并使其成为/ view中的每个.php文件在/ controller中都应该具有相同名称的.php。这样我有两个home.php,两个login.php等。我创建了一个AJAX调用,所以我可以将一些数据传输到PHP。

的index.php

<?php

session_start();
$all_url = explode('?', $_SERVER['REQUEST_URI']);
$url = explode('/', $all_url[0]);

//Homepage
if (empty($url[1])) {
$url[1] = 'frontpage';
}

//DB connect
$config = array(
'host' => 'xxx', //CHANGE THIS, DB SERVER
'user' => 'xxx', //CHANGE THIS, DB USER
'password' => 'xxx', //CHANGE THIS, DB PASSWORD
'database' => 'xxx' //CHANGE THIS, DB NAME
);
$DB = new PDO('mysql:host=' . $config['host'] . ';dbname=' . $config['database'], $config['user'], $config['password']);
$DB->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
$DB->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
$DB->exec("SET names utf8");

if (file_exists('controller/' . $url[1] . '.php')) {

$gtpl = 'main';
$restricted = array('cart');

if (!isset($_SESSION['firstVisit']) && $url[1]!='frontpage') {
    $_SESSION['firstVisit'] = false; //ensures we never enter this clause again
    require 'controller/frontpage.php';
}else if (empty($_SESSION['uid']) && in_array($url[1], $restricted)) {
    //not logged in, but trying to access restricted page.
    require 'controller/login.php';
}else{
    require 'controller/' . $url[1] . '.php';
}

require 'view/' . $gtpl . '.php';
} else {
echo 'ERROR - File not found!!!';

}

main.php

<!DOCTYPE HTML>
<html>
<head>
    <meta charset="utf-8">
    <title></title>
    <meta name="description" content="">
    media="screen"/>

    "My styles"

</head>
<body>
    <div class="container">

        <?php require 'view/frontend/'.$tpl.'.php';?>

    </div>

    "My .js scripts"

</body>

main.js

$('#your_button').bind("click", function() {

    var json_data = {"category": "A", "size": "20"};

    $.ajax({
        url: 'home',
        dataType: "json",
        type: "POST",
        cache: false,
        data: json_data,
        success: function (data) {
            if (!data.error) {
                window.location.reload(true);
                alert('k');
            } else {
                alert('error!');
            }
        }
    });
});

由于我的index.php,我不需要使用网址:&#39; home.php&#39;,但这会产生问题:我无法定位特定文件,例如home.php位于/ controller中。每当我写url:&#39; home.php&#39;或网址:&#39; controller / home&#39;我在index.php的末尾收到错误 - &#34;&#39;错误 - 找不到文件!!!&#39;;&#34;。当我将url保留在我的main.js中时,我得到了home.php的整个HTML代码的响应,位于view加上main.php。我似乎无法理解这是怎么发生的。有办法解决吗?

1 个答案:

答案 0 :(得分:0)

您可以在index.php中覆盖您的功能。例如,在您的ajax请求中发送另一个参数,例如custom=true;

....
url: 'home.php?custom=true',
....

并在index.php;

.....
if (!isset($_SESSION['firstVisit']) && $url[1]!='frontpage') {
    $_SESSION['firstVisit'] = false; //ensures we never enter this clause again
    require 'controller/frontpage.php';
}else if (empty($_SESSION['uid']) && in_array($url[1], $restricted)) {
    //not logged in, but trying to access restricted page.
    require 'controller/login.php';
}else{
    require 'controller/' . $url[1] . '.php';
}

$path = (empty($_GET["custom"])) ? 'view/' . $gtpl . '.php' : $gtpl;

require $path;

..........

以上代码将需要您在ajax网址中指定的确切网址。