2012-07-03 160 views
2

I'm making a site using divs. I have one across the top with a row of links which I want to change what's in the main window. I would prefer to keep strictly to PHP and HTML and use a full page refresh as I am comfortable with them at current, rather than using an Ajax refresh as I don't have any knowledge of Ajax.從<a href=""> link

I don't want to use iframes. I thought the best way to do this would be as follows:

<div id="top"> 
<a href="news.html">News</a> 
</div> 
<div id="main"> 
<?php include($page); ?> 
</div> 

What I need is a way of changing the $page variable upon clicking the link. If this means changing the variable and then reloading the page then so be it.

A command following the logic of:

<a href="news.html" action="<?php $page="news.html"; ?>">News</a> 

would be ideal! I thought about using a form, but it seems there should be an easier way.

+0

我認爲什麼是被要求在這裏是如何加載一個新的HTML/PHP頁面中的一個div。你可以使用jQuery的負載來完成。看到一個類似的問題[這裏](http://stackoverflow.com/questions/3564356/loading-html-page-inside-div?rq=1) –

+1

你不需要分析一個變量,你只需要檢查什麼當前頁面是,這個信息就是$ _SERVER數組。 – 2012-07-03 21:10:21

回答

1

you could use a GET request on the same page,

<a href="news.html?page=news.html">News</a> 

This will cause a page refresh on click, with the value for the page variable passed to php, and accessible through

$page = $_GET['page'] //at this point, $page will be "news.html" 
1

If I understood correctly, to change the include on the fly, as you are talking, you need to use AJAX, otherwise you need a full refresh.

Send data back to the server, with a normal link.

<a href="page.php?page=newpage.html">link</a> 

Then you can use the $_GET variable of PHP, which is a array that parse the parameters for a URL page.

Example:

$page = $_GET['page'] 
include $page; 

If you use jQuery, or want to try a easy AJAX method, you can search for jQuery.load()更改PHP變量。

0

你也可以使用

$page = $_SERVER['PHP_SELF']; 

在PHP腳本的頂部。如果你的頁面是「somepage.php」,上面會輸出

/somepage.php 

作爲$ page的值。

爲了擺脫斜線和.PHP只是使用SUBSTR:

$page = substr($_SERVER['PHP_SELF'],1,-4); 

這將留給你$頁= SomePage的

相關問題