我是一个新的动态网站编码,我有一个项目,最终将规模相当大的。
我无法从我的外部网站html文件或我创建的从mysql表中提取内容的外部PHP文件中提取我的javascript。我也不太知道如何将我的静态实现转换为动态实现,我在下面提供了静态版本和部分工作/不完全动态版本的详细信息。
点状处理:
我有一个有点适用于静态html文件的示例:
HTML页面上的HTML:
<a href="#" id="item1">item1</a>DIV内容将加载到:
<div id="content"></div>HTML页面上的javascript:
<script>
$('#item1').click(function() {
$.get("item1.html",function (test) { $("#content").prepend(test);});
});
</script>上面的示例适用于静态预置代码,而不需要每次单击链接时清除“content”DIV,这正是我们想要的。但是,当试图将此静态设置转换为动态设置时,我们无法这样做。
我还尝试了以下javascript修改,以确定我们是否可以从外部源加载html,但是它没有这样做(不确定为什么我的javascript不加载外部页面?):
<script>
$('#item1').click(function() {
$.get("http://localhost/item1.html",function (test) { $("#content").prepend(test);});
});
</script>我认为动态结构的最终解决方案应该是:
注不起作用,只是概念。
HTML将显示:
<a href="#" id="I8">item8</a>javascript概念:
PHP PageContent.php 不完全
<?php
$conn = mysqli_connect('localhost', 'user', 'pass', 'DB')
or die("Unable to connect to MySQL");
echo "Connected to MySQL<br>";
$sql = "SELECT * FROM `content` where ID='<? ID VARIABLE that was passed from javascript HERE ?>';";
$result = mysqli_query($conn, $sql);
$resultarr = mysqli_fetch_assoc($result);
?>我从未构建过php页面,您可以以这种方式将信息传递给它并从中获得结果,在这种方式中,您可以使用“?ID=”。如何实现PageContent.php?ID=8的通过
发布于 2014-12-12 05:13:04
假设您的内容所在有index.php。。然后在getContent.php上使用mysql查询来获取mysql表中的数据。
$(function(){
$('#item1').click(function(){
var id = 5; //id of your mysql table row
$.get("getContent.php?id=" + id,function(response){
$('#content').html(response); //append the data return from content.php
});
});
});然后在你的getContent.php上
$id = $_GET['id'];
//then here is your mysql query getting data by it's ID and store it in a variable
// sample is $mysqlContentData
echo $mysqlContentData; // and also try using return $mysqlContentData;它将将数据从getContent.php返回到index.php上的$.get函数。
https://stackoverflow.com/questions/27436859
复制相似问题