2012-07-03 9 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.PHP 변수를 <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을로드하는 방법입니다되고 어떻게 생각/div의 PHP 페이지. jQuery의로드를 사용하여이를 수행 할 수있다. 비슷한 질문을 참조하십시오. [여기] (http://stackoverflow.com/questions/3564356/loading-html-page-inside-div?rq=1) –

+1

변수를 구문 분석 할 필요가 없습니다. 현재 페이지는 $ _SERVER 배열이 될 것입니다. –

답변

2

You can't change a variable, per se. Reloading the page means running the PHP program from scratch. You just have to provide it with different input. From a regular link, that is only possible through the URL.

<a href="foo.php?something=a_value"> 

Data in the query string is accessible via $_GET

echo htmlspecialchars($_GET['something']); 
+0

Perfect를 통해 GET 매개 변수로 전달할 수 있습니다. 그게 나를위한 이상적인 일이야! – aener

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()에서 바꾸기.

0

또한 PHP 스크립트의 상단에

$page = $_SERVER['PHP_SELF']; 

를 사용할 수 있습니다. 페이지가 "somepage.php"이면 위의 결과는 $ page의 값으로

/somepage.php 

으로 출력됩니다.

은 슬래시를 제거하려면 그냥 SUBSTR을 사용 .PHP :

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

은 $ 페이지 = somepage 당신을 떠날 것이다

관련 문제