简体   繁体   English

单击PHP列表中的项目后,将外部页面加载到div中

[英]Load external page into div after clicking on item from php list

I know, I know. 我知道我知道。 From the title up above, you are probably wondering what in in the world I am asking. 从上面的标题开始,您可能想知道我要问的是什么。 Let me try and break this down a little easier because I am stuck and cannot figure this out. 让我尝试更轻松地解决这个问题,因为我被困住了,无法解决。 So here goes.............. 所以这里...........

I have a place on my site where a user can look at a list of news items that have been posted. 我在我的站点上有一个地方,用户可以在其中查看已发布的新闻项目的列表。 All of the news items are listed in a MYSQL database and is retrieved and displayed on the page using PhP, like this: 所有新闻项均列在MYSQL数据库中,并使用PhP检索并显示在页面上,如下所示:

$sql_list = "SELECT * FROM LatestMusic WHERE active = 'y' ORDER BY lm_id DESC";  
 $sql_list_result = mysql_query($sql_list,$connection) or die ('Could not select the List of Latest Music Entries');  

 $bgc = "FFC6C6";  
while($list_data = mysql_fetch_array($sql_list_result)){  
    if ($bgc == "FFC6C6") {  
        $bgc = "FFFFFF";  
    } else {  
        $bgc = "FFC6C6";  
    }  
print "<tr bgcolor=\"#" .$bgc. "\">";  
print "<td align=\"center\" width=\"44\">";  
print "<a href=\"#\" onclick=\"loadXMLDoc()\"><img src=\"/images/b_edit.png\" border=\"0\"></a>";  
print "</td>";  
print "<td align=\"center\" width=\"63\">";  
print "<img src=\"/images/b_drop.png\" border=\"0\">";  
print "</td>";  
print "<td align=\"left\" style=\"padding-left: 15px;\">";  
echo $list_data[1];  
print "</td>";  
print "</tr>";  
}

That part I get. 我得到的那部分。 From there, when the user click on anyone of the entries listed from the database, I need an external PHP page to load inside of a DIV on the same page without loading. 从那里开始,当用户单击数据库中列出的任何条目时,我需要一个外部PHP页面才能在同一页面上的DIV内部加载而不加载。 I am using this coding in the header on the same page in order to achieve such a task: 我在同一页的页眉中使用此编码,以完成此任务:

<script>  
 function loadXMLDoc() {
     var xmlhttp;
     if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari
         xmlhttp = new XMLHttpRequest();
     } else { // code for IE6, IE5
         xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
     }
     xmlhttp.onreadystatechange = function () {
         if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
             document.getElementById("myDiv").innerHTML = xmlhttp.responseText;
         }
     }
     xmlhttp.open("GET", "YSMhome_LMeditform.php", true);
     xmlhttp.send();
 }
 </script>

I can get the external page to load just fine into the DIV without the page reloading. 我可以将外部页面加载到DIV中而无需重新加载页面。 The problem I'm having is this: The page that loads needs to be a form with form fields. 我遇到的问题是:加载的页面必须是带有表单字段的表单。 When a user clicks on an entry in the list from the database, I need to pass a variable from the list (letting me know which entry they clicked on) into the external page that is loading in the DIV so that I can pre-populate the form fields with the data being sent. 当用户单击数据库中列表中的条目时,我需要将列表中的变量(让我知道他们单击了哪个条目)传递到加载到DIV中的外部页面中,以便我可以预先填充表单字段以及要发送的数据。 That is the part I cannot figure out. 这是我无法弄清楚的部分。

If anyone can assist me with this, it would be greatly appreciated. 如果有人可以帮助我,将不胜感激。 I am new to AJAX (this is my first attempt with it), but I am willing to learn what I need to in order to get this to work. 我是AJAX的新手(这是我的第一次尝试),但是我愿意学习我需要的东西才能使它起作用。

When you call your ajax function, you need to pass it the relevant information. 调用ajax函数时,需要向其传递相关信息。 In your example, you should change: 在您的示例中,您应该更改:

onclick=\"loadXMLDoc()\"

to something like (assuming that $list_data[0] is an integer for simplicity): 类似于(假设$list_data[0]是一个简单的整数):

onclick=\"loadXMLDoc({$list_data[0]})\"

Then in javascript, your function definition would look something like: 然后在javascript中,您的函数定义将类似于:

function loadXMLDoc(id)

and later on, you could change the url that is called to something like: 然后,您可以将调用的网址更改为类似以下内容的网址:

"YSMhome_LMeditform.php?id=" + id

Then you would have the ID available in "YSMhome_LMeditform.php as $_GET['id'] . 然后,您将在"YSMhome_LMeditform.php$_GET['id']的身份获得$_GET['id']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM