使用javascript/jquery/php构建动态网站并将html存储在mysql中
我是动态网站编码新手,我有一个项目,最终将扩展到相当大的规模 我无法从我的外部网站html文件或我创建的从mysql表中提取内容的外部PHP文件中提取javascript。我也不太知道如何将静态实现转换为动态实现,我在下面提供了静态版本和部分工作/不完整动态版本的详细信息 点式流程:使用javascript/jquery/php构建动态网站并将html存储在mysql中,javascript,php,html,mysql,dynamic,Javascript,Php,Html,Mysql,Dynamic,我是动态网站编码新手,我有一个项目,最终将扩展到相当大的规模 我无法从我的外部网站html文件或我创建的从mysql表中提取内容的外部PHP文件中提取javascript。我也不太知道如何将静态实现转换为动态实现,我在下面提供了静态版本和部分工作/不完整动态版本的详细信息 点式流程: onclick javascript与php对话 php与mysql对话,访问请求的内容 mysql发送回php php发送到javascript javascript将内容附加、前置或加载(取决于我们希望它做什
- onclick javascript与php对话
- php与mysql对话,访问请求的内容
- mysql发送回php
- php发送到javascript
- javascript将内容附加、前置或加载(取决于我们希望它做什么)到网页上的特定DIV中
<a href="#" id="item1">item1</a>
<script>
$('#item1').click(function() {
$.get("item1.html",function (test) { $("#content").prepend(test);});
});
</script>
内容将加载到的DIV:
<div id="content"></div>
HTML页面上的javascript:
<a href="#" id="item1">item1</a>
<script>
$('#item1').click(function() {
$.get("item1.html",function (test) { $("#content").prepend(test);});
});
</script>
$('#item1')。单击(函数(){
$.get(“item1.html”,函数(test){$(“#content”).prepend(test);});
});
上面的这个例子适用于静态地预编代码,而无需在每次单击我们想要的链接时清除“content”DIV。但是,当尝试将此静态设置转换为动态设置时,我们无法这样做
我还尝试了以下javascript更改,只是想看看是否可以从外部源加载html,但失败了(不确定我的javascript为什么不加载外部页面?):
$('#item1')。单击(函数(){
$.get(”http://localhost/item1.html,函数(test){$(“#content”).prepend(test);};
});
我认为动态外观结构的最终解决方案是:
注释不起作用,只是概念
HTML将显示:
<a href="#" id="I8">item8</a>
javascript概念:
- 通过PageContent.php?ID=I8单击从锚点到PageContent.php的锚点链接传递ID
- 等待PHP PageContent.PHP处理请求
- php以HTML和javascript的形式将内容传递回javascript,然后将内容前置/追加/加载到#content DIV
<script>
$('#item1').click(function() {
$.get("http://localhost/item1.html",function (test) { $("#content").prepend(test);});
});
</script>
<?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);
?>
假设您的内容所在的位置有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;
在运行php脚本后,它会将您的数据从getContent.php返回到index.php上的$.get函数。尝试item1.php,然后在mysql中选择语句后将其存储在数组变量中,然后返回$mysqlData
或echo$mysqlData
,不幸的是,这不是我想要的。我试图获取存储在MYSQL数据库中的HTML内容,并将其传递给PHP,PHP将其传递给javascript并相应地更新DOM。我不太确定你的解释是什么意思?我想这可能接近我想要的,我对javascript/php编程还是比较陌生的,我会尝试实现它,然后回复你。谢谢