php HTML - 更改\更新页面内容而不刷新\重新加载页面
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3644585/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
HTML - Change\Update page contents without refreshing\reloading the page
提问by Moon
I get the data from DB and display it in a div... what I want to do is when I click a link it should change the content of the div
我从数据库中获取数据并将其显示在一个 div 中......我想要做的是当我点击一个链接时它应该改变 div 的内容
one option is to pass parameter through URL to itself and reload the page...
一种选择是通过 URL 将参数传递给自身并重新加载页面...
I need to do it without reloading\refreshing...
我需要在不重新加载\刷新...
<?php
$id = '1';
function recp( $rId ) {
$id = $rId;
}
?>
<a href="#" onClick="<?php recp('1') ?>" > One </a>
<a href="#" onClick="<?php recp('2') ?>" > Two </a>
<a href="#" onClick="<?php recp('3') ?>" > Three </a>
<div id='myStyle'>
<?php
require ('myConnect.php');
$results = mysql_query("SELECT para FROM content WHERE para_ID='$id'");
if( mysql_num_rows($results) > 0 ) {
$row = mysql_fetch_array( $results );
echo $row['para'];
}
?>
</div>
The goal is when I click any of the links the contents of the div and php variable\s gets updated without refreshing.... so that user could see new dataand after that if some query is performed it is on new variable\s
目标是当我单击任何链接时,div 和 php 变量的内容会在不刷新的情况下更新.... 以便用户可以看到新数据,然后如果执行某些查询,它会在新变量上进行
p.s I know it is gonna require some AJAX but I don't know AJAX.. so please reply with something by which I can learn... my knowledge is limited to HTML, PHP, some JavaScript & some jQuery
ps 我知道它需要一些 AJAX 但我不知道 AJAX .. 所以请回复一些我可以学习的东西......我的知识仅限于 HTML、PHP、一些 JavaScript 和一些 jQuery
回答by casablanca
You've got the right idea, so here's how to go ahead: the onclick
handlers run on the client side, in the browser, so you cannot call a PHP function directly. Instead, you need to add a JavaScript function that (as you mentioned) uses AJAX to call a PHP script and retrieve the data. Using jQuery, you can do something like this:
您的想法是正确的,所以这里是如何继续:onclick
处理程序在客户端、浏览器中运行,因此您不能直接调用 PHP 函数。相反,您需要添加一个 JavaScript 函数(如您所提到的)使用 AJAX 调用 PHP 脚本并检索数据。使用 jQuery,您可以执行以下操作:
<script type="text/javascript">
function recp(id) {
$('#myStyle').load('data.php?id=' + id);
}
</script>
<a href="#" onClick="recp('1')" > One </a>
<a href="#" onClick="recp('2')" > Two </a>
<a href="#" onClick="recp('3')" > Three </a>
<div id='myStyle'>
</div>
Then you put your PHP code into a separate file: (I've called it data.php
in the above example)
然后你把你的 PHP 代码放到一个单独的文件中:(我data.php
在上面的例子中调用了它)
<?php
require ('myConnect.php');
$id = $_GET['id'];
$results = mysql_query("SELECT para FROM content WHERE para_ID='$id'");
if( mysql_num_rows($results) > 0 )
{
$row = mysql_fetch_array( $results );
echo $row['para'];
}
?>
回答by Sorantis
jQuery will do the job. You can use either jQuery.ajax function, which is general one for performing ajax calls, or its wrappers: jQuery.get, jQuery.post for getting/posting data. Its very easy to use, for example, check out thistutorial, which shows how to use jQuery with PHP.
jQuery 将完成这项工作。您可以使用 jQuery.ajax 函数(通常用于执行 ajax 调用)或其包装器:jQuery.get、jQuery.post 用于获取/发布数据。它非常易于使用,例如,查看本教程,其中展示了如何将 jQuery 与 PHP 结合使用。