頁面與你PHP代碼在服務器端執行。解釋PHP,然後將內容發送到您的瀏覽器。在收到數據後,你的JS代碼在客戶端執行(感謝你的瀏覽器的JS機器)。
如果你想顯示信息,而無需重新加載任何東西,你有2個解決方案:
如果我恢復過程可能是:
1)的第一個要求是在你的主腳本main.php
2)頁面顯示所有項目(僅嵌入ID)並且包含信息容器(隱藏且空的)
示例
<!-- Your list of link with article ID -->
<div>
<a class="articleLink" id="123" href="#">View</a>
<a class="articleLink" id="124" href="#">View</a>
<a class="articleLink" id="125" href="#">View</a>
</div>
<!-- The bloc that will contain info and will be shown in a popup later (hidden) -->
<div id="divInfo">
<input type="text" name="name" value=""/>
<input type="text" name="description" value=""/>
</div>
<script>
$(document).ready(function() {
// add listener to all link
$(".articleLink").click(function(mouseEvent) {
// Retrieve the article ID
var myId = $(this).attr('id');
// Ajax call with the id of the link
// onSuccess, fill the divInfo and show it
$.ajax('getInfo.php', {
dataType: 'json',
type: 'POST',
data: {
articleId: myId
},
success: function(data, status, xhrObj) {
// The data is received, update the info container
$("#divInfo input[name=name]").val(data.name);
$("#divInfo input[name=description]").val(data.desc);
// Show the div in a popup
//...
}
});
return false;
});
});
</script>
3)你點擊一個鏈接,它會運行一個AJAX調用第二個PHP腳本:getInfo.php,給指定的ID
4)腳本檢索數據庫中的數據並最終返回信息(以JSON爲例)
假設您的PHP getInfo。PHP將返回JSON像
{"name":"an article name","description":"the article description"}
注意:您可以在PHP從陣列產生容易JSON與功能
json_encode()
5)你的Ajax調用的方法的onSuccess被調用時收到的數據,你可以使用這些數據來填寫你的表單(這是唯一的,已經存在於頁面中 - 並且被隱藏)。
好運
您是否嘗試過使用AJAX,當用戶點擊文章,然後顯示它之後加載模式DIV內容? –
showForm($ needThisIDbasedOnClick); ..它顯示了什麼? – shnisaka
@Mt。 Shneiders是的,我試過了,這就是我想要的,但無法得到相關的ID爲了顯示值的形式 –