2014-11-02 150 views
0

我想製作一個簡單的網站,用戶可以在其中輸入其名稱。但我不知道我該如何做到這一點。更改網頁的內容而不更改URL

這是我想要的。

在我的index.php中,會出現一個input,它允許用戶鍵入他們的姓名。用戶單擊按鈕後,我希望用戶留在index.php但內容不同。 我可以做網站哪個文本框和按鈕。但是我不知道用戶點擊按鈕後應該怎麼做。

不明白我的意思嗎? 例如,這是Facebook登錄頁面。 Source: Google

登錄後,

Source: Google

我們仍然在同一個頁面(的index.php),但內容改變。我知道如何製作網站,但我不知道如何在不更改網址的情況下更改內容(例如www.example.com/index.php?u=blah,我只想要www.example.com/index.php)

任何人都可以提供一個關於如何做到這一點的例子/想法?先謝謝了。 對不起,我的英語不好。

編輯:對不起,我的英文不好。其實我的意思是,同一個網址但內容不同。我使用Google搜索,我認爲cookie是最好的解決方案。

+0

你必須使用JavaScript來做到這一點。 PHP在頁面刷新時在服務器上運行。 – Barmar 2014-11-02 05:22:40

+0

您將要使用'GET'而不是'GET'。 – 2014-11-02 05:25:39

回答

0

你需要使用AJAX:

AJAX允許網頁通過交換少量數據與後臺服務器異步更新。這意味着可以更新網頁的某些部分,而無需重新加載整個頁面。

我認爲這是沒有多大用處的給這裏的AJAX教程,但你可以閱讀w3school's AJAX tutorial。 如果你瞭解基礎知識,我會建議使用jQuery,一個JavaScript庫,它比純JavaScript更簡單。

+0

請參閱上面的修改。 – Jeremy 2014-11-02 05:42:09

0

您需要使用AJAX進行內容更新,而無需重新加載網頁。

JS代碼:

<script type="text/javascript"> 
$(document).ready(function() { 
    var track_load = 0; //total loaded record group(s) 
    var loading = false; //to prevents multipal ajax loads 
    var total_groups = <?php echo $total_groups; ?>; //total record group(s) 

    $('#results').load("autoload_process.php", {'group_no':track_load}, function() {track_load++;}); //load first group 

    $(window).scroll(function() { //detect page scroll 

     if($(window).scrollTop() + $(window).height() == $(document).height()) //user scrolled to bottom of the page? 
     { 

      if(track_load <= total_groups && loading==false) //there's more data to load 
      { 
       loading = true; //prevent further ajax loading 
       $('.animation_image').show(); //show loading image 

       //load data from the server using a HTTP POST request 
       $.post('autoload_process.php',{'group_no': track_load}, function(data){ 

        $("#results").append(data); //append received data into the element 

        //hide loading image 
        $('.animation_image').hide(); //hide loading image once data is received 

        track_load++; //loaded group increment 
        loading = false; 

       }).fail(function(xhr, ajaxOptions, thrownError) { //any errors? 

        alert(thrownError); //alert with HTTP error 
        $('.animation_image').hide(); //hide loading image 
        loading = false; 

       }); 

      } 
     } 
    }); 
}); 
</script> 

HTML代碼:

<ol id="results"></ol> 
<div class="animation_image" style="display:none" align="center"><img src="ajax-loader.gif"> </div> 

你可以找到完整的教程here

+0

請參閱上面的修改。 – Jeremy 2014-11-02 05:41:26

0

您需要爲它使用AJAX。首先,創建一個收集表單數據的頁面,然後在索引頁面中使用AJAX加載另一個提交頁面。您可以檢查https://github.com/jobayerccj/Simple-Admin-Panel-using-PDO-AJAX,我已經使用AJAX創建了一個管理面板,其中我使用AJAX加載了內部頁面而不更改鏈接。

+0

請參閱上面的修改。 – Jeremy 2014-11-02 05:41:02

+0

是的,我已經檢查過,你可以在登錄後檢查我的鏈接,當有人試圖瀏覽不同的頁面時,它會改變內部內容,但並沒有改變網址。 – Jobayer 2014-11-02 06:03:48

0

您可以使用PHP來做到這一點,這是我爲我所有的網站做的這只是一個想法,不知道它是否正是你正在尋找或不是,但這是一個鏈接到我目前的網站如果你想看到它的生活。請務必在點擊網站頂部的鏈接時檢查網址。http://www.thedecoy.net

因此,在您的index.php頁面中,您將擁有您的基本html代碼,包括css和圖片,或者您希望在所有網站上顯示的網站上使用的任何其他內容, div標籤你可以做這種類型的PHP代碼。

<?php 
error_reporting(E_ALL^E_NOTICE); 
function open_page($string){ 
    if (preg_match("/^[a-zA-Z]+$/", $string)){ 
     $string = $string; 
    }else{ 
     $string = ""; 
    } 
    return $string; 
} 
$page = open_page($_GET['page']); 
if ($page){ 
    $file = "pages/".$page.".php"; 
if (file_exists($file)){ 
    $pagename = $page; 
}else{ 
    $pagename = "404"; 
} 
include("pages/".$pagename.".php"); 
}else{ 
    echo 'Anything you want displayed in the content of your main page would go here'; 
} 
?> 

你會創建一個名爲在根文件夾「頁面」目錄下的index.php文件是然後在這裏面,你可以創建其他頁面,例如about.php,contact.php ECT我相信你得到的重點,在這些文件中,你只會放置你想要顯示的內容,這樣你就不需要所有的HTML頭體或任何你想要的內容div本身。

比方說您想鏈接用戶訪問您的網站的contact.php文件,你會簡單地鏈接調用像這樣。

<a href="index.php?page=contact">Contact</a> 

這將保持index.php加載,但將內容更改爲contact.php信息。 該代碼有點草率,並從我的網站本身拉,所以你可能需要處理一些事情。

放入頁面404.php頁文件夾,如果一個頁面沒有找到,所以如果他們進入了一個不存在的鏈接訪問者就會看到該網頁,這將被調用。

不知道如果我留下什麼東西出來,但請隨時問,如果這可以幫助你。