繁体   English   中英

使用Ajax调用html和php页面

[英]Call html and Php pages with Ajax

我做了一个ajax函数,它在index.php中的ajax-container div中调用我的html页面,所以如果我也想在ajax中调用php页面,我该怎么办?

有我的代码:

htaccess重写

Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^([a-zA-Z0-9\-]*).html$ index.php?p=$1 [L]

index.php

 <div class="link" href="page2.html">go html</div> <div class="link" href="page3.php">go php</div> <div id="ajax-container"> <?php $d = "pages/"; if (isset($_GET['p'])) { $p = strtolower($_GET['p']); if (preg_match("/^[a-z0-9\\-]+$/", $p) && file_exists($d . $p . ".html")) { include $d . $p . ".html"; } else { include $d . "404.html"; } } else { include $d . "home.html"; } ?> </div> 

这是我的ajax函数从文件夹页面/调用HTML页面

 var afficher = function(data, page) { $('#ajax-container').fadeOut(100, function() { $('#ajax-container').empty(); $('#ajax-container').append(data); $('#ajax-container').fadeIn(100, function() {}); }); }; var lastRequest = null; if (lastRequest !== null) { lastRequest.abort(); } var loadPage = function(page, storeHistory) { if (typeof storeHistory === 'undefined') { storeHistory = true; } lastRequest = $.ajax({ url: 'pages/' + page, cache: false, success: function(html) { afficher(html, page); if (storeHistory === true) { history.pushState({ 'key': 'value', 'url': page }, '', page); } }, error: function(XMLHttpRequest, textStatus, errorThrown) { afficher('erreur lors du chagement de la page'); } }); return false; }; window.addEventListener('load', function() { setTimeout(function() { window.addEventListener('popstate', function(e) { if (e.state === null) { loadPage('home.html'); } else { loadPage(e['state']['url'], false); } }); }, 0); }); $(document).ready(function() { $('.link').bind('click', function(e) { e.preventDefault(); var page = $(this).attr('href'); loadPage(page); return false; }); }); 

如果要执行php并显示div上返回的内容,则可以使用jQuery和ajax:

$(function(){
    $(".myDivClass").load('yourPHPFile.php');
}());

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM