使用ajax和PHP打开页面
我想用ajax打开一个页面,用ajax在浏览器url中显示链接。当然,页面不会重新加载整个页面,只会加载内容。 这是我的代码,但我一直有一个错误“注意:第2行C:\xampp\htdocs\www\html5 history api\menu1.php中的未定义索引:rel” 我希望这个错误消失 header.php:使用ajax和PHP打开页面,php,ajax,Php,Ajax,我想用ajax打开一个页面,用ajax在浏览器url中显示链接。当然,页面不会重新加载整个页面,只会加载内容。 这是我的代码,但我一直有一个错误“注意:第2行C:\xampp\htdocs\www\html5 history api\menu1.php中的未定义索引:rel” 我希望这个错误消失 header.php: <br>Header Content from header.php</br></br> <style> #menu{font-s
<br>Header Content from header.php</br></br>
<style>
#menu{font-size:20px;}
#content{font-size:30px;}
</style>
<script language="javascript" src="jquery-1.4.4.min.js"></script>
<script>
$(function(){
$("a[rel='tab']").click(function(e){
//e.preventDefault();
/*
if uncomment the above line, html5 nonsupported browers won't change the url but will display the ajax content;
if commented, html5 nonsupported browers will reload the page to the specified link.
*/
//get the link location that was clicked
pageurl = $(this).attr('href');
//to get the ajax content and display in div with id 'content'
$.ajax({url:pageurl+'?rel=tab',success: function(data){
$('#content').html(data);
}});
//to change the browser URL to 'pageurl'
if(pageurl!=window.location){
window.history.pushState({path:pageurl},'',pageurl);
}
return false;
});
});
/* the below code is to override back button to get the ajax content without reload*/
$(window).bind('popstate', function() {
$.ajax({url:location.pathname+'?rel=tab',success: function(data){
$('#content').html(data);
}});
});
</script>
<div id='menu'>
<a rel='tab' href='http://localhost/www/html5-history-api/menu1.php'>menu1</a> |
<a rel='tab' href='http://localhost/www/html5-history-api/menu2.php'>menu2</a> |
<a rel='tab' href='http://localhost/www/html5-history-api/menu3.php'>menu3</a>
</div>
Header.php中的标题内容
#菜单{字体大小:20px;}
#内容{字体大小:30px;}
$(函数(){
$([rel='tab'])。单击(函数(e){
//e、 预防默认值();
/*
如果取消对上述行的注释,html5不受支持的浏览器不会更改url,但会显示ajax内容;
如果有注释,html5不支持的浏览器会将页面重新加载到指定的链接。
*/
//获取已单击的链接位置
pageurl=$(this.attr('href');
//获取ajax内容并以id为“content”的div显示
$.ajax({url:pageurl+'?rel=tab',成功:函数(数据){
$('#content').html(数据);
}});
//将浏览器URL更改为“页面URL”
如果(pageurl!=窗口位置){
window.history.pushState({path:pageurl}',,pageurl);
}
返回false;
});
});
/*下面的代码是重写back按钮,以在不重新加载的情况下获取ajax内容*/
$(窗口).bind('popstate',function(){
$.ajax({url:location.pathname+'?rel=tab',success:function(data){
$('#content').html(数据);
}});
});
|
|
和menu1.php(menu2和3与1相同)
正如警告所说,您没有定义数组索引。你的代码应该是
if (isset($_GET['rel']) && ($_GET['rel'] != 'tab')) {
正在尝试从以下位置更新它:
if($_GET['rel']!='tab'){
为此:
if(isset($_GET['rel']) ? $_GET['rel']!='tab')){
看看这能不能解决问题 检查AJAX调用的方法。你确定你发送的是GET而不是POST吗?如果您正在发送帖子,$\u GET将不会设置“rel”索引。尝试
$.ajax({type:“GET”,…
在尝试比较GET参数之前,应该使用isset()。
if(isset($_GET['rel']) ? $_GET['rel']!='tab')){