HTML - 无刷新\重新加载页面更改\更新页面内容 [英] HTML - Change\Update page contents without refreshing\reloading the page

查看:341
本文介绍了HTML - 无刷新\重新加载页面更改\更新页面内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从数据库中的数据,并在div中显示它...我想要做的是,当我点击一个链接就应该改变div的内容

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

一种选择是通过URL来传递参数,本身并重新加载页面...

one option is to pass parameter through URL to itself and reload the page...

我需要做的无需重新加载\刷新

<?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>

我们的目标是,当我点击任何div和PHP变量\ s的内容而无需刷新获取更新的链接....让用户可以看到新的数据,并在那之后,如果一些进行查询,它是在新的变量\ S

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 data and after that if some query is performed it is on new variable\s

附言:我知道这是要去需要一些Ajax,但我不知道AJAX的..所以请的东西由我可以了解......我的知识仅限于HTML,PHP,一些JavaScript和放大器答复;一些jQuery

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

推荐答案

您已经有了正确的想法,所以在这里是如何继续:在的onclick 处理程序运行在客户端,浏览器,所以你不能直接调用PHP函数。相反,你需要添加(如你提到的)使用AJAX调用PHP脚本和检索数据的JavaScript函数。使用jQuery,你可以做这样的事情:

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:

<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>

然后你把你的PHP code到一个单独的文件:(我在上面的例子中把它叫做 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'];
  }
?>

这篇关于HTML - 无刷新\重新加载页面更改\更新页面内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆